import { DeleteOutlined, PlusOutlined } from '@ant-design/icons'; import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components'; import { Button, Col, Form, Input, Modal, Row, Select, Table, Tag, message } from 'antd'; import { useEffect, useRef, useState } from 'react'; import { Link } from '@umijs/max'; import { createStockCheck, listProducts, listStockChecks } from '@/services/api'; import type { Product, StockCheck } from '@/types/api'; const STATUS_MAP: Record = { 0: { text: '草稿', color: 'default' }, 1: { text: '进行中', color: 'processing' }, 2: { text: '已完成', color: 'success' }, }; interface DetailRow { key: number; productId?: string; actualQuantity?: string } let rowKey = 0; const ChecksPage: React.FC = () => { const columns: ProColumns[] = [ { title: '盘点单号', dataIndex: 'checkNo' }, { title: '盘点日期', dataIndex: 'checkDate', valueType: 'date', search: false }, { title: '盘点人', dataIndex: 'checker', search: false }, { title: '状态', dataIndex: 'status', search: false, render: (_, record) => { const s = STATUS_MAP[record.status] ?? STATUS_MAP[0]; return {s.text}; }, }, { title: '创建时间', dataIndex: 'createdAt', valueType: 'dateTime', search: false }, { title: '操作', valueType: 'option', render: (_, record) => [查看] }, ]; const actionRef = useRef(); const [form] = Form.useForm(); const [open, setOpen] = useState(false); const [submitting, setSubmitting] = useState(false); const [products, setProducts] = useState([]); const [details, setDetails] = useState([]); useEffect(() => { listProducts({ page: 1, pageSize: 500 }).then((res) => { if (res?.code === 0) setProducts(res.data?.list || []); }); }, []); const handleOpen = () => { setDetails([]); setOpen(true); }; const handleSubmit = async () => { let values: { checkDate: string; checker?: string; remark?: string }; try { values = await form.validateFields(); } catch { return; } setSubmitting(true); try { const res = await createStockCheck({ ...values, details: details .filter((r) => r.productId && r.actualQuantity) .map((r) => ({ productId: r.productId!, actualQuantity: r.actualQuantity! })), }); if (res?.code !== 0) { message.error(res?.msg || '创建失败'); return; } setOpen(false); actionRef.current?.reload(); message.success('盘点单已创建'); } catch { message.error('创建失败'); } finally { setSubmitting(false); } }; const addRow = () => setDetails((prev) => [...prev, { key: rowKey++ }]); const removeRow = (idx: number) => setDetails((prev) => prev.filter((_, i) => i !== idx)); const updateRow = (idx: number, field: keyof DetailRow, value: string) => setDetails((prev) => prev.map((r, i) => (i === idx ? { ...r, [field]: value } : r))); const today = new Date().toISOString().slice(0, 10); return ( <> actionRef={actionRef} headerTitle="库存盘点" rowKey="checkId" columns={columns} search={false} request={async (params) => { const res = await listStockChecks({ page: params.current, pageSize: params.pageSize }); return { data: res?.data?.list || [], total: res?.data?.total || 0, success: res?.code === 0 }; }} toolBarRender={() => [ , ]} /> setOpen(false)} confirmLoading={submitting} width={680} afterOpenChange={(o) => { if (!o) form.resetFields(); }} >
盘点明细(可选)
( updateRow(idx, 'actualQuantity', e.target.value)} /> ), }, { title: '', width: 40, render: (_, __, idx) => (