154 lines
6.6 KiB
TypeScript
154 lines
6.6 KiB
TypeScript
'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<CrmCustomer[]>([]);
|
|
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 (
|
|
<div className="flex flex-col gap-5 min-w-0">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<div className="flex items-center gap-2 min-w-0">
|
|
<h1 className="text-lg font-semibold text-foreground">Repair customers</h1>
|
|
<span className="inline-flex items-center justify-center min-w-5 h-5 px-1.5 crm-radius-toggle bg-primary/10 text-primary text-[11px] font-semibold">
|
|
{filtered.length}
|
|
</span>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={() => loadCustomers()}
|
|
className="w-9 h-9 crm-radius-control border border-border bg-card text-muted-foreground hover:text-foreground hover:bg-muted flex items-center justify-center cursor-pointer"
|
|
title="Reload"
|
|
>
|
|
<RefreshCw className={`w-3.5 h-3.5 ${loading ? 'animate-spin' : ''}`} />
|
|
</button>
|
|
</div>
|
|
|
|
<div className={shell}>
|
|
<div className="px-4 py-3 border-b border-border">
|
|
<div className="relative max-w-[320px]">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground pointer-events-none" />
|
|
<input
|
|
value={query}
|
|
onChange={(event) => setQuery(event.target.value)}
|
|
placeholder="Search"
|
|
className={`${field} w-full pl-9`}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="py-16 flex justify-center">
|
|
<Loader2 className="w-6 h-6 animate-spin text-primary" />
|
|
</div>
|
|
) : (
|
|
<div className="overflow-x-auto">
|
|
<table className="crm-data-table min-w-[860px]">
|
|
<thead>
|
|
<tr className="border-b border-border">
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Customer</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Phone</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Email</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Repairs</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Service tier</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Sales tier</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{pager.items.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={6} className="px-4 py-16 text-center text-[13px] text-muted-foreground whitespace-normal">
|
|
{query ? 'No repair customers match this search' : 'No repair customers yet'}
|
|
</td>
|
|
</tr>
|
|
) : pager.items.map((customer) => (
|
|
<tr key={customer.customer_id} className="border-b border-border last:border-0">
|
|
<td className="px-4 py-3">
|
|
<div className="flex items-center gap-2 min-w-0">
|
|
<span className="text-[13px] font-semibold text-foreground truncate">{customerName(customer)}</span>
|
|
{isDual(customer) && (
|
|
<span className="inline-flex items-center px-2 py-0.5 crm-radius-badge bg-primary/10 text-primary text-[11px] font-semibold shrink-0">
|
|
Store and repair
|
|
</span>
|
|
)}
|
|
</div>
|
|
</td>
|
|
<td className="px-4 py-3 text-foreground">{crmService.formatPhoneDisplay(customer.phone) || '—'}</td>
|
|
<td className="px-4 py-3 text-muted-foreground">{customer.email || '—'}</td>
|
|
<td className="px-4 py-3 text-foreground">{customer.total_repairs_count || 0}</td>
|
|
<td className="px-4 py-3">
|
|
<span className="inline-flex items-center px-2.5 py-1 crm-radius-badge bg-muted text-foreground text-[11px] font-semibold">
|
|
{tierLabel(customer.service_tier)}
|
|
</span>
|
|
</td>
|
|
<td className="px-4 py-3 text-muted-foreground">{tierLabel(customer.sales_tier)}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
<TablePagination {...pager} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|