"use client" import { useState } from "react" 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 { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog" import { Search, MoreHorizontal, Edit, Trash2, Mail, Phone, CreditCard, Plus } from "lucide-react" import type { Supplier } from "@/types/business" interface SuppliersTableProps { suppliers: Supplier[] onEdit: (supplier: Supplier) => void onDelete: (supplierId: string) => void onAddPurchase: (supplierId: string) => void onRecordPayment: (supplierId: string) => void } export function SuppliersTable({ suppliers, onEdit, onDelete, onAddPurchase, onRecordPayment }: SuppliersTableProps) { const [searchTerm, setSearchTerm] = useState("") const [deleteSupplier, setDeleteSupplier] = useState(null) const filteredSuppliers = suppliers.filter( (supplier) => supplier.name.toLowerCase().includes(searchTerm.toLowerCase()) || supplier.email.toLowerCase().includes(searchTerm.toLowerCase()) || supplier.contactPerson.toLowerCase().includes(searchTerm.toLowerCase()) || supplier.businessType.toLowerCase().includes(searchTerm.toLowerCase()), ) const getPaymentStatus = (amountOwed: number) => { if (amountOwed === 0) return { label: "Paid Up", variant: "default" as const, color: "text-green-600" } if (amountOwed > 5000) return { label: "High Balance", variant: "secondary" as const, color: "text-red-600" } return { label: "Outstanding", variant: "secondary" as const, color: "text-yellow-600" } } const handleDelete = (supplier: Supplier) => { onDelete(supplier.id) setDeleteSupplier(null) } return (
{/* Search */}
setSearchTerm(e.target.value)} className="pl-10" />
{/* Suppliers Table */}
Supplier Contact Business Type Payment Terms Status Amount Owed {filteredSuppliers.length === 0 ? ( {searchTerm ? "No suppliers found matching your search." : "No suppliers added yet."} ) : ( filteredSuppliers.map((supplier) => { const paymentStatus = getPaymentStatus(supplier.amountOwed) return (

{supplier.name}

{supplier.contactPerson && (

{supplier.contactPerson}

)}
{supplier.email && (
{supplier.email}
)} {supplier.phone && (
{supplier.phone}
)}
{supplier.businessType || "N/A"} {supplier.paymentTerms} {paymentStatus.label}

0 ? paymentStatus.color : ""}`}> ${supplier.amountOwed.toFixed(2)}

onEdit(supplier)}> Edit onAddPurchase(supplier.id)}> Add Purchase {supplier.amountOwed > 0 && ( onRecordPayment(supplier.id)}> Record Payment )} setDeleteSupplier(supplier)} className="text-red-600"> Delete
) }) )}
{/* Delete Confirmation Dialog */} setDeleteSupplier(null)}> Delete Supplier Are you sure you want to delete "{deleteSupplier?.name}"? This action cannot be undone and will remove all associated purchase history. Cancel deleteSupplier && handleDelete(deleteSupplier)} className="bg-red-600 hover:bg-red-700" > Delete
) }