import { useMemo, useState } from "react"; import { Badge, Button, Group, Modal, Paper, Select, Stack, Table, Text, ThemeIcon, } from "@mantine/core"; import { CheckCircle2, Layers, Lock, LockOpen, PlayCircle, Repeat, XCircle } from "lucide-react"; import { BookingStatusBadge } from "@/components/bookings/BookingStatusBadge"; import { useBatchActions, useBookableSchedules, } from "@/hooks/trainScheduling/useTrainScheduling"; import { useToast } from "@/hooks/use-toast"; import type { TrainScheduleDetail } from "@/types/trainScheduling"; interface ScheduleBatchPanelProps { schedule: TrainScheduleDetail; } const windowColor: Record = { OPEN: "green", FULL: "orange", CLOSED: "gray", }; export function ScheduleBatchPanel({ schedule }: ScheduleBatchPanelProps) { const { toast } = useToast(); const actions = useBatchActions(schedule.id); const windowStatus = (schedule as { bookingWindowStatus?: string }).bookingWindowStatus ?? "OPEN"; const locked = schedule.status === "DISPATCHED" || schedule.status === "ARRIVED"; const [moveBookingId, setMoveBookingId] = useState(null); const [moveTarget, setMoveTarget] = useState(null); const { data: targets } = useBookableSchedules( schedule.originStation?.id, schedule.destinationStation?.id, ); const moveOptions = useMemo( () => (targets ?? []) .filter((s) => s.id !== schedule.id) .map((s) => ({ value: s.id, label: `${s.routeName ?? `${s.origin} → ${s.destination}`} · ${new Date( s.scheduleDate, ).toLocaleString()} · ${s.remainingWagons}/${s.maxWagons} free`, })), [targets, schedule.id], ); const bookings = schedule.bookings ?? []; const run = (fn: Promise, ok: string) => fn .then(() => toast({ title: ok })) .catch(() => toast({ title: "Action failed", variant: "destructive" })); return (
Batch allocation {bookings.length} allocated · {schedule.trainSet?.wagonCount ?? 0} wagons used
Window: {windowStatus}
{!locked && ( {windowStatus === "CLOSED" ? ( ) : ( )} )} {bookings.length === 0 ? ( No bookings allocated yet. The batch cron fills this schedule by priority; paid bookings are assigned automatically. ) : ( Booking Customer Status Actions {bookings.map((b) => ( {b.reference ?? b.id.slice(0, 8)} {b.customer ?? "—"} {!locked && ( {b.status !== "PAID" && ( )} )} ))}
)} setMoveBookingId(null)} title="Move booking to another schedule" centered radius="lg" >