'use client' import { useState, useEffect } from 'react'; import DeviceList from './DeviceList'; import DeviceForm from './DeviceForm'; import DistributionRecord from './DistributionRecord'; import { saveToLocalStorage, getFromLocalStorage, exportData } from '../utils/localStorage'; import Alert from './Alert'; export interface Device { id: string; name: string; quantity: number; serialNumber: string; } export interface Distribution { id: string; deviceId: string; recipient: string; startDate: string; endDate: string; quantity: number; } export default function Device() { const [devices, setDevices] = useState([]); const [distributions, setDistributions] = useState([]); const [alert, setAlert] = useState<{ message: string; type: 'success' | 'error' | 'warning' } | null>(null); useEffect(() => { const savedDevices = getFromLocalStorage('devices'); const savedDistributions = getFromLocalStorage('distributions'); if (savedDevices) setDevices(savedDevices); if (savedDistributions) setDistributions(savedDistributions); }, []); useEffect(() => { saveToLocalStorage('devices', devices); }, [devices]); useEffect(() => { saveToLocalStorage('distributions', distributions); }, [distributions]); const handleAddDevice = (device: Device) => { setDevices([...devices, device]); }; const handleUpdateDevice = (updatedDevice: Device) => { setDevices(devices.map(d => d.id === updatedDevice.id ? updatedDevice : d)); }; const handleDeleteDevice = (id: string) => { setDevices(devices.filter(d => d.id !== id)); setDistributions(distributions.filter(d => d.deviceId !== id)); showAlert('设备已成功删除', 'success'); }; const handleAddDistribution = (distribution: Distribution) => { const device = devices.find(d => d.id === distribution.deviceId); if (device && device.quantity >= distribution.quantity) { setDistributions([...distributions, distribution]); handleUpdateDevice({ ...device, quantity: device.quantity - distribution.quantity }); showAlert('设备分发成功', 'success'); } else { showAlert('设备数量不足,无法分发', 'error'); } }; const handleUpdateDistribution = (updatedDistribution: Distribution) => { const oldDistribution = distributions.find(d => d.id === updatedDistribution.id); if (oldDistribution) { const device = devices.find(d => d.id === updatedDistribution.deviceId); if (device) { const quantityDiff = oldDistribution.quantity - updatedDistribution.quantity; handleUpdateDevice({ ...device, quantity: device.quantity + quantityDiff }); } } setDistributions(distributions.map(d => d.id === updatedDistribution.id ? updatedDistribution : d)); }; const handleDeleteDistribution = (id: string) => { const distribution = distributions.find(d => d.id === id); if (distribution) { const device = devices.find(d => d.id === distribution.deviceId); if (device) { handleUpdateDevice({ ...device, quantity: device.quantity + distribution.quantity }); } } setDistributions(distributions.filter(d => d.id !== id)); }; const handleImport = (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (file) { const reader = new FileReader(); reader.onload = (e) => { const content = e.target?.result as string; const importedData = JSON.parse(content); if (importedData.devices) setDevices(importedData.devices); if (importedData.distributions) setDistributions(importedData.distributions); }; reader.readAsText(file); } }; const handleExport = () => { exportData({ devices, distributions }, 'inventory_data.json'); }; const showAlert = (message: string, type: 'success' | 'error' | 'warning') => { setAlert({ message, type }); }; return (

摄影社团库存管理系统

{alert && ( setAlert(null)} /> )}
); }