import { useEffect, useMemo, useState } from "react"; import PageMeta from "../../components/common/PageMeta"; import { apiGet } from "../../lib/api"; type DisplayItem = { nama: string; qty: number; harga: number; total: number; }; type DisplayState = { waktu: string; lokasi?: string | null; items: DisplayItem[]; subtotal: number; diskon: number; pajak: number; total: number; bayar: number; kembalian: number; metode: string; }; export default function CustomerDisplay() { const [storeName, setStoreName] = useState("POS"); const [storeLogo, setStoreLogo] = useState(""); const [state, setState] = useState(null); useEffect(() => { apiGet<{ data: { toko?: { nama?: string; logo?: string } } }>( "/admin-api/pengaturan" ) .then((res) => { const name = res?.data?.toko?.nama?.trim(); const logo = res?.data?.toko?.logo || ""; if (name) setStoreName(name); setStoreLogo(logo); }) .catch(() => {}); }, []); useEffect(() => { const raw = localStorage.getItem("pos-display-state"); if (raw) { try { setState(JSON.parse(raw)); } catch {} } let channel: BroadcastChannel | null = null; if ("BroadcastChannel" in window) { channel = new BroadcastChannel("pos-customer-display"); channel.onmessage = (event) => { setState(event.data); }; } const onStorage = (event: StorageEvent) => { if (event.key !== "pos-display-state" || !event.newValue) return; try { setState(JSON.parse(event.newValue)); } catch {} }; window.addEventListener("storage", onStorage); return () => { window.removeEventListener("storage", onStorage); channel?.close(); }; }, []); const items = state?.items || []; const isEmpty = items.length === 0; const metodeLabel = (value?: string) => { if (!value) return "-"; if (value === "CASH") return "TUNAI"; if (value === "VOUCHER_CASH") return "VOUCHER + TUNAI"; return value; }; const totalItems = useMemo( () => items.reduce((sum, i) => sum + i.qty, 0), [items] ); return ( <>
{storeLogo ? ( Logo ) : (
{storeName.trim().charAt(0) || "P"}
)}
{storeName}
{state?.lokasi ? `Lokasi: ${state.lokasi}` : "Kasir"}
{state?.waktu ? new Date(state.waktu).toLocaleString("id-ID") : new Date().toLocaleString("id-ID")}
Daftar Belanja
{totalItems} item
{isEmpty ? (
Silakan berbelanja
) : (
{items.map((i, idx) => ( ))}
Produk Qty Harga Total
{i.nama} {i.qty} Rp {i.harga.toLocaleString("id-ID")} Rp {i.total.toLocaleString("id-ID")}
)}
Total Bayar
Rp {((isEmpty ? 0 : state?.total) || 0).toLocaleString("id-ID")}
Metode: {metodeLabel(state?.metode)}
Subtotal Rp {((isEmpty ? 0 : state?.subtotal) || 0).toLocaleString("id-ID")}
Diskon Rp {((isEmpty ? 0 : state?.diskon) || 0).toLocaleString("id-ID")}
Pajak Rp {((isEmpty ? 0 : state?.pajak) || 0).toLocaleString("id-ID")}
Bayar Rp {((isEmpty ? 0 : state?.bayar) || 0).toLocaleString("id-ID")}
Kembalian Rp {((isEmpty ? 0 : state?.kembalian) || 0).toLocaleString("id-ID")}
Terima kasih telah berbelanja
); }