"use client"; import { useEffect, useState } from "react"; import { useParams, useRouter } from "next/navigation"; import { Alert, ConfirmDialog, Toast } from "@/src/Components/UI"; import { AddressFormModal } from "@/src/Components/Client"; import { AddressDetails } from "@/src/Components/Client_Adress/AddressDetails"; import { clientAddressService } from "@/src/services/clientAddress.service"; import { clientService } from "@/src/services/client.service"; import { getStoredToken } from "@/src/lib/auth"; import type { Client } from "@/src/types/client"; import type { ClientAddress } from "@/src/types/client_adresses"; import type { ToastNotification } from "@/src/Components/UI"; import type { CreateAddressFormValues, UpdateAddressFormValues, } from "@/src/validations/client_address.validator"; // ─── Page ────────────────────────────────────────────────────────────────── export default function AddressDetailPage() { const params = useParams(); const router = useRouter(); const clientId = (params?.clientId ?? params?.id) as string | undefined; const addressId = params?.addressId as string | undefined; // ── Data state ─────────────────────────────────────────────────────────── const [address, setAddress] = useState(null); const [client, setClient] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); // ── Modal state ────────────────────────────────────────────────────────── const [editOpen, setEditOpen] = useState(false); const [deleteOpen, setDeleteOpen] = useState(false); const [deleting, setDeleting] = useState(false); // ── Toast ──────────────────────────────────────────────────────────────── const [notification, setNotification] = useState(null); const notify = (n: ToastNotification) => { setNotification(n); setTimeout(() => setNotification(null), 4000); }; // ── Fetch ──────────────────────────────────────────────────────────────── useEffect(() => { if (!clientId || !addressId) return; setLoading(true); Promise.all([ clientAddressService.getById(clientId, addressId, getStoredToken()), clientService.getById(clientId, getStoredToken()), ]) .then(([addrRes, clientRes]) => { const raw = (addrRes as any).data ?? addrRes; setAddress({ ...raw, id: raw.id ?? raw._id }); setClient(clientRes?.data); }) .catch(() => setError("تعذّر تحميل بيانات العنوان. يرجى المحاولة مجدداً.")) .finally(() => setLoading(false)); }, [clientId, addressId]); // ── Update ─────────────────────────────────────────────────────────────── const handleUpdateSubmit = async ( data: CreateAddressFormValues | UpdateAddressFormValues ): Promise => { if (!clientId || !addressId) return false; try { const res = await clientAddressService.update( clientId, addressId, data as UpdateAddressFormValues, getStoredToken() ); const raw = (res as any).data ?? res; setAddress({ ...raw, id: raw.id ?? raw._id }); notify({ type: "success", message: "تم تحديث العنوان بنجاح." }); return true; } catch (err) { notify({ type: "error", message: err instanceof Error ? err.message : "تعذّر تحديث العنوان.", }); return false; } }; // ── Delete ─────────────────────────────────────────────────────────────── const handleDeleteConfirm = async () => { if (!clientId || !addressId || deleting) return; setDeleting(true); try { await clientAddressService.delete(clientId, addressId, getStoredToken()); notify({ type: "success", message: "تم حذف العنوان بنجاح." }); // short delay so the toast shows before navigation setTimeout(() => { router.replace(`/dashboard/clients/${clientId}/addresses`); }, 700); } catch (err) { notify({ type: "error", message: err instanceof Error ? err.message : "تعذّر حذف العنوان.", }); setDeleting(false); } }; // ── Guards ─────────────────────────────────────────────────────────────── if (loading) { return (
); } if (error || !address) { return (
); } // ── Render ──────────────────────────────────────────────────────────────── return ( <> {/* Edit modal */} {editOpen && ( setEditOpen(false)} onSubmit={handleUpdateSubmit} /> )} {/* Delete confirmation */} { if (!deleting) setDeleteOpen(false); }} onConfirm={handleDeleteConfirm} title="حذف العميل" description={`هل أنت متأكد من حذف ${address.label}؟ سيتم حذف جميع عناوينه أيضاً. لا يمكن التراجع عن هذا الإجراء.`} />
{/* ── Page header ── */}
{/* Back link */}

تفاصيل العنوان

{address.label}

{client && (

{client.name}

)}
{/* Action buttons */}
{/* ── Address details component ── */}
); }