'use client' import { useState, useRef } from 'react' import { useRouter } from 'next/navigation' import { ArrowLeft, Plus, Trash2, Upload, Info, CheckCircle, FileText, X, ScanLine } from 'lucide-react' import { useApi } from '@/lib/hooks' interface CategoryItem { id: number; name: string; status: string; has_calibration: number } interface VersionItem { id: number; type: string; version: string; status: string } interface BoardEntry { id: number type: string version: string sn: string productionDate: string status: string calibStatus: string calibFile: File | null } let nextId = 1 export default function BoardRegisterPage() { const router = useRouter() const { data: categoriesData } = useApi('/api/material-categories', []) const { data: versionsData } = useApi('/api/material-versions', []) const typeOptions = categoriesData.filter(c => c.status === '启用').map(c => c.name) // 根据分类名获取该分类下的版本列表 const getVersionsForType = (type: string) => versionsData.filter(v => v.type === type && v.status === '在产') // 判断分类是否需要校准 const needsCalibration = (type: string) => !!categoriesData.find(c => c.name === type)?.has_calibration function createEntry(): BoardEntry { const defaultType = typeOptions[0] || '' const versions = getVersionsForType(defaultType) return { id: nextId++, type: defaultType, version: versions[0]?.version || '', sn: '', productionDate: '', status: '在库', calibStatus: '-', calibFile: null } } const [entries, setEntries] = useState([createEntry()]) const [batchMode, setBatchMode] = useState(false) const addEntry = () => { const defaultType = typeOptions[0] || '' const versions = getVersionsForType(defaultType) setEntries(prev => [...prev, { id: nextId++, type: defaultType, version: versions[0]?.version || '', sn: '', productionDate: '', status: '在库', calibStatus: '-', calibFile: null }]) } const removeEntry = (id: number) => { if (entries.length <= 1) return setEntries(prev => prev.filter(e => e.id !== id)) } const updateEntry = (id: number, field: keyof BoardEntry, value: string) => { setEntries(prev => prev.map(e => { if (e.id !== id) return e const updated = { ...e, [field]: value } // 切换类型时自动选第一个版本 if (field === 'type') { const versions = getVersionsForType(value) updated.version = versions[0]?.version || '' if (needsCalibration(value)) { updated.calibStatus = '待校准' } else { updated.calibStatus = '-'; updated.calibFile = null } } return updated })) } const fileInputRefs = useRef>({}) const handleCalibFileChange = (entryId: number, file: File | null) => { setEntries(prev => prev.map(e => e.id === entryId ? { ...e, calibFile: file } : e)) } const formatFileSize = (bytes: number): string => { if (bytes < 1024) return bytes + ' B' if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB' return (bytes / (1024 * 1024)).toFixed(1) + ' MB' } const isValid = entries.every(e => e.sn.trim() && e.productionDate) return (
{/* Header */}

登记物料

登记新物料信息,支持单个或批量登记

{/* Info Banner */}
物料SN号为唯一标识,请确保录入正确。采集板登记后需要进行校准才能用于装配。选择版本后固件版本会自动填充。
{/* Mode Toggle */}
登记模式:
{batchMode && ( 当前 {entries.length} 条 )}
{/* Entry Cards */} {entries.map((entry, idx) => (

{batchMode ? `物料 #${idx + 1}` : '物料信息'}

{batchMode && entries.length > 1 && ( )}
{/* 板卡类型 */}
{/* 板卡版本 */}
{/* 物料SN */}
updateEntry(entry.id, 'sn', e.target.value)} placeholder={`扫码或手动输入 如 ${entry.version}-20250401001`} style={{ flex: 1, padding: '8px 12px', border: '1px solid #D9D9D9', borderRadius: 6, fontSize: 14, boxSizing: 'border-box' }} />
{/* 生产日期 */}
updateEntry(entry.id, 'productionDate', e.target.value)} style={{ width: '100%', padding: '8px 12px', border: '1px solid #D9D9D9', borderRadius: 6, fontSize: 14, boxSizing: 'border-box' }} />
{/* 物料状态 */}
{/* 校准状态(仅需校准的分类显示) */} {needsCalibration(entry.type) && (
)}
{/* 需校准物料提示 */} {needsCalibration(entry.type) && (
该物料登记后状态为"待校准",需完成校准后才能用于设备装配。
)} {/* 校准文件导入(需校准的分类显示) */} {needsCalibration(entry.type) && (
{ fileInputRefs.current[entry.id] = el }} onChange={e => handleCalibFileChange(entry.id, e.target.files?.[0] || null)} style={{ display: 'none' }} /> {entry.calibFile ? (
{entry.calibFile.name}
{formatFileSize(entry.calibFile.size)}
) : ( )}
)}
))} {/* Add More Button (batch mode) */} {batchMode && ( )} {/* Preview Summary */} {entries.length > 0 && (

登记预览

{['序号', '物料分类', '物料版本', '物料SN号', '生产日期', '物料状态', '校准状态', '校准文件'].map(h => ( ))} {entries.map((entry, i) => ( ))}
{h}
{i + 1} {entry.type} {entry.version} {entry.sn || '未填写'} {entry.productionDate || '未填写'} {entry.status} {needsCalibration(entry.type) ? entry.calibStatus : -} {needsCalibration(entry.type) ? ( entry.calibFile ? ( {entry.calibFile.name} ) : ( 待上传 ) ) : ( - )}
)}
{/* Sticky Bottom Bar */}
共 {entries.length} 条物料待登记 {entries.some(e => needsCalibration(e.type)) && · 含需校准物料} {entries.some(e => needsCalibration(e.type) && e.calibFile) && · {entries.filter(e => e.calibFile).length} 个已附校准文件}
) }