179 lines
5.3 KiB
TypeScript
179 lines
5.3 KiB
TypeScript
import React, { useState } from 'react';
|
|
import {
|
|
Card, Table, Button, Space, Popconfirm, Tag,
|
|
Modal, Form, Input, Select
|
|
} from 'antd';
|
|
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons';
|
|
import { useTranslation } from 'react-i18next';
|
|
import i18n from '../i18n';
|
|
|
|
const AntdCard = Card as any;
|
|
const { Option } = Select;
|
|
const { Search } = Input;
|
|
|
|
// 模拟数据
|
|
const mockSub = [
|
|
{ id: 1, name: i18n.t('alerts.mockSubOps'), deviceType: 'all', level: 'all', channels: ['短信'] },
|
|
{ id: 2, name: i18n.t('alerts.mockSubAdmin'), deviceType: 'sensor', level: 'error', channels: ['短信', '邮件'] },
|
|
{ id: 3, name: i18n.t('alerts.mockSubDevice'), deviceType: 'camera', level: 'warn', channels: ['APP推送'] },
|
|
];
|
|
|
|
const AlarmSubscription = () => {
|
|
const { t } = useTranslation();
|
|
const [list, setList] = useState(mockSub);
|
|
const [filteredList, setFilteredList] = useState(mockSub);
|
|
const [modalVisible, setModalVisible] = useState(false);
|
|
const [editingId, setEditingId] = useState<number | null>(null);
|
|
const [form] = Form.useForm();
|
|
|
|
// 通知渠道显示映射(值为稳定编码,显示时翻译)
|
|
const channelLabelMap = {
|
|
'短信': t('alerts.sms'),
|
|
'邮件': t('alerts.email'),
|
|
'APP推送': t('alerts.appPush'),
|
|
};
|
|
|
|
// 搜索筛选
|
|
const handleSearch = (value) => {
|
|
const res = list.filter(item =>
|
|
item.name.includes(value) ||
|
|
item.deviceType.includes(value) ||
|
|
item.level.includes(value)
|
|
);
|
|
setFilteredList(res);
|
|
};
|
|
|
|
// 打开新增
|
|
const openAdd = () => {
|
|
setEditingId(null);
|
|
form.resetFields();
|
|
setModalVisible(true);
|
|
};
|
|
|
|
// 打开编辑(回填数据)
|
|
const openEdit = (record) => {
|
|
setEditingId(record.id);
|
|
form.setFieldsValue(record);
|
|
setModalVisible(true);
|
|
};
|
|
|
|
// 保存(新增+编辑)
|
|
const handleSave = () => {
|
|
form.validateFields().then(values => {
|
|
if (editingId !== null) {
|
|
// 编辑
|
|
const updated = list.map(item =>
|
|
item.id === editingId ? { ...item, ...values } : item
|
|
);
|
|
setList(updated);
|
|
setFilteredList(updated);
|
|
} else {
|
|
// 新增
|
|
const newItem = { ...values, id: Date.now() };
|
|
const newList = [...list, newItem];
|
|
setList(newList);
|
|
setFilteredList(newList);
|
|
}
|
|
setModalVisible(false);
|
|
});
|
|
};
|
|
|
|
// 删除
|
|
const handleDelete = (id) => {
|
|
const newList = list.filter(x => x.id !== id);
|
|
setList(newList);
|
|
setFilteredList(newList);
|
|
};
|
|
|
|
const columns = [
|
|
{ title: t('alerts.subscriptionName'), dataIndex: 'name' },
|
|
{ title: t('devices.deviceType'), dataIndex: 'deviceType' },
|
|
{ title: t('alerts.alertLevel'), dataIndex: 'level' },
|
|
{
|
|
title: t('alerts.notifyChannel'),
|
|
render: (r) => r.channels.map(i => <Tag key={i}>{channelLabelMap[i] || i}</Tag>)
|
|
},
|
|
{
|
|
title: t('roles.actions'),
|
|
render: (r) => (
|
|
<Space>
|
|
<Button type="link" icon={<EditOutlined />} onClick={() => openEdit(r)}>{t('roles.edit')}</Button>
|
|
<Popconfirm title={t('common.confirmDelete')} onConfirm={() => handleDelete(r.id)}>
|
|
<Button type="link" danger icon={<DeleteOutlined />}>{t('roles.delete')}</Button>
|
|
</Popconfirm>
|
|
</Space>
|
|
)
|
|
}
|
|
];
|
|
|
|
return (
|
|
<div style={{ padding: 24 }}>
|
|
<AntdCard
|
|
title={t('alerts.alarmSubscription')}
|
|
extra={
|
|
<Space>
|
|
<Search
|
|
placeholder={t('alerts.searchSubscription')}
|
|
style={{ width: 260 }}
|
|
onSearch={handleSearch}
|
|
/>
|
|
<Button icon={<PlusOutlined />} type="primary" onClick={openAdd}>
|
|
{t('alerts.addSubscription')}
|
|
</Button>
|
|
</Space>
|
|
}
|
|
>
|
|
<Table
|
|
rowKey="id"
|
|
columns={columns}
|
|
dataSource={filteredList}
|
|
pagination={{ pageSize: 10 }}
|
|
/>
|
|
</AntdCard>
|
|
|
|
{/* 新增/编辑 弹窗 */}
|
|
<Modal
|
|
title={editingId ? t('alerts.editAlertSubscription') : t('alerts.addAlertSubscription')}
|
|
open={modalVisible}
|
|
onCancel={() => setModalVisible(false)}
|
|
onOk={handleSave}
|
|
maskClosable={false}
|
|
>
|
|
<Form form={form} layout="vertical">
|
|
<Form.Item label={t('alerts.subscriptionName')} name="name" rules={[{ required: true }]}>
|
|
<Input placeholder={t('alerts.pleaseInputSubscriptionName')} />
|
|
</Form.Item>
|
|
|
|
<Form.Item label={t('devices.deviceType')} name="deviceType" rules={[{ required: true }]}>
|
|
<Select>
|
|
<Option value="all">{t('alerts.allDevices')}</Option>
|
|
<Option value="sensor">{t('alerts.deviceSensor')}</Option>
|
|
<Option value="camera">{t('devices.camera')}</Option>
|
|
<Option value="battery">{t('alerts.deviceBattery')}</Option>
|
|
</Select>
|
|
</Form.Item>
|
|
|
|
<Form.Item label={t('alerts.alertLevel')} name="level" rules={[{ required: true }]}>
|
|
<Select>
|
|
<Option value="all">{t('alerts.allLevels')}</Option>
|
|
<Option value="error">{t('constants.critical')}</Option>
|
|
<Option value="warn">{t('constants.warning')}</Option>
|
|
<Option value="info">{t('constants.info')}</Option>
|
|
</Select>
|
|
</Form.Item>
|
|
|
|
<Form.Item label={t('alerts.notifyChannel')} name="channels" rules={[{ required: true }]}>
|
|
<Select mode="multiple" placeholder={t('alerts.pleaseSelectChannel')}>
|
|
<Option value="短信">{t('alerts.sms')}</Option>
|
|
<Option value="邮件">{t('alerts.email')}</Option>
|
|
<Option value="APP推送">{t('alerts.appPush')}</Option>
|
|
</Select>
|
|
</Form.Item>
|
|
</Form>
|
|
</Modal>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default AlarmSubscription;
|