|
|
@@ -1,53 +1,102 @@
|
|
|
-import React from 'react';
|
|
|
-import { Table, Button, Tag } from 'antd';
|
|
|
+import React, { useState } from 'react';
|
|
|
+import { Table, Button, Modal, Form, Input, Select, Spin } from 'antd';
|
|
|
+import { useDeviceManagement } from '../hooks/useDeviceManagement';
|
|
|
+import { Device } from '../types/device';
|
|
|
+
|
|
|
+const { Option } = Select;
|
|
|
|
|
|
const DeviceManagement: React.FC = () => {
|
|
|
+ const { devices, loading, addDevice, updateDevice, deleteDevice } = useDeviceManagement();
|
|
|
+ const [isModalVisible, setIsModalVisible] = useState(false);
|
|
|
+ const [form] = Form.useForm();
|
|
|
+
|
|
|
const columns = [
|
|
|
- {
|
|
|
- title: '设备ID',
|
|
|
- dataIndex: 'id',
|
|
|
- key: 'id',
|
|
|
- },
|
|
|
- {
|
|
|
- title: 'IP地址',
|
|
|
- dataIndex: 'ip',
|
|
|
- key: 'ip',
|
|
|
- },
|
|
|
- {
|
|
|
- title: '状态',
|
|
|
- dataIndex: 'status',
|
|
|
- key: 'status',
|
|
|
- render: (status: string) => (
|
|
|
- <Tag color={status === '在线' ? 'green' : 'red'}>
|
|
|
- {status}
|
|
|
- </Tag>
|
|
|
- ),
|
|
|
- },
|
|
|
+ { title: '设备名称', dataIndex: 'name', key: 'name' },
|
|
|
+ { title: 'IP地址', dataIndex: 'ipAddress', key: 'ipAddress' },
|
|
|
+ { title: '状态', dataIndex: 'status', key: 'status' },
|
|
|
+ { title: '最后在线时间', dataIndex: 'lastSeen', key: 'lastSeen' },
|
|
|
{
|
|
|
title: '操作',
|
|
|
key: 'action',
|
|
|
- render: () => (
|
|
|
- <Button type="link">配置</Button>
|
|
|
+ render: (text: string, record: Device) => (
|
|
|
+ <span>
|
|
|
+ <Button type="link" onClick={() => handleEdit(record)}>编辑</Button>
|
|
|
+ <Button type="link" onClick={() => handleDelete(record.id)}>删除</Button>
|
|
|
+ </span>
|
|
|
),
|
|
|
},
|
|
|
];
|
|
|
|
|
|
- const data = [
|
|
|
- {
|
|
|
- key: '1',
|
|
|
- id: 'DEVICE001',
|
|
|
- ip: '192.168.1.100',
|
|
|
- status: '在线',
|
|
|
- },
|
|
|
- // 更多数据...
|
|
|
- ];
|
|
|
+ const showModal = () => {
|
|
|
+ setIsModalVisible(true);
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleOk = () => {
|
|
|
+ form.validateFields().then(values => {
|
|
|
+ const device = {
|
|
|
+ ...values,
|
|
|
+ lastSeen: new Date().toISOString(),
|
|
|
+ };
|
|
|
+ if (form.getFieldValue('id')) {
|
|
|
+ updateDevice(device as Device);
|
|
|
+ } else {
|
|
|
+ addDevice(device);
|
|
|
+ }
|
|
|
+ setIsModalVisible(false);
|
|
|
+ form.resetFields();
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleCancel = () => {
|
|
|
+ setIsModalVisible(false);
|
|
|
+ form.resetFields();
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleEdit = (device: Device) => {
|
|
|
+ form.setFieldsValue(device);
|
|
|
+ setIsModalVisible(true);
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleDelete = (id: string) => {
|
|
|
+ Modal.confirm({
|
|
|
+ title: '确认删除',
|
|
|
+ content: '确定要删除这个设备吗?',
|
|
|
+ onOk: () => deleteDevice(id),
|
|
|
+ });
|
|
|
+ };
|
|
|
|
|
|
return (
|
|
|
<div>
|
|
|
<h1>设备管理</h1>
|
|
|
- <Table columns={columns} dataSource={data} />
|
|
|
+ <Button type="primary" onClick={showModal} style={{ marginBottom: 16 }}>
|
|
|
+ 添加设备
|
|
|
+ </Button>
|
|
|
+ {loading ? (
|
|
|
+ <Spin size="large" />
|
|
|
+ ) : (
|
|
|
+ <Table columns={columns} dataSource={devices} rowKey="id" />
|
|
|
+ )}
|
|
|
+ <Modal title="设备信息" visible={isModalVisible} onOk={handleOk} onCancel={handleCancel}>
|
|
|
+ <Form form={form} layout="vertical">
|
|
|
+ <Form.Item name="id" hidden>
|
|
|
+ <Input />
|
|
|
+ </Form.Item>
|
|
|
+ <Form.Item name="name" label="设备名称" rules={[{ required: true }]}>
|
|
|
+ <Input />
|
|
|
+ </Form.Item>
|
|
|
+ <Form.Item name="ipAddress" label="IP地址" rules={[{ required: true }]}>
|
|
|
+ <Input />
|
|
|
+ </Form.Item>
|
|
|
+ <Form.Item name="status" label="状态" rules={[{ required: true }]}>
|
|
|
+ <Select>
|
|
|
+ <Option value="online">在线</Option>
|
|
|
+ <Option value="offline">离线</Option>
|
|
|
+ </Select>
|
|
|
+ </Form.Item>
|
|
|
+ </Form>
|
|
|
+ </Modal>
|
|
|
</div>
|
|
|
);
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
export default DeviceManagement;
|