import { useMemo, useState } from "react"; import { useLocation, useNavigate, useParams } from "react-router-dom"; import { CafeEvent, mockCafeEvents } from "../data/mockCafeEvents"; import { mockReservations, Reservation } from "../data/mockReservations"; import { formatPersianNumber } from "../utils/formatNumber"; function ChevronIcon({ className = "" }: { className?: string }) { return ( ); } function SearchIcon({ className = "" }: { className?: string }) { return ( ); } function CheckIcon() { return ( ); } function toEnglishDigits(value: string) { const persianDigits = "۰۱۲۳۴۵۶۷۸۹"; return value.replace(/[۰-۹]/g, (digit) => String(persianDigits.indexOf(digit)), ); } function reservationMatches(reservation: Reservation, query: string) { if (!query) return true; const haystack = toEnglishDigits( `${reservation.name} ${reservation.phone} ${reservation.count} ${formatPersianNumber(reservation.count)} ${reservation.code}`, ).toLowerCase(); return haystack.includes(toEnglishDigits(query).toLowerCase()); } function downloadReservationsCsv(reservations: Reservation[]) { const header = ["کد رزرو", "تعداد", "نام", "شماره تماس"]; const rows = reservations.map((r) => [ r.code, String(r.count), r.name, toEnglishDigits(r.phone), ]); const csvContent = [header, ...rows] .map((row) => row.map((cell) => `"${cell.replace(/"/g, '""')}"`).join(",")) .join("\n"); const blob = new Blob(["" + csvContent], { type: "text/csv;charset=utf-8;", }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = "reservations.csv"; link.click(); URL.revokeObjectURL(url); } function ReservationCard({ reservation, selected, onToggle, }: { reservation: Reservation; selected: boolean; onToggle: () => void; }) { return (
{reservation.code} {formatPersianNumber(reservation.count)} نفر

{reservation.name}

{reservation.phone}

); } export default function EventReservations() { const { id } = useParams(); const location = useLocation(); const navigate = useNavigate(); const [query, setQuery] = useState(""); const [selectedIds, setSelectedIds] = useState([]); const event: CafeEvent | undefined = (location.state as { event?: CafeEvent } | null)?.event ?? mockCafeEvents.find((e) => e.id === Number(id)); const filteredReservations = useMemo( () => mockReservations.filter((reservation) => reservationMatches(reservation, query), ), [query], ); const toggleSelected = (reservationId: number) => { setSelectedIds((prev) => prev.includes(reservationId) ? prev.filter((id) => id !== reservationId) : [...prev, reservationId], ); }; const handleDownload = () => { const selectedReservations = selectedIds.length > 0 ? mockReservations.filter((r) => selectedIds.includes(r.id)) : mockReservations; downloadReservationsCsv(selectedReservations); }; return (
setQuery(e.target.value)} placeholder="جستجو" className="w-full rounded-2xl shadow-[0px_4px_12px_0px_#0000001F] bg-white py-3 pr-6 pl-4 text-right placeholder:font-light placeholder:text-[#999999]" />
{filteredReservations.map((reservation) => ( toggleSelected(reservation.id)} /> ))}
); }