import React, { useState, useEffect } from 'react'; import { Table, Button, Modal, Form, Input, message, Select } from 'antd'; import { PlusOutlined, EditOutlined, DeleteOutlined, EyeOutlined } from '@ant-design/icons'; import { supplierService } from '../services/supplier.service'; import type { Supplier } from '../services/supplier.service'; const { Option } = Select; const SupplierManagement: React.FC = () => { const [suppliers, setSuppliers] = useState([]); const [loading, setLoading] = useState(false); const [modalVisible, setModalVisible] = useState(false); const [modalType, setModalType] = useState<'create' | 'edit'>('create'); const [currentSupplier, setCurrentSupplier] = useState(null); const [form] = Form.useForm(); const fetchSuppliers = async () => { setLoading(true); try { const data = await supplierService.getSuppliers(); setSuppliers(data); } catch (error) { message.error('获取供应商列表失败'); } finally { setLoading(false); } }; useEffect(() => { fetchSuppliers(); }, []); const handleCreate = () => { setModalType('create'); setCurrentSupplier(null); form.resetFields(); setModalVisible(true); }; const handleEdit = (supplier: Supplier) => { setModalType('edit'); setCurrentSupplier(supplier); form.setFieldsValue(supplier); setModalVisible(true); }; const handleDelete = async (id: string) => { try { await supplierService.deleteSupplier(id); message.success('删除成功'); fetchSuppliers(); } catch (error) { message.error('删除失败'); } }; const handleSubmit = async () => { try { const values = await form.validateFields(); if (modalType === 'create') { await supplierService.createSupplier(values); message.success('创建成功'); } else if (modalType === 'edit' && currentSupplier) { await supplierService.updateSupplier(currentSupplier.id, values); message.success('更新成功'); } setModalVisible(false); fetchSuppliers(); } catch (error) { message.error('操作失败'); } }; const columns = [ { title: '供应商名称', dataIndex: 'name', key: 'name', }, { title: '联系人', dataIndex: 'contact_person', key: 'contact_person', }, { title: '电话', dataIndex: 'phone', key: 'phone', }, { title: '邮箱', dataIndex: 'email', key: 'email', }, { title: '地址', dataIndex: 'address', key: 'address', ellipsis: true, }, { title: '类型', dataIndex: 'type', key: 'type', }, { title: '创建时间', dataIndex: 'created_at', key: 'created_at', }, { title: '操作', key: 'action', render: (_: any, record: Supplier) => (
), }, ]; return (

供应商管理

setModalVisible(false)} >
); }; export default SupplierManagement;