'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { useRef } from 'react' import { ArrowLeft, Plus, Trash2, Upload, Info, CheckCircle, FileText, X, ScanLine } from 'lucide-react' /** 板卡类型 -> 可选版本 */ const versionsByType: Record = { '主协板': [ { version: 'MB-V1.2', firmware: 'v2.1' }, { version: 'MB-V2.1', firmware: 'v1.8' }, ], '采集板': [ { version: 'RX-V1.3', firmware: 'v3.0' }, { version: 'RX-V2.1', firmware: 'v2.5' }, ], '发射板': [ { version: 'TX-V1.5', firmware: 'v1.2' }, { version: 'TX-V2.1', firmware: 'v1.0' }, ], '升压板': [ { version: 'BO-V2.1', firmware: 'v1.1' }, { version: 'BO-V2.2', firmware: 'v0.9' }, ], '电缆头': [ { version: 'CBH-V1.0', firmware: '-' }, ], '电缆': [ { version: 'CBL-30M', firmware: '-' }, { version: 'CBL-60M', firmware: '-' }, { version: 'CBL-100M', firmware: '-' }, ], '机箱': [ { version: 'GD30-CASE-A', firmware: '-' }, { version: 'GD30-CASE-B', firmware: '-' }, { version: 'GM10-CASE-A', firmware: '-' }, ], '电源': [ { version: 'BP150-V1.0', firmware: '-' }, { version: 'BP300-V1.0', firmware: '-' }, { version: 'BP600-V1.0', firmware: '-' }, { version: 'BP600-V2.0', firmware: '-' }, ], } const typeOptions = Object.keys(versionsByType) interface BoardEntry { id: number type: string version: string firmware: string sn: string productionDate: string remark: string calibFile: File | null } let nextId = 1 export default function BoardRegisterPage() { const router = useRouter() const [entries, setEntries] = useState([createEntry()]) const [batchMode, setBatchMode] = useState(false) function createEntry(): BoardEntry { return { id: nextId++, type: '采集板', version: 'RX-V2.1', firmware: 'v2.1', sn: '', productionDate: '', remark: '', calibFile: null } } const addEntry = () => { setEntries(prev => [...prev, createEntry()]) } 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 = versionsByType[value] if (versions && versions.length > 0) { updated.version = versions[0].version updated.firmware = versions[0].firmware } // 切换到非采集板时清除校准文件 if (value !== '采集板') updated.calibFile = null } // 切换版本时自动填充固件 if (field === 'version') { const versions = versionsByType[updated.type] const match = versions?.find(m => m.version === value) if (match) updated.firmware = match.firmware } 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' }} />
{/* 备注 */}
updateEntry(entry.id, 'remark', e.target.value)} placeholder="可选" style={{ width: '100%', padding: '8px 12px', border: '1px solid #D9D9D9', borderRadius: 6, fontSize: 14, boxSizing: 'border-box' }} />
{/* 采集板校准提示 */} {entry.type === '采集板' && (
采集板登记后状态为"待校准",需完成校准后才能用于设备装配。
)} {/* 采集板校准文件导入 */} {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.firmware} {entry.sn || '未填写'} {entry.productionDate || '未填写'} {entry.type === '采集板' ? ( entry.calibFile ? ( {entry.calibFile.name} ) : ( 未导入 ) ) : ( - )} {entry.type === '采集板' ? '待校准' : '在库'}
)}
{/* Sticky Bottom Bar */}
共 {entries.length} 条物料待登记 {entries.some(e => e.type === '采集板') && · 含采集板需后续校准} {entries.some(e => e.type === '采集板' && e.calibFile) && · {entries.filter(e => e.calibFile).length} 个已附校准文件}
) }