import './styles.css' import { api, type PLC, type MP, type Station } from './api/client.js' /* ===== Navigation ===== */ const $ = (s: string) => document.querySelector(s)! const $$ = (s: string) => document.querySelectorAll(s) $$('.tab-btn').forEach(btn => btn.addEventListener('click', () => { $$('.tab-btn').forEach(b => b.classList.remove('active')) $$('.page').forEach(p => p.classList.remove('active')) btn.classList.add('active') document.getElementById(`page-${btn.dataset.page}`)!.classList.add('active') })) /* ===== Query Page ===== */ const qInput = $('#q-input') as HTMLInputElement const qBtn = $('#q-btn') as HTMLButtonElement const qResult = $('#q-result') as HTMLDivElement async function doQuery() { const pid = qInput.value.trim() if (!pid) return qResult.innerHTML = '
查询中...
' try { const r = await api.queryProduct(pid) qResult.innerHTML = '' if (!r.data.length) { qResult.innerHTML = `
${r.message || '暂无数据'}
` return } for (const d of r.data) { const ok = d.isQualified === 1, rw = d.inspectionCount > 1 qResult.innerHTML += `
${d.pointName} ${d.fieldUnit || ''} ${d.recordTime}
检测次数
${d.inspectionCount}${rw ? '(返修)' : ''}
合格判定
${ok ? '合格' : '不合格'}
实测值
${d.measuredValue ?? '-'}
上限
${d.upperLimit ?? '-'}
下限
${d.lowerLimit ?? '-'}
` } } catch (e: any) { qResult.innerHTML = `
${e.message}
` } } qBtn.addEventListener('click', doQuery) qInput.addEventListener('keydown', e => { if (e.key === 'Enter') doQuery() }) /* ===== Config Page ===== */ let configLoaded = false let plcTabLoaded = false let activeSubTab: 'mp' | 'plc' = 'mp' async function loadConfig() { configLoaded = true plcTabLoaded = false activeSubTab = 'mp' const el = document.getElementById('page-config')! el.innerHTML = `
` document.getElementById('sub-tab-mp')!.addEventListener('click', () => switchTab('mp')) document.getElementById('sub-tab-plc')!.addEventListener('click', () => switchTab('plc')) const [plcs, mps, stations] = await Promise.all([api.getPLCs(), api.getMPs(), api.getStations()]) renderMPTable(mps) if (activeSubTab === 'plc') { renderPLCTable(plcs, stations, mps) switchTab('plc') } } function switchTab(tab: 'mp' | 'plc') { activeSubTab = tab document.querySelectorAll('.sub-tab').forEach(b => { b.classList.remove('border-indigo-600', 'text-indigo-700') b.classList.add('border-transparent', 'text-gray-500') }) const btn = document.getElementById(`sub-tab-${tab}`)! btn.classList.remove('border-transparent', 'text-gray-500') btn.classList.add('border-indigo-600', 'text-indigo-700') document.getElementById('mp-panel')!.classList.toggle('hidden', tab !== 'mp') document.getElementById('plc-panel')!.classList.toggle('hidden', tab !== 'plc') if (tab === 'plc' && !plcTabLoaded) { plcTabLoaded = true Promise.all([api.getPLCs(), api.getMPs(), api.getStations()]).then(([plcs, mps, stations]) => { renderPLCTable(plcs, stations, mps) }) } } function renderMPTable(mps: MP[]) { const el = document.getElementById('mp-panel')! el.innerHTML = `
${mps.map(m => ``).join('')}
名称单位
` document.querySelectorAll('[data-mp-save]').forEach(b => b.addEventListener('click', () => { const id = Number((b as HTMLElement).dataset.mpSave) api.updateMP(id, { PointName: (document.querySelector(`[data-mp-name="${id}"]`) as HTMLInputElement).value, FieldUnit: (document.querySelector(`[data-mp-unit="${id}"]`) as HTMLInputElement).value, }).then(() => loadConfig()) })) document.querySelectorAll('[data-mp-del]').forEach(b => b.addEventListener('click', () => { if (!confirm('删除检测点会同时删除关联的工序,确定?')) return api.delMP(Number((b as HTMLElement).dataset.mpDel)).then(() => loadConfig()) })) document.getElementById('mp-add')?.addEventListener('click', () => { const name = (document.getElementById('mp-new-name') as HTMLInputElement).value if (!name) return api.addMP({ PointName: name, FieldUnit: (document.getElementById('mp-new-unit') as HTMLInputElement).value, }).then(() => loadConfig()) }) } function renderPLCTable(plcs: PLC[], stations: Station[], mps: MP[]) { const el = document.getElementById('plc-panel')! el.innerHTML = `
${plcs.map(p => ``).join('')}
名称IPDB
工序: ${stations.filter(s => s.PLCId === p.Id).map(s => `${s.StationName} `).join('')}
` document.querySelectorAll('[data-plc-save]').forEach(b => b.addEventListener('click', () => { const id = Number((b as HTMLElement).dataset.plcSave) api.updatePLC(id, { PLCName: (document.querySelector(`[data-plc-name="${id}"]`) as HTMLInputElement).value, IPAddress: (document.querySelector(`[data-plc-ip="${id}"]`) as HTMLInputElement).value, DBNumber: Number((document.querySelector(`[data-plc-db="${id}"]`) as HTMLInputElement).value), }).then(() => loadConfig()) })) document.querySelectorAll('[data-plc-del]').forEach(b => b.addEventListener('click', () => { if (!confirm('删除PLC会同时删除其工序,确定?')) return api.delPLC(Number((b as HTMLElement).dataset.plcDel)).then(() => loadConfig()) })) document.querySelectorAll('[data-sta-del]').forEach(b => b.addEventListener('click', () => { api.delStation(Number((b as HTMLElement).dataset.staDel)).then(() => loadConfig()) })) document.querySelectorAll('[data-sta-add]').forEach(b => b.addEventListener('click', async () => { const plcId = Number((b as HTMLElement).dataset.staAdd) const sel = document.querySelector(`[data-sta-point="${plcId}"]`) as HTMLSelectElement const pointId = Number(sel.value) if (!pointId) return const pointName = sel.options[sel.selectedIndex]?.text || '' api.addStation({ PLCId: plcId, StationName: pointName, PointId: pointId, StationIndex: 0, SortOrder: 0 }).then(() => loadConfig()) })) document.getElementById('plc-add')?.addEventListener('click', () => { const name = (document.getElementById('plc-new-name') as HTMLInputElement).value if (!name) return api.addPLC({ PLCName: name, IPAddress: (document.getElementById('plc-new-ip') as HTMLInputElement).value, DBNumber: Number((document.getElementById('plc-new-db') as HTMLInputElement).value), }).then(() => loadConfig()) }) } document.querySelector('[data-page="config"]')?.addEventListener('click', () => { if (!configLoaded) loadConfig() })