import { App, Alert, Button, Card, Empty, Modal, Space, Tag, Typography } from 'antd' import { useQuery } from '@tanstack/react-query' import { useEffect, useMemo, useState } from 'react' import PercentTable from '@/components/common/PercentTable' import { fetchAdminConsignmentMatchCandidates, matchAdminConsignmentWorkOrder, } from '@/services/admin/consignment' import type { ConsignmentMatchCandidate } from '@/types/consignment' function money(fen: number) { return `¥${((Number(fen || 0) || 0) / 100).toFixed(2)}` } export default function ConsignmentWorkOrderMatchModal({ open, workOrderId, onClose, onMatched, }: { open: boolean workOrderId: number | null onClose: () => void onMatched?: () => void }) { const { message } = App.useApp() const [selectedIds, setSelectedIds] = useState([]) const [submitting, setSubmitting] = useState(false) const candidatesQuery = useQuery({ queryKey: ['admin-consignment-match-candidates', workOrderId], queryFn: () => fetchAdminConsignmentMatchCandidates(Number(workOrderId)), enabled: open && Boolean(workOrderId), }) const data = candidatesQuery.data?.data const options = data?.options || [] const needed = Math.max(1, Number(data?.totalQuantity || 1) - Number(data?.activeQuantity || 0)) useEffect(() => { if (!open) { setSelectedIds([]) return } if (options.length === 0) return // 默认按缺口选中价格最低的几条,客服可自行调整。 setSelectedIds(options.slice(0, needed).map((item) => item.inventoryId)) }, [open, options, needed]) const totalPrice = useMemo( () => selectedIds.reduce((sum, id) => { const item = options.find((option) => option.inventoryId === id) return sum + Number(item?.salePriceFen || 0) }, 0), [options, selectedIds], ) return ( } > {candidatesQuery.isLoading ? : null} {!candidatesQuery.isLoading && options.length === 0 ? ( ) : null} {options.length > 0 ? ( rowKey="inventoryId" size="small" pagination={false} dataSource={options} rowSelection={{ selectedRowKeys: selectedIds, onChange: (keys) => setSelectedIds(keys.map((key) => Number(key))), }} columns={[ { title: '寄卖库存', render: (_, item) => ( {item.itemName} {item.inventoryNo} · {item.workerName || `打手#${item.workerId || '-'}`} ), }, { title: '规格', render: (_, item) => `${item.system}/${item.serverZone}/${item.series}/${item.diamondLevel}钻/${item.brillianceCount}璀`, }, { title: '库存', width: 90, render: (_, item) => ( 1 ? 'blue' : 'default'}> 剩 {item.remainingQuantity}/{item.quantity} ), }, { title: '售价', width: 110, render: (_, item) => money(item.salePriceFen), }, ]} /> 已选 {selectedIds.length} 条,合计成本 {money(totalPrice)} ) : null} ) }