"use client"; import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { useParams, useRouter } from "next/navigation"; import { ArrowLeft, Wifi, FileText, Ticket, RefreshCw } from "lucide-react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/Card"; import { Badge } from "@/components/ui/Badge"; import { Button } from "@/components/ui/Button"; import { Table, TableHead, TableBody, TableRow, Th, Td, EmptyState } from "@/components/ui/Table"; import { formatDate, formatCurrency } from "@/lib/utils"; import api from "@/lib/api"; import type { Client, Subscription, Invoice, Ticket as TicketType, PaginatedResponse, LegacyPaginatedResponse } from "@/types"; type Tab = "profile" | "subscriptions" | "invoices" | "tickets"; const statusColor: Record = { active: "success", ACTIVE: "success", suspended: "warning", SUSPENDED: "warning", cancelled: "danger", CANCELLED: "danger", disconnected: "danger", pending: "muted", PENDING: "muted", }; export default function ClientDetailPage() { const { id } = useParams<{ id: string }>(); const router = useRouter(); const [activeTab, setActiveTab] = useState("profile"); const { data: client, isLoading, refetch: refetchClient } = useQuery({ queryKey: ["client", id], queryFn: async () => { const res = await api.get(`/api/v1/clients/${id}`); return res.data; }, }); const { data: subscriptions, isError: subsError } = useQuery({ queryKey: ["client-subscriptions", id], queryFn: async () => { const res = await api.get(`/api/v1/clients/${id}/subscriptions`); return Array.isArray(res.data) ? res.data : []; }, enabled: activeTab === "subscriptions", retry: false, }); const { data: invoicesData } = useQuery>({ queryKey: ["client-invoices", id], queryFn: async () => { const res = await api.get>(`/api/v1/invoices?clientId=${id}&page=1&limit=20`); return res.data; }, enabled: activeTab === "invoices", }); const { data: ticketsData } = useQuery>({ queryKey: ["client-tickets", id], queryFn: async () => { const res = await api.get>(`/api/v1/tickets?clientId=${id}&page=1&limit=20`); return res.data; }, enabled: activeTab === "tickets", }); const tabs: { key: Tab; label: string; icon: React.ComponentType<{ className?: string }> }[] = [ { key: "profile", label: "Profile", icon: ArrowLeft }, { key: "subscriptions", label: "Subscriptions", icon: Wifi }, { key: "invoices", label: "Invoices", icon: FileText }, { key: "tickets", label: "Tickets", icon: Ticket }, ]; if (isLoading) { return (
); } if (!client) { return (

Client not found

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

{client.firstName} {client.lastName}

{client.accountNumber} • {client.area?.name ?? ""}

{client.isActive ? "Active" : "Inactive"}
{/* Tabs */}
{[ { key: "profile" as Tab, label: "Profile" }, { key: "subscriptions" as Tab, label: "Subscriptions" }, { key: "invoices" as Tab, label: "Invoices" }, { key: "tickets" as Tab, label: "Tickets" }, ].map((tab) => ( ))}
{/* Profile Tab */} {activeTab === "profile" && ( Client Profile
{[ { label: "Account Number", value: client.accountNumber }, { label: "Full Name", value: `${client.firstName} ${client.lastName}` }, { label: "Email", value: client.email }, { label: "Phone", value: client.phone }, { label: "Address", value: client.address || "—" }, { label: "Area", value: client.area?.name || "—" }, { label: "Status", value: client.isActive ? "Active" : "Inactive" }, { label: "Joined", value: formatDate(client.createdAt) }, ].map(({ label, value }) => (
{label}
{value}
))}
)} {/* Subscriptions Tab */} {activeTab === "subscriptions" && ( Subscriptions {subsError ? ( ) : !subscriptions || subscriptions.length === 0 ? ( ) : ( subscriptions.map((sub) => ( )) )}
Plan Status Start Date Monthly Rate
No data yet
{sub.plan?.name ?? sub.planId} {sub.status} {formatDate(sub.startDate)} {formatCurrency(sub.plan?.monthlyPrice ?? sub.monthlyRate ?? sub.mrc ?? 0)}
)} {/* Invoices Tab */} {activeTab === "invoices" && ( Invoices {!invoicesData?.data || invoicesData.data.length === 0 ? ( ) : ( invoicesData.data.map((inv) => ( )) )}
Invoice # Amount Due Date Status {inv.invoiceNumber ?? inv.id.slice(0, 8)} {formatCurrency(inv.amount ?? inv.totalAmount ?? 0)} {formatDate(inv.dueDate)} {inv.status}
)} {/* Tickets Tab */} {activeTab === "tickets" && ( Tickets {!ticketsData?.data || ticketsData.data.length === 0 ? ( ) : ( ticketsData.data.map((ticket) => ( )) )}
Subject Type Priority Status Created {ticket.subject} {ticket.type} {ticket.priority} {ticket.status} {formatDate(ticket.createdAt)}
)}
); }