ifixkart-admin/app/(admin)/repair-tickets/page.tsx

214 lines
9.4 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import { repairService, RepairTicket } from '@/services/api/repairService';
import { Clock, Search, Smartphone, ShieldCheck, RefreshCw, Loader2 } from 'lucide-react';
const STAGES = ['INTAKE', 'DIAGNOSING', 'REPAIRING', 'COMPLETED', 'DELIVERED'];
const shell =
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0';
export default function RepairTicketsPage() {
const [tickets, setTickets] = useState<RepairTicket[]>([]);
const [loading, setLoading] = useState(true);
const [searchImei, setSearchImei] = useState('');
useEffect(() => {
loadTickets();
}, []);
const loadTickets = async () => {
try {
setLoading(true);
const data = await repairService.getTickets();
setTickets(data);
} catch (err) {
setTickets([
{
ticket_id: '1',
tenant_id: 'tenant_default',
ticket_number: 'REP-2026-001',
customer_id: 'c1',
customer_name: 'Adithiyan E',
customer_phone: '+91 9876543210',
imei_serial: '356789123456789',
brand_name: 'Apple',
model_name: 'iPhone 14 Pro',
problem_description: 'Screen shattered, touch un-responsive',
status: 'REPAIRING',
priority: 'URGENT',
sla_due_at: new Date(Date.now() + 3600000 * 3).toISOString(),
created_at: new Date().toISOString()
},
{
ticket_id: '2',
tenant_id: 'tenant_default',
ticket_number: 'REP-2026-002',
customer_id: 'c2',
customer_name: 'Suresh Babu',
customer_phone: '+91 9123456789',
imei_serial: '864201928374651',
brand_name: 'Samsung',
model_name: 'Galaxy S23 Ultra',
problem_description: 'Battery drain & overheating during charging',
status: 'DIAGNOSING',
priority: 'NORMAL',
sla_due_at: new Date(Date.now() + 3600000 * 12).toISOString(),
created_at: new Date().toISOString()
},
{
ticket_id: '3',
tenant_id: 'tenant_default',
ticket_number: 'REP-2026-003',
customer_id: 'c3',
customer_name: 'Meena R',
customer_phone: '+91 9988776655',
imei_serial: '990011223344556',
brand_name: 'OnePlus',
model_name: 'OnePlus 11',
problem_description: 'Charging port damaged',
status: 'INTAKE',
priority: 'HIGH',
sla_due_at: new Date(Date.now() + 3600000 * 6).toISOString(),
created_at: new Date().toISOString()
}
]);
} finally {
setLoading(false);
}
};
const filteredTickets = (tickets || []).filter((t) => {
const q = (searchImei || '').toLowerCase().trim();
if (!q) return true;
const imei = (t.imei_serial || '').toLowerCase();
const num = (t.ticket_number || '').toLowerCase();
const cust = (t.customer_name || '').toLowerCase();
const brand = (t.brand_name || '').toLowerCase();
const model = (t.model_name || '').toLowerCase();
const desc = (t.problem_description || t.issue_description || '').toLowerCase();
return imei.includes(q) || num.includes(q) || cust.includes(q) || brand.includes(q) || model.includes(q) || desc.includes(q);
});
return (
<div className="flex flex-col gap-5 min-w-0">
<div className="flex flex-wrap items-center justify-between gap-3 min-w-0">
<div className="flex items-center gap-2 min-w-0">
<h1 className="text-lg font-semibold text-foreground">5-Stage Repair Queue</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 leading-none">
{filteredTickets.length}
</span>
</div>
<div className="flex items-center gap-2">
<div className="relative">
<Search className="w-3.5 h-3.5 text-muted-foreground absolute left-3 top-1/2 -translate-y-1/2" />
<input
type="text"
placeholder="Search IMEI, Ticket #, Customer..."
value={searchImei}
onChange={(e) => setSearchImei(e.target.value)}
className="h-9 w-full sm:w-[240px] pl-8 pr-3 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary"
/>
</div>
<button
type="button"
onClick={loadTickets}
className="w-8 h-8 crm-radius-control border border-border bg-card text-muted-foreground hover:text-foreground hover:bg-muted cursor-pointer flex items-center justify-center"
title="Reload"
>
<RefreshCw className={`w-3.5 h-3.5 ${loading ? 'animate-spin' : ''}`} />
</button>
</div>
</div>
{loading ? (
<div className={`${shell} py-16 flex justify-center`}>
<Loader2 className="w-6 h-6 text-primary animate-spin" />
</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-5 gap-4">
{STAGES.map((stage) => {
const stageTickets = filteredTickets.filter((t) => {
const currentStage = (t.stage || t.status || '').toUpperCase();
return currentStage === stage.toUpperCase() ||
(stage === 'INTAKE' && (currentStage === 'RECEIVED' || currentStage === 'INTAKE')) ||
(stage === 'REPAIRING' && (currentStage === 'UNDER_REPAIR' || currentStage === 'PART_PENDING')) ||
(stage === 'COMPLETED' && (currentStage === 'READY_FOR_DELIVERY' || currentStage === 'QC_TESTING'));
});
return (
<div key={stage} className={`${shell} p-3 flex flex-col min-h-[500px]`}>
<div className="flex items-center justify-between border-b border-border pb-2.5 mb-3">
<span className="text-[13px] font-semibold text-foreground">
{stage}
</span>
<span className="inline-flex items-center justify-center min-w-5 h-5 px-1.5 crm-radius-badge bg-muted text-muted-foreground text-[11px] font-semibold">
{stageTickets.length}
</span>
</div>
<div className="space-y-3 overflow-y-auto">
{stageTickets.map((t) => (
<div
key={t.ticket_id}
className="p-3 bg-background border border-border crm-radius-control hover:border-primary transition-colors cursor-pointer"
>
<div className="flex items-center justify-between gap-2 mb-1.5">
<span className="text-[13px] font-semibold text-foreground">{t.ticket_number || 'TKT-PENDING'}</span>
<span
className={`inline-flex items-center px-2 py-0.5 crm-radius-badge text-[11px] font-semibold ${
t.priority === 'URGENT'
? 'bg-destructive/10 text-destructive'
: t.priority === 'HIGH'
? 'bg-warning/15 text-warning'
: 'bg-muted text-muted-foreground'
}`}
>
{t.priority || 'NORMAL'}
</span>
</div>
<div className="text-[13px] font-medium text-foreground flex items-center gap-1.5 mb-1">
<Smartphone className="w-3.5 h-3.5 text-muted-foreground shrink-0" />
{(t.brand_name || t.model_name)
? `${t.brand_name || ''} ${t.model_name || ''}`.trim()
: (t.device_id ? `Device #${t.device_id.slice(-6)}` : 'Customer Device')}
</div>
<p className="text-[13px] text-muted-foreground line-clamp-2 mb-2">
{t.problem_description || t.issue_description || 'No description provided'}
</p>
<div className="border-t border-border pt-2 flex items-center justify-between gap-2 text-[12px] text-muted-foreground">
<div className="flex items-center gap-1 min-w-0">
<ShieldCheck className="w-3.5 h-3.5 text-success shrink-0" />
<span className="font-mono truncate">
{t.imei_serial ? t.imei_serial.slice(-6) : (t.device_id ? t.device_id.slice(-6) : (t.ticket_number || 'N/A').slice(-6))}
</span>
</div>
{t.sla_due_at && (
<div className="flex items-center gap-1 text-warning font-semibold shrink-0">
<Clock className="w-3.5 h-3.5" />
<span>SLA Active</span>
</div>
)}
</div>
</div>
))}
{stageTickets.length === 0 && (
<div className="py-12 text-center text-[13px] text-muted-foreground border border-dashed border-border crm-radius-control">
No Tickets in {stage}
</div>
)}
</div>
</div>
);
})}
</div>
)}
</div>
);
}