import React, { useState, useEffect } from 'react'; import { Table, Button, Modal, Form, Input, Select, message, Card, Space, Popconfirm } from 'antd'; import { PlusOutlined, EditOutlined, DeleteOutlined, ReloadOutlined } from '@ant-design/icons'; import axios from 'axios'; import CloseButton from '../components/CloseButton'; const { Option } = Select; function TicketCategoryManagement() { const [categories, setCategories] = useState([]); const [loading, setLoading] = useState(true); const [modalVisible, setModalVisible] = useState(false); const [editingCategory, setEditingCategory] = useState(null); const [form] = Form.useForm(); const fetchCategories = async () => { try { setLoading(true); const response = await axios.get('/api/ticket-categories'); setCategories(response.data || []); } catch (error) { message.error('获取故障分类列表失败'); console.error('获取故障分类列表失败:', error); } finally { setLoading(false); } }; const initCategories = async () => { try { await axios.post('/api/ticket-categories/init'); message.success('初始化分类成功'); fetchCategories(); } catch (error) { message.error('初始化分类失败'); console.error('初始化分类失败:', error); } }; useEffect(() => { fetchCategories(); }, []); const showModal = (category = null) => { setEditingCategory(category); if (category) { form.setFieldsValue({ name: category.name, description: category.description, priority: category.priority, defaultPriority: category.defaultPriority, expectedDuration: category.expectedDuration, isActive: category.isActive, }); } else { form.resetFields(); } setModalVisible(true); }; const handleCancel = () => { setModalVisible(false); setEditingCategory(null); }; const handleSubmit = async values => { try { if (editingCategory) { await axios.put(`/api/ticket-categories/${editingCategory.categoryId}`, values); message.success('分类更新成功'); } else { await axios.post('/api/ticket-categories', values); message.success('分类创建成功'); } setModalVisible(false); fetchCategories(); setEditingCategory(null); } catch (error) { message.error(editingCategory ? '分类更新失败' : '分类创建失败'); console.error(error); } }; const handleDelete = async categoryId => { Modal.confirm({ title: '确认删除', content: '确定要删除这个故障分类吗?此操作不可恢复!', okText: '删除', okType: 'danger', cancelText: '取消', onOk: async () => { try { await axios.delete(`/api/ticket-categories/${categoryId}`); message.success('分类删除成功'); fetchCategories(); } catch (error) { message.error('分类删除失败'); console.error(error); } }, }); }; const columns = [ { title: '分类ID', dataIndex: 'categoryId', key: 'categoryId', width: 150, }, { title: '分类名称', dataIndex: 'name', key: 'name', width: 180, }, { title: '分类说明', dataIndex: 'description', key: 'description', width: 300, ellipsis: true, }, { title: '优先级', dataIndex: 'priority', key: 'priority', width: 80, }, { title: '默认优先级', dataIndex: 'defaultPriority', key: 'defaultPriority', width: 100, }, { title: '预计时长(分钟)', dataIndex: 'expectedDuration', key: 'expectedDuration', width: 120, }, { title: '启用状态', dataIndex: 'isActive', key: 'isActive', width: 100, render: text => ( {text ? '启用' : '禁用'} ), }, { title: '操作', key: 'action', width: 150, render: (_, record) => ( handleDelete(record.categoryId)} okText="确认" cancelText="取消" > ), }, ]; return (
} > } onCancel={handleCancel} footer={null} width={600} >
); } export default TicketCategoryManagement;