"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { Button } from "@/app/components/ui/button"; import { Label } from "@/app/components/ui/label"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/app/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/app/components/ui/select"; import { ErrorState } from "@/app/components/ui/states"; import { closeEvent, getEventCloseOut, startEventCloseOut } from "@/app/lib/services/adminStockCounts"; import { listAdminWarehouses } from "@/app/lib/services/adminWarehouses"; import { useT } from "@/app/lib/i18n/MessagesProvider"; import type { AdminEvent, AdminWarehouse, EventCloseOutState } from "@/app/lib/types"; // Event close-out (add-stock-count-event-closeout): after the sales window, with no open pickups, // count the event warehouse; once that count is approved, return every leftover to a warehouse and // close the event. A Closed event's warehouse accepts no further movement. The server enforces each // step; this dialog shows the current one. const message = (e: unknown) => (e instanceof Error ? e.message : String(e)); export function EventCloseOutDialog({ event, onClose, onClosed, }: { event: AdminEvent; onClose: () => void; onClosed: () => void; }) { const t = useT(); const router = useRouter(); const [state, setState] = useState(null); const [warehouses, setWarehouses] = useState([]); const [returnId, setReturnId] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); useEffect(() => { Promise.all([getEventCloseOut(event.id), listAdminWarehouses()]) .then(([s, ws]) => { setState(s); setWarehouses(ws); if (s.suggestedReturnWarehouseId !== null) setReturnId(String(s.suggestedReturnWarehouseId)); }) .catch(e => setError(message(e))); }, [event.id]); const returnOptions = warehouses.filter(w => w.status === "Y" && w.type !== "EVENT"); const codeOf = (id: number | null) => warehouses.find(w => w.id === id)?.code ?? String(id ?? ""); const start = async () => { setBusy(true); setError(null); try { const count = await startEventCloseOut(event.id); router.push(`/inventory/stock-counts/${count.id}`); } catch (e) { setError(message(e)); setBusy(false); } }; const close = async () => { setBusy(true); setError(null); try { await closeEvent(event.id, Number(returnId)); onClosed(); } catch (e) { setError(message(e)); setBusy(false); } }; const blocked = state !== null && (!state.windowsOver || state.openReservations.length > 0); return ( { if (!open) onClose(); }}> {t("EvtCloseOutTitle", { name: event.name })} {state?.phase === "NOT_STARTED" && (

{t("EvtCloseOutIntro")}

{!state.windowsOver &&

{t("EvtCloseOutSalesNotOver")}

} {state.openReservations.length > 0 && (

{t("EvtCloseOutOpenOrders")}

    {state.openReservations.map(b =>
  • {b.orderNo ?? t("EvtCloseOutUnpaidOrder", { ref: b.ref })}
  • )}
)}
)} {(state?.phase === "COUNTING" || state?.phase === "AWAITING_APPROVAL") && (

{state.phase === "COUNTING" ? t("EvtCloseOutCounting") : t("EvtCloseOutAwaitingApproval")}{" "} {t("EvtCloseOutOpenCount", { id: String(state.countId) })}

)} {state?.phase === "READY_TO_CLOSE" && (

{t("EvtCloseOutReady")}

)} {state?.phase === "CLOSED" && (

{t("EvtCloseOutClosed", { warehouse: codeOf(state.returnWarehouseId) })}

)} {error && } {state?.phase === "NOT_STARTED" && ( )} {state?.phase === "READY_TO_CLOSE" && ( )}
); }