"use client" import { useState, useEffect } from "react" import { useParams, useRouter } from "next/navigation" import Link from "next/link" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Separator } from "@/components/ui/separator" import { Textarea } from "@/components/ui/textarea" import { useLoading } from "@/contexts/loading-context" import { ArrowLeft, Clock, CheckCircle, FileText, Settings, User, Calendar, Tag, MessageSquare, Send, AlertTriangle, Wrench, Truck, HelpCircle, Cpu, } from "lucide-react" // Mock ticket data - in production, this would come from an API const mockTickets: Record = { "TKT-001": { id: "TKT-001", title: "PC won't boot after assembly", type: "troubleshooting", priority: "high", status: "open", createdAt: "2024-01-15", updatedAt: "2024-01-16", description: "My PC won't turn on after I assembled all the components. No lights or fans spinning.", assignedTo: "Tech Support", replies: [ { id: 1, author: "Tech Support", message: "Thank you for contacting us. Let's troubleshoot this step by step. First, please check if the power supply switch is turned on.", createdAt: "2024-01-15T10:30:00", }, { id: 2, author: "You", message: "I've checked the power supply switch and it's on. Still no response.", createdAt: "2024-01-15T14:20:00", }, { id: 3, author: "Tech Support", message: "Please verify all power connections, especially the 24-pin ATX and CPU power connectors. Also check if the RAM is properly seated.", createdAt: "2024-01-16T09:15:00", }, ], }, "TKT-002": { id: "TKT-002", title: "Request for delivery service", type: "delivery", priority: "medium", status: "in_progress", createdAt: "2024-01-14", updatedAt: "2024-01-15", description: "I need help with delivery options for my completed build.", assignedTo: "Delivery Team", replies: [ { id: 1, author: "Delivery Team", message: "We offer several delivery options. Please let us know your location and preferred delivery date.", createdAt: "2024-01-14T11:00:00", }, ], }, "TKT-003": { id: "TKT-003", title: "Component compatibility issue", type: "build_problem", priority: "low", status: "resolved", createdAt: "2024-01-10", updatedAt: "2024-01-12", description: "RAM not fitting properly in motherboard slots.", assignedTo: "Build Support", replies: [ { id: 1, author: "Build Support", message: "This is usually a RAM compatibility issue. Please verify the RAM type matches your motherboard specifications.", createdAt: "2024-01-10T15:30:00", }, { id: 2, author: "You", message: "I've checked and the RAM type is correct. Still having issues.", createdAt: "2024-01-11T10:00:00", }, { id: 3, author: "Build Support", message: "Try inserting the RAM at a slight angle and applying even pressure. Make sure the clips click into place.", createdAt: "2024-01-11T16:45:00", }, { id: 4, author: "You", message: "That worked! Thank you for the help.", createdAt: "2024-01-12T09:20:00", }, ], }, } const supportTypes = [ { value: "troubleshooting", label: "Troubleshooting", icon: Wrench }, { value: "build_problem", label: "Build Problem", icon: Settings }, { value: "delivery", label: "Delivery/Repair", icon: Truck }, { value: "general", label: "General Inquiry", icon: HelpCircle }, ] const priorityLevels = [ { value: "low", label: "Low", color: "bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-300" }, { value: "medium", label: "Medium", color: "bg-yellow-100 text-yellow-800 dark:bg-yellow-900/20 dark:text-yellow-300" }, { value: "high", label: "High", color: "bg-orange-100 text-orange-800 dark:bg-orange-900/20 dark:text-orange-300" }, { value: "urgent", label: "Urgent", color: "bg-red-100 text-red-800 dark:bg-red-900/20 dark:text-red-300" }, ] const statusOptions = [ { value: "open", label: "Open", color: "bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-300", icon: Clock }, { value: "in_progress", label: "In Progress", color: "bg-yellow-100 text-yellow-800 dark:bg-yellow-900/20 dark:text-yellow-300", icon: Settings }, { value: "resolved", label: "Resolved", color: "bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-300", icon: CheckCircle }, { value: "closed", label: "Closed", color: "bg-gray-100 text-gray-800 dark:bg-gray-900/20 dark:text-gray-300", icon: FileText }, ] export default function TicketDetailPage() { const params = useParams() const router = useRouter() const { startLoading, stopLoading } = useLoading() const ticketId = params.id as string const [ticket, setTicket] = useState(null) const [isLoading, setIsLoading] = useState(true) const [newReply, setNewReply] = useState("") const [isSubmitting, setIsSubmitting] = useState(false) useEffect(() => { const fetchTicket = async () => { startLoading("Loading ticket details...") setIsLoading(true) // Simulate API call await new Promise(resolve => setTimeout(resolve, 500)) // Try to load from localStorage first (user-created tickets) let ticketData = null if (typeof window !== 'undefined') { const savedTickets = localStorage.getItem('buildmate-support-tickets') if (savedTickets) { try { const tickets = JSON.parse(savedTickets) ticketData = tickets.find((t: any) => t.id === ticketId) || null } catch (e) { console.error('Error loading tickets from localStorage:', e) } } } // If not found in localStorage, check mock tickets if (!ticketData) { ticketData = mockTickets[ticketId] || null } // If ticket found, add empty replies array if it doesn't exist if (ticketData && !ticketData.replies) { ticketData.replies = [] } setTicket(ticketData) setIsLoading(false) stopLoading() } if (ticketId) { fetchTicket() } }, [ticketId, startLoading, stopLoading]) const handleSubmitReply = async (e: React.FormEvent) => { e.preventDefault() if (!newReply.trim()) return setIsSubmitting(true) // Simulate API call await new Promise(resolve => setTimeout(resolve, 500)) // Add new reply (in production, this would be an API call) const newReplyObj = { id: (ticket.replies?.length || 0) + 1, author: "You", message: newReply, createdAt: new Date().toISOString(), } const updatedTicket = { ...ticket, replies: [...(ticket.replies || []), newReplyObj], updatedAt: new Date().toISOString().split('T')[0], } setTicket(updatedTicket) // Update ticket in localStorage if (typeof window !== 'undefined') { const savedTickets = localStorage.getItem('buildmate-support-tickets') if (savedTickets) { try { const tickets = JSON.parse(savedTickets) const ticketIndex = tickets.findIndex((t: any) => t.id === ticket.id) if (ticketIndex !== -1) { tickets[ticketIndex] = updatedTicket localStorage.setItem('buildmate-support-tickets', JSON.stringify(tickets)) } } catch (e) { console.error('Error updating ticket in localStorage:', e) } } } setNewReply("") setIsSubmitting(false) } const getStatusIcon = (status: string) => { const statusOption = statusOptions.find(s => s.value === status) const Icon = statusOption?.icon || Clock return } const getPriorityColor = (priority: string) => { const level = priorityLevels.find(p => p.value === priority) return level?.color || "bg-gray-100 text-gray-800" } const getStatusColor = (status: string) => { const option = statusOptions.find(s => s.value === status) return option?.color || "bg-gray-100 text-gray-800" } const getTypeIcon = (type: string) => { const typeOption = supportTypes.find(t => t.value === type) const Icon = typeOption?.icon || HelpCircle return } if (isLoading) { return (

Loading ticket details...

) } if (!ticket) { return (

Ticket not found

The ticket you're looking for doesn't exist or has been removed.

) } return (
{/* Page Header */}

Ticket Details

Ticket ID: {ticket.id}

{/* Main Content */}
{/* Ticket Header */}

{ticket.title}

{priorityLevels.find(p => p.value === ticket.priority)?.label} {getStatusIcon(ticket.status)} {statusOptions.find(s => s.value === ticket.status)?.label}
{supportTypes.find(t => t.value === ticket.type)?.label}
{ticket.assignedTo}
Created: {ticket.createdAt}
Updated: {ticket.updatedAt}

Description

{ticket.description}

{/* Replies Section */} Conversation ({ticket.replies?.length || 0} replies)
{ticket.replies && ticket.replies.length > 0 ? ( ticket.replies.map((reply: any, index: number) => (
{reply.author} {new Date(reply.createdAt).toLocaleString()}

{reply.message}

{index < ticket.replies.length - 1 && }
)) ) : (

No replies yet. Start the conversation!

)}
{/* Reply Form */} Add Reply Respond to this ticket