import { useMemo, useState } from 'react' import { useQuery } from '@tanstack/react-query' import { useNavigate } from 'react-router-dom' import { get } from '../../lib/api' import type { Band, Enterprise, Sector } from '../../lib/api' import { ActionChip, Empty, GhostBtn, RiskBadge, Sparkline, inputCls } from '../../components/ui/bits' import { SECTORS, SECTOR_LIST } from '../../lib/demo/enterprises' import { scopedEnterprises } from '../../lib/demo/ops' import { inrCompact } from '../../lib/demo/seed' import { exportCsv } from '../../lib/csv' import { useScope } from './officerScope' const BAND_FILTERS: { key: Band | 'ALL'; label: string }[] = [ { key: 'ALL', label: 'All bands' }, { key: 'RED', label: 'High risk' }, { key: 'WATCH', label: 'Watch' }, { key: 'STANDARD', label: 'Standard' }, ] /** The full enterprise register — searchable and filterable, feeding the detail page. */ export function EnterprisesPage() { const navigate = useNavigate() const { block } = useScope() const [q, setQ] = useState('') const [band, setBand] = useState('ALL') const [sector, setSector] = useState('ALL') const { data } = useQuery({ queryKey: ['enterprises', block], queryFn: () => get('/officer/enterprises', { block }), }) const all = data ?? scopedEnterprises(block) const rows = useMemo(() => { const needle = q.trim().toLowerCase() return all.filter( (e) => (band === 'ALL' || e.band === band) && (sector === 'ALL' || e.sector === sector) && (!needle || e.name.toLowerCase().includes(needle) || e.enterpriseId.toLowerCase().includes(needle)), ) }, [all, band, sector, q]) function handleExport() { exportCsv( `udyamlinc-enterprises-${block}.csv`, ['Enterprise ID', 'Name', 'Sector', 'Block', 'District', 'Score', 'Band', 'Vintage (months)', 'Outstanding', 'Instalment'], rows.map((e) => [ e.enterpriseId, e.name, SECTORS[e.sector].label, e.block, e.district, e.score, e.band, e.vintageMonths, e.loanOutstanding, e.monthlyInstalment, ]), ) } return (
setQ(e.target.value)} />
{BAND_FILTERS.map((b) => ( setBand(b.key)}> {b.label} ))}
{rows.length} enterprises Export CSV
{rows.map((e) => ( navigate(`/officer/enterprises/${e.enterpriseId}`)} > ))}
Enterprise Sector Block Vintage Outstanding Instalment Score Status Trend
{e.name} {e.enterpriseId} {SECTORS[e.sector].short} {e.block} {e.vintageMonths} mo {inrCompact(e.loanOutstanding)} {inrCompact(e.monthlyInstalment)} {e.score}
{!rows.length && }
) }