169 lines
4.6 KiB
TypeScript
169 lines
4.6 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';
|
||
|
|
|
||
|
|
const AntdCard = Card as any;
|
||
|
|
const { Option } = Select;
|
||
|
|
const { Search } = Input;
|
||
|
|
|
||
|
|
// 模拟数据
|
||
|
|
const mockSub = [
|
||
|
|
{ id: 1, name: '运维订阅', deviceType: 'all', level: 'all', channels: ['短信'] },
|
||
|
|
{ id: 2, name: '管理员订阅', deviceType: 'sensor', level: 'error', channels: ['短信', '邮件'] },
|
||
|
|
{ id: 3, name: '设备订阅', deviceType: 'camera', level: 'warn', channels: ['APP推送'] },
|
||
|
|
];
|
||
|
|
|
||
|
|
const AlarmSubscription = () => {
|
||
|
|
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 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: '订阅名称', dataIndex: 'name' },
|
||
|
|
{ title: '设备类型', dataIndex: 'deviceType' },
|
||
|
|
{ title: '告警级别', dataIndex: 'level' },
|
||
|
|
{
|
||
|
|
title: '通知渠道',
|
||
|
|
render: (r) => r.channels.map(i => <Tag key={i}>{i}</Tag>)
|
||
|
|
},
|
||
|
|
{
|
||
|
|
title: '操作',
|
||
|
|
render: (r) => (
|
||
|
|
<Space>
|
||
|
|
<Button type="link" icon={<EditOutlined />} onClick={() => openEdit(r)}>编辑</Button>
|
||
|
|
<Popconfirm title="确定删除?" onConfirm={() => handleDelete(r.id)}>
|
||
|
|
<Button type="link" danger icon={<DeleteOutlined />}>删除</Button>
|
||
|
|
</Popconfirm>
|
||
|
|
</Space>
|
||
|
|
)
|
||
|
|
}
|
||
|
|
];
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div style={{ padding: 24 }}>
|
||
|
|
<AntdCard
|
||
|
|
title="告警订阅"
|
||
|
|
extra={
|
||
|
|
<Space>
|
||
|
|
<Search
|
||
|
|
placeholder="搜索订阅/设备/级别"
|
||
|
|
style={{ width: 260 }}
|
||
|
|
onSearch={handleSearch}
|
||
|
|
/>
|
||
|
|
<Button icon={<PlusOutlined />} type="primary" onClick={openAdd}>
|
||
|
|
新增订阅
|
||
|
|
</Button>
|
||
|
|
</Space>
|
||
|
|
}
|
||
|
|
>
|
||
|
|
<Table
|
||
|
|
rowKey="id"
|
||
|
|
columns={columns}
|
||
|
|
dataSource={filteredList}
|
||
|
|
pagination={{ pageSize: 10 }}
|
||
|
|
/>
|
||
|
|
</AntdCard>
|
||
|
|
|
||
|
|
{/* 新增/编辑 弹窗 */}
|
||
|
|
<Modal
|
||
|
|
title={editingId ? '编辑告警订阅' : '新增告警订阅'}
|
||
|
|
open={modalVisible}
|
||
|
|
onCancel={() => setModalVisible(false)}
|
||
|
|
onOk={handleSave}
|
||
|
|
maskClosable={false}
|
||
|
|
>
|
||
|
|
<Form form={form} layout="vertical">
|
||
|
|
<Form.Item label="订阅名称" name="name" rules={[{ required: true }]}>
|
||
|
|
<Input placeholder="请输入订阅名称" />
|
||
|
|
</Form.Item>
|
||
|
|
|
||
|
|
<Form.Item label="设备类型" name="deviceType" rules={[{ required: true }]}>
|
||
|
|
<Select>
|
||
|
|
<Option value="all">全部设备</Option>
|
||
|
|
<Option value="sensor">传感器</Option>
|
||
|
|
<Option value="camera">摄像头</Option>
|
||
|
|
<Option value="battery">电池设备</Option>
|
||
|
|
</Select>
|
||
|
|
</Form.Item>
|
||
|
|
|
||
|
|
<Form.Item label="告警级别" name="level" rules={[{ required: true }]}>
|
||
|
|
<Select>
|
||
|
|
<Option value="all">全部级别</Option>
|
||
|
|
<Option value="error">严重</Option>
|
||
|
|
<Option value="warn">警告</Option>
|
||
|
|
<Option value="info">提示</Option>
|
||
|
|
</Select>
|
||
|
|
</Form.Item>
|
||
|
|
|
||
|
|
<Form.Item label="通知渠道" name="channels" rules={[{ required: true }]}>
|
||
|
|
<Select mode="multiple" placeholder="请选择通知渠道">
|
||
|
|
<Option value="短信">短信</Option>
|
||
|
|
<Option value="邮件">邮件</Option>
|
||
|
|
<Option value="APP推送">APP推送</Option>
|
||
|
|
</Select>
|
||
|
|
</Form.Item>
|
||
|
|
</Form>
|
||
|
|
</Modal>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
};
|
||
|
|
|
||
|
|
export default AlarmSubscription;
|