"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; // ── UI components ────────────────────────────────────────────────────────── // NOTE: Toast is now imported from the canonical UI barrel, NOT from Client/Toast. import { Alert, Toast, ArchiveButton } from "@/src/Components/UI"; // ── Client-specific components ───────────────────────────────────────────── import { useClients } from "@/src/hooks/useClients"; import type { Client, ClientFormData } from "@/src/types/client"; import { ClientFormModal } from "@/src/Components/Client/Clientformmodal"; import { ClientTable } from "@/src/Components/Client/Clienttable"; import { DeleteConfirmModal } from "@/src/Components/Client/Deleteconfirmmodal"; import { ArchivedClientsModal } from "@/src/Components/Client/archive/ArchivedClientsModal"; export default function ClientsPage() { const router = useRouter(); // ── Modal state ────────────────────────────────────────────────────────── // false = closed | null = create mode | Client = edit mode const [formTarget, setFormTarget] = useState(false); const [deleteTarget, setDeleteTarget] = useState(null); const [deleting, setDeleting] = useState(false); // Archive browser modal open/closed const [archiveOpen, setArchiveOpen] = useState(false); // ── Data hook ──────────────────────────────────────────────────────────── const { clients, loading, total, pages, error, page, search, setPage, handleSearch, clearError, createClient, updateClient, deleteClient, notification, } = useClients(); // ── Create / Update handler ────────────────────────────────────────────── const handleFormSubmit = async ( data: ClientFormData, isNew: boolean, ): Promise => { if (isNew) return createClient(data); return updateClient((formTarget as Client).id, data); }; // ── Delete handler ─────────────────────────────────────────────────────── const handleDeleteConfirm = async () => { if (!deleteTarget || deleting) return; setDeleting(true); const ok = await deleteClient(deleteTarget.id); setDeleting(false); if (ok) setDeleteTarget(null); }; // ── Navigate to address management ────────────────────────────────────── // This is called both by the AddressBadge click and the "العناوين" button // inside the ClientDetailPanel. const handleManageAddresses = (client: Client) => { router.push(`/dashboard/clients/${client.id}/addresses`); }; // ── Render ─────────────────────────────────────────────────────────────── return ( <> {/* * Global toast notification. * Using the new UI/Toast which accepts ToastNotification | null. * The hook's `notification` shape { type, message } matches ToastNotification exactly. */} {/* Create / Edit modal */} {formTarget !== false && ( setFormTarget(false)} onSubmit={handleFormSubmit} /> )} {/* Delete confirmation dialog */} {deleteTarget && ( { if (!deleting) setDeleteTarget(null); }} onConfirm={handleDeleteConfirm} /> )} {/* Archive browser modal */} {archiveOpen && ( setArchiveOpen(false)} /> )}
{/* ── Page header ── */}

إدارة العملاء

العملاء

إجمالي{" "} {total} {" "} عميل

{/* Search input */}
handleSearch(e.target.value)} dir="rtl" style={{ width: "100%", height: 40, paddingRight: 36, paddingLeft: 12, borderRadius: "var(--radius-lg)", border: "1px solid var(--color-border)", background: "var(--color-surface)", fontSize: 13, outline: "none", fontFamily: "var(--font-sans)", color: "var(--color-text-primary)", }} />
{/* Add client button */}
{/* General load error */} {error && } {/* Clients table */} setFormTarget(client)} onDelete={(client) => setDeleteTarget(client)} onAddFirst={() => setFormTarget(null)} onPageChange={setPage} onManageAddresses={handleManageAddresses} />
{/* Floating button to open the archive browser */} setArchiveOpen(true)} label="أرشيف العملاء" /> ); }