import { money, clock, readOrders, updateOrder, watch, STATUS } from './store.js' const $ = (id) => document.getElementById(id) const esc = (text) => String(text ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])) let tab = 'new' const FLOW = { new: [{ action: 'cooking', label: '接单', cls: 'primary' }], cooking: [ { action: 'served', label: '做好了', cls: 'ok' }, { action: 'new', label: '退回', cls: 'ghost' }, ], served: [{ action: 'closed', label: '结账', cls: 'ghost' }], closed: [], } function minutesAgo(ts) { const mins = Math.floor((Date.now() - ts) / 60000) return mins <= 0 ? '刚刚' : `${mins} 分钟前` } function paint() { const orders = readOrders() const counts = { new: 0, cooking: 0, served: 0, closed: 0 } for (const order of orders) counts[order.status] = (counts[order.status] ?? 0) + 1 $('n-new').textContent = String(counts.new ?? 0) $('n-cooking').textContent = String(counts.cooking ?? 0) $('n-served').textContent = String(counts.served ?? 0) $('n-all').textContent = String(orders.length) $('statTotal').textContent = String(orders.length) $('statOpen').textContent = String((counts.new ?? 0) + (counts.cooking ?? 0)) $('statMoney').textContent = money(orders.reduce((sum, order) => sum + order.payable, 0)) const list = tab === 'all' ? orders : orders.filter((order) => order.status === tab) $('list').innerHTML = list.length === 0 ? `

${tab === 'all' ? '今天还没有订单' : '这个状态下暂时没有单子'}

` : list.map((order) => { const status = STATUS[order.status] ?? STATUS.new const items = order.items.map((item) => { const spec = [Object.entries(item.spec).map(([k, v]) => `${k}:${v}`).join(' '), item.extras.join(' ')] .filter(Boolean).join(' · ') return `${esc(item.name)}${spec ? `(${esc(spec)})` : ''} ×${item.qty}` }).join('
') const actions = FLOW[order.status] ?? [] return `
${order.no} 号单 ${esc(order.table)} 桌
${status.text}
${items}
${order.note ? `
备注:${esc(order.note)}
` : ''} ${order.urgedAt ? `
⚠️ 顾客催单(${clock(order.urgedAt)})
` : ''}
${clock(order.createdAt)} · ${minutesAgo(order.createdAt)} · ${money(order.payable)}${order.utensils ? ` · 餐具 ${order.utensils}` : ''}
${actions.map((action) => ``).join('')}
` }).join('') } $('tabs').addEventListener('click', (event) => { const button = event.target.closest('[data-tab]') if (!button) return tab = button.dataset.tab document.querySelectorAll('.kb-tab').forEach((el) => el.classList.toggle('on', el === button)) paint() }) $('list').addEventListener('click', (event) => { const button = event.target.closest('[data-go]') if (!button) return const card = button.closest('[data-id]') updateOrder(card.dataset.id, { status: button.dataset.go, handledAt: Date.now() }) paint() }) watch(paint) setInterval(paint, 30000) paint()