import React, { useState, useEffect, useCallback } from 'react';
import { Table, Button, Space, Tag, Tooltip, Popconfirm, Empty, Spin, Badge } from 'antd';
import {
PlusOutlined,
EditOutlined,
DeleteOutlined,
ReloadOutlined,
ApiOutlined,
} from '@ant-design/icons';
import axios from 'axios';
import PortCreateModal from './PortCreateModal';
import { designTokens } from '../config/theme';
function PortManagementPanel({ deviceId, deviceName, onRefresh }) {
const [ports, setPorts] = useState([]);
const [loading, setLoading] = useState(false);
const [createModalVisible, setCreateModalVisible] = useState(false);
const fetchPorts = useCallback(async () => {
if (!deviceId) return;
try {
setLoading(true);
const response = await axios.get(`/api/device-ports/device/${deviceId}`);
setPorts(response.data || []);
} catch (error) {
console.error('获取端口列表失败:', error);
} finally {
setLoading(false);
}
}, [deviceId]);
useEffect(() => {
fetchPorts();
}, [fetchPorts]);
const handleDelete = useCallback(
async port => {
try {
await axios.delete(`/api/device-ports/${port.portId}`);
import('antd').then(({ message }) => message.success('端口删除成功'));
fetchPorts();
onRefresh?.();
} catch (error) {
import('antd').then(({ message }) => message.error('端口删除失败'));
}
},
[fetchPorts, onRefresh]
);
const handleCreateSuccess = useCallback(() => {
fetchPorts();
onRefresh?.();
}, [fetchPorts, onRefresh]);
const getStatusTag = status => {
const config = {
free: { color: 'success', text: '空闲' },
occupied: { color: 'processing', text: '占用' },
fault: { color: 'error', text: '故障' },
};
const { color, text } = config[status] || { color: 'default', text: status };
return