'use client'; import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import Link from 'next/link'; import api from '@/lib/api'; import { useAuthStore } from '@/lib/store'; import type { Ticket, TicketStatus } from '@/types'; import { Wrench, Briefcase, User, CheckCircle } from 'lucide-react'; const statusColors: Record = { open: 'bg-yellow-100 text-yellow-700', waiting: 'bg-orange-100 text-orange-700', answered: 'bg-green-100 text-green-700', closed: 'bg-gray-100 text-gray-500', }; const priorityColors: Record = { low: 'bg-blue-100 text-blue-700', medium: 'bg-yellow-100 text-yellow-700', high: 'bg-red-100 text-red-700', }; export default function StaffTicketsPage() { const user = useAuthStore((s) => s.user); const [statusFilter, setStatusFilter] = useState(''); // Determine which department this staff member handles const department = user?.role === 'sales' ? 'sales' : 'technical'; const departmentLabel = department === 'technical' ? 'Technical' : 'Sales'; const DeptIcon = department === 'technical' ? Wrench : Briefcase; const { data: tickets = [], isLoading } = useQuery({ queryKey: ['staff-tickets', department, statusFilter], queryFn: () => { const params = statusFilter ? `?status=${statusFilter}` : ''; return api.get(`/tickets/staff/${department}${params}`).then((r) => r.data); }, }); const unansweredCount = tickets.filter((t) => t.status === 'open' || t.status === 'waiting').length; const answeredCount = tickets.filter((t) => t.status === 'answered').length; return (

{departmentLabel} Tickets

{tickets.length} total tickets

{/* Summary Cards */}

Unanswered

0 ? 'text-red-600' : 'text-green-600'}`}> {unansweredCount}

Need response

Answered

{answeredCount}

Waiting for user

Total Open

{tickets.filter((t) => t.status !== 'closed').length}

Excluding closed

{/* Filters */}
{(['', 'open', 'waiting', 'answered', 'closed'] as const).map((status) => ( ))}
{/* Tickets List */} {isLoading ? (
) : tickets.length === 0 ? (

No tickets

{statusFilter ? `No ${statusFilter} tickets` : 'No tickets in this department'}

) : (
{tickets.map((ticket) => (

{ticket.subject}

{ticket.status} {ticket.priority}
{ticket.user && ( {ticket.user.firstName} {ticket.user.lastName} ({ticket.user.email}) )} {new Date(ticket.createdAt).toLocaleDateString('en-US', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' })} {ticket.messages?.length || 0} msg{(ticket.messages?.length || 0) !== 1 ? 's' : ''}
))}
)}
); }