'use client' import { useEffect, useMemo, useState } from 'react' import Link from 'next/link' import { formatCurrency } from '@rentaldrivego/types' import { apiFetch } from '@/lib/api' import { useDashboardI18n } from '@/components/I18nProvider' type ReservationRow = { id: string contractNumber: string | null invoiceNumber: string | null status: string paymentStatus: string startDate: string endDate: string totalAmount: number customer: { firstName: string; lastName: string; email: string } vehicle: { make: string; model: string; licensePlate: string } } export default function ContractsPage() { const { language } = useDashboardI18n() const [rows, setRows] = useState([]) const [search, setSearch] = useState('') const [error, setError] = useState(null) const copy = { en: { heading: 'Contracts', subtitle: 'Generate or reopen rental contracts from any booking.', search: 'Search customer, vehicle, plate, contract number…', booking: 'Booking', customer: 'Customer', vehicle: 'Vehicle', dates: 'Dates', contract: 'Contract', invoice: 'Invoice', total: 'Total', status: 'Status', action: 'Action', open: 'Open contract', generate: 'Generate contract', empty: 'No bookings found.', }, fr: { heading: 'Contrats', subtitle: 'Générez ou rouvrez un contrat de location depuis n’importe quelle réservation.', search: 'Rechercher client, véhicule, plaque, numéro de contrat…', booking: 'Réservation', customer: 'Client', vehicle: 'Véhicule', dates: 'Dates', contract: 'Contrat', invoice: 'Facture', total: 'Total', status: 'Statut', action: 'Action', open: 'Ouvrir le contrat', generate: 'Générer le contrat', empty: 'Aucune réservation trouvée.', }, ar: { heading: 'العقود', subtitle: 'أنشئ أو افتح عقد التأجير من أي حجز.', search: 'ابحث بالعميل أو السيارة أو اللوحة أو رقم العقد…', booking: 'الحجز', customer: 'العميل', vehicle: 'المركبة', dates: 'التواريخ', contract: 'العقد', invoice: 'الفاتورة', total: 'الإجمالي', status: 'الحالة', action: 'الإجراء', open: 'فتح العقد', generate: 'إنشاء العقد', empty: 'لم يتم العثور على حجوزات.', }, }[language] useEffect(() => { apiFetch('/reservations?pageSize=100') .then((data) => setRows(data ?? [])) .catch((err) => setError(err.message ?? 'Failed to load contracts')) }, []) const filteredRows = useMemo(() => { const q = search.trim().toLowerCase() if (!q) return rows return rows.filter((row) => `${row.customer.firstName} ${row.customer.lastName}`.toLowerCase().includes(q) || row.customer.email.toLowerCase().includes(q) || `${row.vehicle.make} ${row.vehicle.model}`.toLowerCase().includes(q) || row.vehicle.licensePlate.toLowerCase().includes(q) || (row.contractNumber ?? '').toLowerCase().includes(q) || (row.invoiceNumber ?? '').toLowerCase().includes(q), ) }, [rows, search]) const localeCode = language === 'fr' ? 'fr-FR' : language === 'ar' ? 'ar-MA' : 'en-US' const formatDate = (iso: string) => new Date(iso).toLocaleString(localeCode, { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit', }) return (

{copy.heading}

{copy.subtitle}

setSearch(event.target.value)} placeholder={copy.search} className="input-field w-full lg:max-w-md" />
{error ? (
{error}
) : (
{filteredRows.map((row) => ( ))} {filteredRows.length === 0 && ( )}
{copy.customer} {copy.vehicle} {copy.dates} {copy.contract} {copy.invoice} {copy.status} {copy.total} {copy.action}

{row.customer.firstName} {row.customer.lastName}

{row.customer.email}

{row.vehicle.make} {row.vehicle.model}

{row.vehicle.licensePlate}

{formatDate(row.startDate)}

{formatDate(row.endDate)}

{row.contractNumber ?? '—'} {row.invoiceNumber ?? '—'}
{row.status} {row.paymentStatus}
{formatCurrency(row.totalAmount, 'MAD')} {row.contractNumber ? copy.open : copy.generate}
{copy.empty}
)}
) }