import { useState } from 'react'; import { Badge, Input, Select, Space, Table, Typography } from 'antd'; import type { TableColumnsType } from 'antd'; import { useNavigate } from '@tanstack/react-router'; import type { ActivityStatus, ClientSummary } from '@sar/api-interface'; import { useClientList } from '../../lib/queries/clients'; const { Title } = Typography; const { Search } = Input; // ─── Badge configs ──────────────────────────────────────────────────────────── const ACTIVITY_CONFIG: Record = { active: { color: 'success', label: 'Ativo' }, alert: { color: 'warning', label: 'Em alerta' }, inactive: { color: 'error', label: 'Inativo' }, }; // ─── Columns ────────────────────────────────────────────────────────────────── function buildColumns(navigate: ReturnType): TableColumnsType { return [ { title: 'Cliente', dataIndex: 'nome', key: 'nome', render: (nome: string, record: ClientSummary) => ( navigate({ to: '/clientes/$id', params: { id: String(record.idCliente) } }) } > {nome} {record.razao && ( {record.razao} )} ), sorter: true, }, { title: 'CNPJ / CPF', dataIndex: 'cgcpf', key: 'cgcpf', width: 160, render: (v: string | null) => ( {v ?? '—'} ), }, { title: 'Atividade', dataIndex: 'activityStatus', key: 'activityStatus', width: 120, render: (v: ActivityStatus) => { const cfg = ACTIVITY_CONFIG[v]; return ; }, }, { title: 'Última compra', dataIndex: 'dtUltimaCompra', key: 'dtUltimaCompra', width: 140, render: (v: string | null) => { if (!v) return ; return ( {new Date(v).toLocaleDateString('pt-BR')} ); }, }, ]; } // ─── Page ───────────────────────────────────────────────────────────────────── export function ClientsPage() { const navigate = useNavigate(); const [q, setQ] = useState(''); const [search, setSearch] = useState(''); const [activityFilter, setActivityFilter] = useState(); const [page, setPage] = useState(1); const limit = 50; const { data, isLoading, isFetching } = useClientList({ q: search || undefined, status: activityFilter, page, limit, }); const columns = buildColumns(navigate); return ( {/* Cabeçalho */} Carteira de Clientes {data ? `${data.total} cliente${data.total !== 1 ? 's' : ''} na sua carteira` : ' '} {/* Filtros */} setQ(e.target.value)} onSearch={(v) => { setSearch(v); setPage(1); }} allowClear style={{ width: 320 }} /> placeholder="Atividade" allowClear style={{ width: 140 }} value={activityFilter} onChange={(v) => { setActivityFilter(v); setPage(1); }} options={[ { value: 'active', label: 'Ativo' }, { value: 'alert', label: 'Em alerta' }, { value: 'inactive', label: 'Inativo' }, ]} /> {/* Tabela */} columns={columns} dataSource={data?.data ?? []} rowKey="idCliente" loading={isLoading || isFetching} pagination={{ current: page, pageSize: limit, total: data?.total ?? 0, showSizeChanger: false, showTotal: (total) => `${total} clientes`, onChange: (p) => setPage(p), }} scroll={{ x: 700 }} size="middle" onRow={(record) => ({ style: { cursor: 'pointer' }, onClick: () => navigate({ to: '/clientes/$id', params: { id: String(record.idCliente) } }), })} /> ); }