"use client"; // Components/Car/CarDetailPanel.tsx // Slide-in panel showing full car details with action buttons. import { useEffect, useState } from "react"; import { Spinner } from "../UI"; import { CarImageGallery } from "./CarImageGallery"; import { carService } from "../../services/car.service"; import { getStoredToken } from "../../lib/auth"; import type { Car, CarStatus, InsuranceStatus } from "../../types/car"; // ── Status config ───────────────────────────────────────────────────────────── const STATUS_MAP: Record = { Active: { label: "نشط", color: "#166534", bg: "#DCFCE7", border: "#BBF7D0" }, InMaintenance: { label: "صيانة", color: "#854D0E", bg: "#FFFBEB", border: "#FDE68A" }, InTrip: { label: "في رحلة", color: "#1E40AF", bg: "#EFF6FF", border: "#BFDBFE" }, Inactive: { label: "غير نشط", color: "#64748B", bg: "#F1F5F9", border: "#E2E8F0" }, }; const INS_MAP: Record = { Valid: { label: "سارٍ", color: "#16A34A" }, Expired: { label: "منتهي", color: "#DC2626" }, NotInsured: { label: "غير مؤمَّن", color: "#D97706" }, }; // ── Helper: format date ─────────────────────────────────────────────────────── function fmtDate(iso?: string | null): string { if (!iso) return "—"; return new Date(iso).toLocaleDateString("ar-SA", { year: "numeric", month: "long", day: "numeric", }); } /** Returns true if the date is within 90 days in the future (or already past) */ function isExpiringSoon(iso?: string | null): boolean { if (!iso) return false; const diff = new Date(iso).getTime() - Date.now(); return diff <= 90 * 86_400_000; } // ── Props ───────────────────────────────────────────────────────────────────── interface CarDetailPanelProps { carId: string; onClose: () => void; onEdit: (car: Car) => void; onDelete: (car: Car) => void; } // ── Row component ───────────────────────────────────────────────────────────── function DetailRow({ label, value, mono = false, warn = false }: { label: string; value: string; mono?: boolean; warn?: boolean }) { return (
{label} {warn && value !== "—" ? "⚠ " : ""}{value}
); } // ── Component ───────────────────────────────────────────────────────────────── export function CarDetailPanel({ carId, onClose, onEdit, onDelete }: CarDetailPanelProps) { const [car, setCar] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [gallery, setGallery] = useState(false); useEffect(() => { const token = getStoredToken(); setLoading(true); setError(null); carService .getById(carId, token) .then(res => setCar((res as unknown as { data: Car }).data)) .catch((err: Error) => setError(err.message)) .finally(() => setLoading(false)); }, [carId]); useEffect(() => { const h = (e: KeyboardEvent) => { if (e.key === "Escape" && !gallery) onClose(); }; window.addEventListener("keydown", h); return () => window.removeEventListener("keydown", h); }, [gallery, onClose]); // ── Render ─────────────────────────────────────────────────────────────────── return ( <> {/* Backdrop */}
{/* Panel */} {/* Image gallery overlay */} {gallery && car && ( setGallery(false)} /> )} ); }