import { PlusOutlined } from '@ant-design/icons'; import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components'; import { Button, Form, Input, Modal, Space, Tag, message } from 'antd'; import { useRef, useState } from 'react'; import { createSupplier, deleteSupplier, listSuppliers, updateSupplier } from '@/services/api'; import type { Supplier } from '@/types/api'; const STATUS_MAP: Record = { 0: { text: '停用', color: 'default' }, 1: { text: '正常', color: 'success' }, }; const SuppliersPage: React.FC = () => { const actionRef = useRef(); const [form] = Form.useForm(); const [modalOpen, setModalOpen] = useState(false); const [editing, setEditing] = useState(null); const openCreate = () => { setEditing(null); form.resetFields(); setModalOpen(true); }; const openEdit = (record: Supplier) => { setEditing(record); form.setFieldsValue({ supplierName: record.supplierName, phone: record.phone, remark: record.remark }); setModalOpen(true); }; const handleSubmit = async () => { const values = await form.validateFields(); if (editing) { await updateSupplier(editing.supplierId, values); message.success('更新成功'); } else { await createSupplier(values); message.success('创建成功'); } setModalOpen(false); actionRef.current?.reload(); }; const handleDelete = async (id: string) => { await deleteSupplier(id); message.success('删除成功'); actionRef.current?.reload(); }; const handleToggleStatus = async (record: Supplier) => { await updateSupplier(record.supplierId, { status: record.status === 1 ? 0 : 1 }); message.success('状态已更新'); actionRef.current?.reload(); }; const columns: ProColumns[] = [ { title: '供应商名称', dataIndex: 'supplierName' }, { title: '联系电话', dataIndex: 'phone', search: false }, { title: '采购次数', dataIndex: 'purchaseCount', search: false }, { title: '累计入库数量', dataIndex: 'deliveredQty', search: false }, { title: '应结金额', dataIndex: 'payableAmount', search: false }, { title: '实结金额', dataIndex: 'paidAmount', search: false }, { title: '状态', dataIndex: 'status', search: false, render: (_, r) => { const s = STATUS_MAP[r.status] ?? STATUS_MAP[1]; return {s.text}; }, }, { title: '创建时间', dataIndex: 'createdAt', valueType: 'dateTime', search: false }, { title: '操作', valueType: 'option', render: (_, r) => [ openEdit(r)}>编辑, handleToggleStatus(r)}> {r.status === 1 ? '停用' : '启用'} , Modal.confirm({ title: '确认删除?', onOk: () => handleDelete(r.supplierId) })}>删除, ], }, ]; return ( <> actionRef={actionRef} headerTitle="供应商管理" rowKey="supplierId" columns={columns} request={async (params) => { const res = await listSuppliers({ page: params.current, pageSize: params.pageSize, supplierName: params.supplierName }); return { data: res?.data?.list || [], total: res?.data?.total || 0, success: res?.code === 0 }; }} toolBarRender={() => [ , ]} /> setModalOpen(false)} destroyOnClose >
); }; export default SuppliersPage;