'use client'; import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { Link } from '@/i18n/Link'; import { useT, useLocale } from '@/i18n/I18nProvider'; import api from '@/lib/api'; import { useAuthStore } from '@/lib/store'; import { notify } from '@/lib/notify'; import type { Ticket, CreateTicketDto, TicketDepartment, TicketPriority } from '@/types'; import { Wrench, Briefcase, Ticket as TicketIcon, X } from 'lucide-react'; import { Select } from '@/components/ui/select'; 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 TicketsPage() { const t = useT(); const tk = t.dashboard.tickets; const locale = useLocale(); const queryClient = useQueryClient(); const user = useAuthStore((s) => s.user); const [showCreate, setShowCreate] = useState(false); // Staff can only create tickets to other departments const availableDepartments: { value: TicketDepartment; label: string }[] = []; if (user?.role !== 'technical') { availableDepartments.push({ value: 'technical', label: tk.departmentFull.technical }); } if (user?.role !== 'sales') { availableDepartments.push({ value: 'sales', label: tk.departmentFull.sales }); } const canCreateTicket = availableDepartments.length > 0; const [form, setForm] = useState({ subject: '', department: availableDepartments[0]?.value || 'technical', priority: 'medium', message: '', }); const { data: tickets = [], isLoading } = useQuery({ queryKey: ['my-tickets'], queryFn: () => api.get('/tickets/my').then((r) => r.data), }); const createMutation = useMutation({ mutationFn: (data: CreateTicketDto) => api.post('/tickets', data).then((r) => r.data), onSuccess: () => { notify.success(tk.createdSuccess); queryClient.invalidateQueries({ queryKey: ['my-tickets'] }); setShowCreate(false); setForm({ subject: '', department: availableDepartments[0]?.value || 'technical', priority: 'medium', message: '' }); }, onError: (err: any) => notify.error(err, tk.createFailed), }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); createMutation.mutate(form); }; return (

{tk.myTickets}

{tk.myTicketsSubtitle}

{canCreateTicket && ( )}
{/* Create Ticket Form */} {showCreate && canCreateTicket && (

{tk.createNewTicket}

setForm({ ...form, subject: e.target.value })} required minLength={3} />
setForm({ ...form, priority: v as TicketPriority })} options={[ { value: 'low', label: tk.priority.low }, { value: 'medium', label: tk.priority.medium }, { value: 'high', label: tk.priority.high }, ]} />