import { useMemo, useState } from "react"; import { Alert, Badge, Button, Checkbox, Group, Modal, Stack, Text, } from "@mantine/core"; import { Landmark } from "lucide-react"; import type { EligibleContainerBooking, TrainScheduleDetail, } from "@/types/trainScheduling"; /** * Government-priority switch confirmation: pick the assigned commercial * bookings to take off the train so the government booking can have their * wagons. Mount with key={govBooking.id} so selection resets per booking. */ export function SwitchGovernmentBookingModal({ opened, onClose, govBooking, assignedBookings, loading, onConfirm, }: { opened: boolean; onClose: () => void; govBooking: EligibleContainerBooking | null; assignedBookings: TrainScheduleDetail["bookings"]; loading?: boolean; onConfirm: (removeBookingIds: string[]) => void; }) { const [selected, setSelected] = useState([]); const candidates = useMemo( () => assignedBookings.filter((b) => !b.isGovernment && b.wagonAssigned), [assignedBookings], ); const freedWagons = candidates .filter((b) => selected.includes(b.id)) .reduce((sum, b) => sum + (b.wagonsRequired ?? 0), 0); const toggle = (id: string) => setSelected((ids) => ids.includes(id) ? ids.filter((x) => x !== id) : [...ids, id], ); return ( Switch in government booking } radius="lg" size="lg" > Government bookings have priority. Select the booking(s) to switch out — together they must free at least as many wagons as{" "} {govBooking?.reference} {" "} needs. Switched-out customers are notified to rebook. {candidates.length ? ( {candidates.map((b) => ( toggle(b.id)} label={ {b.reference} {b.customer} } /> {b.weightTons}T {b.wagonsRequired ?? "?"} wagon{b.wagonsRequired === 1 ? "" : "s"} ))} ) : ( No commercial bookings with wagons on this train to switch out. )} Frees {freedWagons} wagon{freedWagons === 1 ? "" : "s"} ); }