import { useState } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { useTranslation } from 'react-i18next' import { Pencil, Trash2, XCircle, CheckCircle2 } from 'lucide-react' import { Button } from '@/components/ui/button' import { Skeleton } from '@/components/ui/skeleton' import { ConfirmDialog } from '@/components/confirmDialog' import { BentoGrid, BentoCard, InfoRow, AuditLog, CollapsibleSection, EmptyState } from '@/components/viewPageComponents' import { DataTable, type ColumnDef } from '@/components/dataTable' import { DataTableToolbar } from '@/components/dataTableToolbar' import { useClient, useDeleteClient } from '@/features/clients/hooks/useClients' import type { ClientRole, ClientBranch, ClientAuditLog } from '@/features/clients/api/clientsApi' // ─── ActiveIndicator ────────────────────────────────────────────────────────── function ActiveIndicator({ active }: { active: boolean }) { return active ? ( ) : ( ); } // ─── Page ───────────────────────────────────────────────────────────────────── export default function ClientDetailPage() { const { id } = useParams<{ id: string }>() const clientId = Number(id) const { t } = useTranslation() const navigate = useNavigate() const deleteMutation = useDeleteClient() const { data: client, isLoading } = useClient(clientId) const [deleteOpen, setDeleteOpen] = useState(false) const [roleSearch, setRoleSearch] = useState('') const [permSearch, setPermSearch] = useState('') const [branchSearch, setBranchSearch] = useState('') const [loanSearch, setLoanSearch] = useState('') const [cardSearch, setCardSearch] = useState('') // ── Role columns ────────────────────────────────────────────────────────── const roleColumns: ColumnDef[] = [ { accessorKey: 'id', header: t('common.id', 'ID'), cell: ({ row }) => {row.original.id}, size: 80, }, { accessorKey: 'code', header: t('roles.fields.code', 'KOD'), cell: ({ row }) => {row.original.code}, }, { accessorKey: 'name', header: t('roles.fields.name', 'ADY'), cell: ({ row }) => {row.original.name}, }, { accessorKey: 'guardName', header: t('roles.fields.guardName', 'GUARD NAME'), cell: ({ row }) => {row.original.guardName}, }, { id: 'actions', header: '', cell: () => (
), size: 60, }, ] // ── Branch columns ──────────────────────────────────────────────────────── const branchColumns: ColumnDef[] = [ { accessorKey: 'id', header: t('common.id', 'ID'), cell: ({ row }) => {row.original.id}, size: 60, }, { accessorKey: 'name', header: t('branches.fields.name', 'ADY'), cell: ({ row }) => {row.original.name}, }, { accessorKey: 'region', header: t('branches.fields.region', 'WELAÝAT'), cell: ({ row }) => {row.original.region}, }, { accessorKey: 'district', header: t('branches.fields.district', 'ETRAP'), cell: ({ row }) => {row.original.district}, }, { accessorKey: 'uniqueCode', header: t('branches.fields.uniqueCode', 'UNIKAL BELGI'), cell: ({ row }) => {row.original.uniqueCode}, }, { id: 'actions', header: '', cell: () => (
), size: 60, }, ] // ── Loading ─────────────────────────────────────────────────────────────── if (isLoading) { return (
{Array.from({ length: 4 }).map((_, i) => (
{Array.from({ length: 3 }).map((_, j) => ( ))}
))}
) } if (!client) return null const auditLogs = (client.auditLogs ?? []).map((log: ClientAuditLog) => ({ id: String(log.id), action: log.actionName, by: log.performedBy, target: log.target, status: log.status, date: log.createdAt, })) const filteredRoles = (client.roles ?? []).filter((r) => r.name.toLowerCase().includes(roleSearch.toLowerCase()) || r.code.toLowerCase().includes(roleSearch.toLowerCase()), ) const filteredBranches = (client.branches ?? []).filter((b) => b.name.toLowerCase().includes(branchSearch.toLowerCase()), ) return (
{/* ── Page header ──────────────────────────────────────────────────── */}

{t('clients.detailTitle', 'Müşderi giňişleýin')}: {client.name}

{/* ── Row 1: Esasy maglumatlar + Kontakt ──────────────────────────── */} {/* ── Rollar ───────────────────────────────────────────────────────── */} {}} columnOrder={[]} onColumnOrderChange={() => {}} actionLabel={t('clients.addRole', 'Rol birikdiriň')} onAction={() => {}} /> {filteredRoles.length > 0 ? ( String(r.id)} enableRowSelection /> ) : ( )} {/* ── Rugsatlar ────────────────────────────────────────────────────── */} {}} columnOrder={[]} onColumnOrderChange={() => {}} actionLabel={t('clients.addPermission', 'Rugsat birikdiriň')} onAction={() => {}} /> {(client.permissions ?? []).length === 0 && ( )} {/* ── Şahamçalar ───────────────────────────────────────────────────── */} {}} columnOrder={[]} onColumnOrderChange={() => {}} actionLabel={t('clients.addBranch', 'Şahamça birikdiriň')} onAction={() => {}} /> {filteredBranches.length > 0 ? ( String(r.id)} enableRowSelection /> ) : ( )} {/* ── Karz sargyt ──────────────────────────────────────────────────── */} {}} columnOrder={[]} onColumnOrderChange={() => {}} actionLabel={t('clients.addLoanOrder', 'Karz sargyt dörediň')} onAction={() => {}} /> {(client.loanOrders ?? []).length === 0 && ( )} {/* ── Kart sargyt ──────────────────────────────────────────────────── */} {}} columnOrder={[]} onColumnOrderChange={() => {}} actionLabel={t('clients.addCardOrder', 'Kart sargyt dörediň')} onAction={() => {}} /> {(client.cardOrders ?? []).length === 0 && ( )} {/* ── Audit log ────────────────────────────────────────────────────── */} deleteMutation.mutate(clientId, { onSuccess: () => navigate('/clients') })} isLoading={deleteMutation.isPending} />
) }