import { useState } from 'react'; import { Device, Distribution } from './InventoryManagement'; interface DistributionRecordProps { devices: Device[]; distributions: Distribution[]; onAddDistribution: (distribution: Distribution) => void; onUpdateDistribution: (distribution: Distribution) => void; onDeleteDistribution: (id: string) => void; showAlert: (message: string, type: 'success' | 'error' | 'warning') => void; } export default function DistributionRecord({ devices, distributions, onAddDistribution, onUpdateDistribution, onDeleteDistribution, showAlert, }: DistributionRecordProps) { const [deviceId, setDeviceId] = useState(''); const [recipient, setRecipient] = useState(''); const [startDate, setStartDate] = useState(''); const [endDate, setEndDate] = useState(''); const [quantity, setQuantity] = useState(''); const [editingId, setEditingId] = useState(null); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const newDistribution: Distribution = { id: Date.now().toString(), deviceId, recipient, startDate, endDate, quantity: parseInt(quantity), }; if (isDeviceAvailable(deviceId, parseInt(quantity))) { onAddDistribution(newDistribution); resetForm(); showAlert('设备分发成功', 'success'); } else { showAlert('设备数量不足,无法分发', 'error'); } }; const resetForm = () => { setDeviceId(''); setRecipient(''); setStartDate(''); setEndDate(''); setQuantity(''); }; const handleEdit = (distribution: Distribution) => { setEditingId(distribution.id); setDeviceId(distribution.deviceId); setRecipient(distribution.recipient); setStartDate(distribution.startDate); setEndDate(distribution.endDate); setQuantity(distribution.quantity.toString()); }; const handleSave = () => { if (editingId) { const updatedDistribution: Distribution = { id: editingId, deviceId, recipient, startDate, endDate, quantity: parseInt(quantity), }; onUpdateDistribution(updatedDistribution); setEditingId(null); resetForm(); } }; const handleDelete = (id: string) => { if (window.confirm('确定要删除这条分发记录吗?')) { onDeleteDistribution(id); showAlert('分发记录已成功删除', 'success'); } }; const isDeviceAvailable = (deviceId: string, quantity: number) => { const device = devices.find(d => d.id === deviceId); return device ? device.quantity >= quantity : false; }; return (

分发记录

setRecipient(e.target.value)} placeholder="领用人" required className="border rounded p-2 w-full" /> setStartDate(e.target.value)} required className="border rounded p-2 w-full" /> setEndDate(e.target.value)} required className="border rounded p-2 w-full" /> setQuantity(e.target.value)} placeholder="数量" required className="border rounded p-2 w-full" />
{distributions.map(distribution => ( ))}
设备 领用人 开始日期 结束日期 数量 操作
{editingId === distribution.id ? ( ) : ( devices.find(d => d.id === distribution.deviceId)?.name )} {editingId === distribution.id ? ( setRecipient(e.target.value)} className="border rounded p-1 w-full" /> ) : ( distribution.recipient )} {editingId === distribution.id ? ( setStartDate(e.target.value)} className="border rounded p-1 w-full" /> ) : ( distribution.startDate )} {editingId === distribution.id ? ( setEndDate(e.target.value)} className="border rounded p-1 w-full" /> ) : ( distribution.endDate )} {editingId === distribution.id ? ( setQuantity(e.target.value)} className="border rounded p-1 w-20" /> ) : ( distribution.quantity )} {editingId === distribution.id ? ( ) : ( )}
); }