'use client'; import { useEffect, useMemo, useState } from 'react'; import { Loader2, RefreshCw, Search } from 'lucide-react'; import { toast } from 'sonner'; import { crmService, CrmCustomer } from '@/services/api/crmService'; import { TablePagination, useClientPagination } from '@/components/ui/TablePagination'; const shell = 'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0'; const field = 'h-9 px-3 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary'; function customerName(customer: CrmCustomer) { const named = [customer.first_name, customer.last_name].filter(Boolean).join(' ').trim(); return customer.name || named || '—'; } function tierLabel(value?: string) { if (!value) return '—'; return value .toLowerCase() .split('_') .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) .join(' '); } function isDual(customer: CrmCustomer) { return Boolean(customer.is_dual_customer) || ((customer.total_orders_count || 0) > 0 && (customer.total_repairs_count || 0) > 0); } export default function ServiceRepairCustomersPage() { const [customers, setCustomers] = useState([]); const [loading, setLoading] = useState(true); const [query, setQuery] = useState(''); useEffect(() => { loadCustomers(); }, []); const loadCustomers = async () => { try { setLoading(true); const data = await crmService.getCustomers(); const svcCusts = data.filter((c) => (c.total_repairs_count || 0) > 0 || c.is_dual_customer || c.service_tier); setCustomers(svcCusts); } catch { toast.error('Failed to load service customers'); } finally { setLoading(false); } }; const filtered = useMemo(() => { const q = query.trim().toLowerCase(); if (!q) return customers; return customers.filter((customer) => { const haystack = [customerName(customer), customer.email, customer.phone, customer.service_tier, customer.sales_tier] .filter(Boolean) .join(' ') .toLowerCase(); return haystack.includes(q); }); }, [customers, query]); const pager = useClientPagination(filtered); return (

Repair customers

{filtered.length}
setQuery(event.target.value)} placeholder="Search" className={`${field} w-full pl-9`} />
{loading ? (
) : (
{pager.items.length === 0 ? ( ) : pager.items.map((customer) => ( ))}
Customer Phone Email Repairs Service tier Sales tier
{query ? 'No repair customers match this search' : 'No repair customers yet'}
{customerName(customer)} {isDual(customer) && ( Store and repair )}
{crmService.formatPhoneDisplay(customer.phone) || '—'} {customer.email || '—'} {customer.total_repairs_count || 0} {tierLabel(customer.service_tier)} {tierLabel(customer.sales_tier)}
)}
); }