"use client"; // Components/Car/CarImageGallery.tsx // Full-screen gallery modal for viewing, uploading and deleting car images. import { useRef, useState } from "react"; import { Spinner } from "../UI"; import { useCarImages } from "../../hooks/useCars"; import { STAGE_MAP } from "../../types/car"; import type { Car, CarImage, ImageStage } from "../../types/car"; // ── Props ───────────────────────────────────────────────────────────────────── interface CarImageGalleryProps { car: Car; onClose: () => void; } // ── Component ───────────────────────────────────────────────────────────────── export function CarImageGallery({ car, onClose }: CarImageGalleryProps) { const [stage, setStage] = useState("GENERAL"); const [sortBy, setSortBy] = useState<"asc" | "desc">("desc"); const [lightbox, setLightbox] = useState(null); const fileRef = useRef(null); const { images, loading, uploading, deleting, error, setError, uploadImages, deleteImage, } = useCarImages(car.id, sortBy); const handleUpload = async (e: React.ChangeEvent) => { const files = Array.from(e.target.files ?? []); if (!files.length) return; await uploadImages(files, stage); if (fileRef.current) fileRef.current.value = ""; }; const handleDelete = async (imageId: string) => { const ok = await deleteImage(imageId); if (ok && lightbox?.id === imageId) setLightbox(null); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Escape") { if (lightbox) setLightbox(null); else onClose(); } }; const imgSrc = (img: CarImage) => img.url ?? `/api/proxy/car-photos/${img.image}`; return (
{ if (e.target === e.currentTarget) onClose(); }} style={{ position: "fixed", inset: 0, zIndex: 70, background: "rgba(15,23,42,0.7)", backdropFilter: "blur(6px)", display: "flex", flexDirection: "column", }} > {/* ── Header ── */}

معرض الصور

{car.manufacturer} {car.model} — {car.plateLetters} {car.plateNumber}

{/* Sort */} {/* Stage selector for upload */} {/* Upload button */} {/* Close */}
{/* ── Error ── */} {error && (
⚠ {error}
)} {/* ── Gallery Grid ── */}
{loading ? (
جارٍ تحميل الصور…
) : images.length === 0 ? (
📸

لا توجد صور بعد

اضغط على "رفع صور" لإضافة أولى الصور لهذه المركبة.

) : (
{images.map(img => { const stageInfo = STAGE_MAP[img.stage ?? "GENERAL"] ?? STAGE_MAP.GENERAL; const isDeleting = deleting === img.id; return (
setLightbox(img)}> {/* eslint-disable-next-line @next/next/no-img-element */} {`صورة { (e.target as HTMLImageElement).src = "/file.svg"; }} /> {stageInfo.label}
{new Date(img.createdAt).toLocaleDateString("ar-SA", { month: "short", day: "numeric" })}
); })}
)}
{/* ── Lightbox ── */} {lightbox && (
setLightbox(null)} style={{ position: "fixed", inset: 0, zIndex: 80, background: "rgba(0,0,0,0.9)", display: "flex", alignItems: "center", justifyContent: "center", padding: "1rem" }}> {/* eslint-disable-next-line @next/next/no-img-element */} عرض مكبَّر e.stopPropagation()} onError={e => { (e.target as HTMLImageElement).src = "/file.svg"; }} />
)}
); }