"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, AlertTriangle } from "lucide-react" import type { Product } from "@/types/business" interface ProductsTableProps { products: Product[] onEdit: (product: Product) => void onDelete: (productId: string) => void } export function ProductsTable({ products, onEdit, onDelete }: ProductsTableProps) { const [searchTerm, setSearchTerm] = useState("") const [deleteProduct, setDeleteProduct] = useState(null) const filteredProducts = products.filter( (product) => product.name.toLowerCase().includes(searchTerm.toLowerCase()) || product.category.toLowerCase().includes(searchTerm.toLowerCase()) || product.sku.toLowerCase().includes(searchTerm.toLowerCase()), ) const getStockStatus = (stock: number, minStock: number) => { if (stock === 0) return { label: "Out of Stock", variant: "destructive" as const } if (stock <= minStock) return { label: "Low Stock", variant: "secondary" as const } return { label: "In Stock", variant: "default" as const } } const handleDelete = (product: Product) => { onDelete(product.id) setDeleteProduct(null) } return (
{/* Search */}
setSearchTerm(e.target.value)} className="pl-10" />
{/* Products Table */}
Product Category SKU Buy Price Sell Price Stock Status {filteredProducts.length === 0 ? ( {searchTerm ? "No products found matching your search." : "No products added yet."} ) : ( filteredProducts.map((product) => { const stockStatus = getStockStatus(product.stock, product.minStock) const profitMargin = (((product.sellPrice - product.buyPrice) / product.sellPrice) * 100).toFixed(1) return (

{product.name}

{product.description && (

{product.description}

)}
{product.category} {product.sku || "N/A"} ${product.buyPrice.toFixed(2)}

${product.sellPrice.toFixed(2)}

{profitMargin}% margin

{product.stock <= product.minStock && product.stock > 0 && ( )} {product.stock}
{stockStatus.label} onEdit(product)}> Edit setDeleteProduct(product)} className="text-red-600"> Delete
) }) )}
{/* Delete Confirmation Dialog */} setDeleteProduct(null)}> Delete Product Are you sure you want to delete "{deleteProduct?.name}"? This action cannot be undone. Cancel deleteProduct && handleDelete(deleteProduct)} className="bg-red-600 hover:bg-red-700" > Delete
) }