Files
dine-order/js/kitchen.js
T
2026-11-17 12:29:33 +08:00

85 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { money, clock, readOrders, updateOrder, watch, STATUS } from './store.js'
const $ = (id) => document.getElementById(id)
const esc = (text) => String(text ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[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
? `<p class="empty">${tab === 'all' ? '今天还没有订单' : '这个状态下暂时没有单子'}</p>`
: 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 ? `<small style="color:#7c6b5b">(${esc(spec)})</small>` : ''} <span class="q">×${item.qty}</span>`
}).join('<br>')
const actions = FLOW[order.status] ?? []
return `
<article class="order-card${order.urgedAt && order.urgedAt > (order.handledAt ?? 0) ? ' urged' : ''}" data-id="${order.id}">
<div class="oc-head">
<div><b>${order.no} 号单</b> <span class="oc-table">${esc(order.table)} 桌</span></div>
<span class="pill ${status.tone}">${status.text}</span>
</div>
<div class="oc-items">${items}</div>
${order.note ? `<div class="oc-note">备注:${esc(order.note)}</div>` : ''}
${order.urgedAt ? `<div class="oc-note">⚠️ 顾客催单(${clock(order.urgedAt)})</div>` : ''}
<div class="oc-foot">
<span class="oc-time">${clock(order.createdAt)} · ${minutesAgo(order.createdAt)} · ${money(order.payable)}${order.utensils ? ` · 餐具 ${order.utensils}` : ''}</span>
<div class="oc-actions">
${actions.map((action) => `<button class="btn-sm ${action.cls}" type="button" data-go="${action.action}">${action.label}</button>`).join('')}
</div>
</div>
</article>`
}).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()