import { useEffect, useState, useRef } from 'react' import { useNavigate, useParams, useSearchParams } from 'react-router-dom' import { useAllowed, useCan } from '@xenia/access' import OrderSummary from '../components/OrderSummary' import useConnectionStore from '../store/connectionStore' import client from '../api/client' import { apiError } from '../api/errors' import { money } from '../i18n' import { getVenueSettings } from '../api/venueSettings' import db from '../db/posdb' import { queueOfflineSplit, queueOfflinePayment as queueOfflinePaymentOp } from '../services/offlineOrders' import { TransferIcon, MergeIcon, FlagsIcon, WaiterIcon, PrintIcon } from '../components/Icons' import usePaymentSettingsStore from '../store/paymentSettingsStore' import useTableViewStore from '../store/tableViewStore' import { useTranslation } from '@xenia/i18n' const fmtPrice = money // ─── Merge mode ─────────────────────────────────────────────────────────────── const MERGE_OFF = 'off' const MERGE_IDENTICAL = 'identical' // same product + options + price const MERGE_SAME_ITEM = 'same_item' // same product + same effective unit price // Returns a stable string key for grouping under MERGE_IDENTICAL function identicalKey(item) { const effPrice = ((item.unit_price ?? 0) + (item.price_adjustment ?? 0)).toFixed(4) const opts = (() => { try { return item.selected_options ?? '[]' } catch { return '[]' } })() const removed = (() => { try { return item.removed_ingredients ?? '[]' } catch { return '[]' } })() const notes = item.notes ?? '' return `${item.product_id}|${effPrice}|${opts}|${removed}|${notes}` } // Returns a grouping key for MERGE_SAME_ITEM (ignores options/removed/notes, only product + price) function sameItemKey(item) { const effPrice = ((item.unit_price ?? 0) + (item.price_adjustment ?? 0)).toFixed(4) return `${item.product_id}|${effPrice}` } // Builds the display item list. When mode is OFF returns items unchanged. // Otherwise returns virtual merged items with _isMerged, _sourceIds, _sourceItems. function mergeItems(items, mode) { if (mode === MERGE_OFF) return items const keyFn = mode === MERGE_IDENTICAL ? identicalKey : sameItemKey const groups = new Map() items.forEach(item => { const k = keyFn(item) if (!groups.has(k)) { groups.set(k, { // Copy the first real item as template, quantity will be summed ...item, quantity: 0, _isMerged: true, _sourceIds: [], _sourceItems: [], }) } const g = groups.get(k) g.quantity += item.quantity g._sourceIds.push(item.id) g._sourceItems.push(item) }) return Array.from(groups.values()) } // ─── Split stepper modal ────────────────────────────────────────────────────── function SplitModal({ item, onConfirm, onClose }) { const { t } = useTranslation('waiter') const [splitQty, setSplitQty] = useState(1) const max = item.quantity - 1 return (
{item.product?.name} ×{item.quantity}
{t('detail.split.howMany')}
{item.product?.name}
{/* Current effective price — tap to type */}{label}
{actionRows.map((a, i) => ( ))}{t('detail.discount.rounding')}
{/* Item multi-select */}{t('detail.discount.items')}
{t('detail.discount.pct')}
{t('detail.discount.note')}
setNote(e.target.value)} placeholder={t('detail.discount.notePlaceholder')} style={{ width: '100%', padding: '9px 12px', borderRadius: 10, border: '1.5px solid var(--border)', background: 'var(--bg3)', color: 'var(--text)', fontSize: 13, boxSizing: 'border-box', }} />{error}
)}{subtitle}
}{t('common.loading')}
)} {!loading && tables.length === 0 && ({t('detail.noTables')}
)} {!loading && tables.map(tb => ( ))}{t('common.loading')}
)} {!loading && flagDefs.length === 0 && ({t('detail.noFlags')}
)} {!loading && flagDefs.map(f => { const sel = selected.includes(f.id) return ( ) })}{t('common.loading')}
)} {!loading && available.length === 0 && ({t('detail.allAssigned')}
)} {!loading && available.map(w => ( ))}{t('detail.pickPrinter')}
{itemCount === null ? t('detail.cancel.whole') : t('detail.cancel.items', { count: itemCount })}
{subtitle}
{fmtPrice(payTotal)}
{/* Cash / Card toggle */} {cardEnabled && ({t('common.sure')}
{fmtPrice(payTotal)} — {method === 'card' ? t('detail.pay.card') : t('detail.pay.cash')}
{t('detail.guests.question')}
{t('common.loading')}
{error}
} {/* Active flag cards */} {currentFlagIds.length > 0 && flagDefs.length > 0 && ({t('tables.quick.noOrder')}
{t('detail.noPaid')}
) : ( <>{t('detail.view.title')}
{[ { mode: MERGE_OFF, label: t('detail.view.off'), desc: t('detail.view.offDesc') }, { mode: MERGE_IDENTICAL, label: t('detail.view.identical'), desc: t('detail.view.identicalDesc') }, { mode: MERGE_SAME_ITEM, label: t('detail.view.sameItem'), desc: t('detail.view.sameItemDesc') }, ].map(({ mode, label, desc }) => { const active = mergeMode === mode return ( ) })}{revertConfirm.itemIds.length === 1 ? t('detail.revert.items', { count: 1 }) : t('detail.revert.items', { count: revertConfirm.itemIds.length })}
{t('detail.close.body')}
{pendingTable.mode === 'transfer' ? t('detail.move.transferTo') : pendingTable.mode === 'merge' ? t('detail.move.mergeWith') : t('detail.move.itemsTo', { count: selectedIds.length })}
{pendingTable.table.label || `T${pendingTable.table.number}`}