"use client"; import { useState } from "react"; import useSWR from "swr"; import Link from "next/link"; import { Card, CardContent, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Plus, Search, Pencil, Trash2, Users, Euro, FileText, ExternalLink, } from "lucide-react"; import { useTableSort } from "@/hooks/use-table-sort"; import { SortableTableHead } from "@/components/sortable-table-head"; import { CustomerDialog } from "@/components/customer-dialog"; import { toast } from "sonner"; import { formatCurrency } from "@/lib/format"; import type { Customer } from "@/lib/types"; const fetcher = (url: string) => fetch(url).then((r) => r.json()); export default function CustomersPage() { const [search, setSearch] = useState(""); const [dialogOpen, setDialogOpen] = useState(false); const [editingCustomer, setEditingCustomer] = useState(null); const [deleteTarget, setDeleteTarget] = useState(null); const { data: customers = [], mutate } = useSWR( `/api/customers?search=${encodeURIComponent(search)}`, fetcher ); const { sorted: sortedCustomers, sort, toggleSort } = useTableSort(customers); function openEdit(customer: Customer) { setEditingCustomer(customer); setDialogOpen(true); } function openCreate() { setEditingCustomer(null); setDialogOpen(true); } async function handleDelete() { if (!deleteTarget) return; try { const res = await fetch(`/api/customers/${deleteTarget.id}`, { method: "DELETE", }); if (!res.ok) { const err = await res.json(); throw new Error(err.error || "Failed to delete"); } toast.success("Customer deleted"); mutate(); } catch (err) { toast.error( err instanceof Error ? err.message : "Failed to delete customer" ); } finally { setDeleteTarget(null); } } const totalCustomers = customers.length; const totalRevenue = customers.reduce( (sum, c) => sum + Number(c.total_revenue || 0), 0 ); const totalOrders = customers.reduce( (sum, c) => sum + Number(c.order_count || 0), 0 ); return (

Customers & Orders

Manage customers and their purchase orders

{/* Summary Cards */}
Total Customers

{totalCustomers}

Total Orders

{totalOrders}

Total Revenue

{formatCurrency(totalRevenue)}

{/* Search */}
setSearch(e.target.value)} className="pl-10" />
{/* Customer Table */}
Actions {sortedCustomers.length === 0 ? ( No customers found. Click "Add Customer" to create one. ) : ( sortedCustomers.map((customer) => ( {customer.code} {customer.name} {customer.contact_person || "-"} {customer.email || "-"} {customer.phone || "-"} {Number(customer.order_count || 0)} {formatCurrency(Number(customer.total_revenue || 0))}
)) )}
mutate()} /> setDeleteTarget(null)} > Delete Customer Are you sure you want to delete "{deleteTarget?.name}"? This action cannot be undone. Cancel Delete
); }