import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "./ui/table"; import { Button } from "./ui/button"; import { Badge } from "./ui/badge"; import { Pencil, Trash2, Eye, ArrowUpDown, ArrowUp, ArrowDown } from "lucide-react"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "./ui/alert-dialog"; import type { Product, ProductType, ProductUnit } from "./ProductManagement"; import { useState } from "react"; import BarcodeViewDialog from "./BarcodeViewDialog"; interface ProductTableProps { products: Product[]; onEdit: (product: Product) => void; onDelete: (id: string) => void; } type SortField = "product_code" | "name" | "type" | "unit" | "barcode_value"; type SortDirection = "asc" | "desc" | null; const productTypeLabels: Record = { raw_material: "原物料", finished_product: "半成品", }; const productUnitLabels: Record = { kg: "公斤", g: "公克", l: "公升", ml: "毫升", piece: "個", box: "盒/箱", pack: "包", bottle: "瓶", can: "罐", jar: "瓶罐", bag: "袋", basin: "盆", container: "容器", }; export default function ProductTable({ products, onEdit, onDelete, }: ProductTableProps) { const [sortField, setSortField] = useState(null); const [sortDirection, setSortDirection] = useState(null); const [barcodeDialogOpen, setBarcodeDialogOpen] = useState(false); const [selectedProduct, setSelectedProduct] = useState(null); // 排序邏輯 const handleSort = (field: SortField) => { if (sortField === field) { // 循環:asc -> desc -> null if (sortDirection === "asc") { setSortDirection("desc"); } else if (sortDirection === "desc") { setSortDirection(null); setSortField(null); } } else { setSortField(field); setSortDirection("asc"); } }; // 排序圖示 const getSortIcon = (field: SortField) => { if (sortField !== field) { return ; } if (sortDirection === "asc") { return ; } if (sortDirection === "desc") { return ; } return ; }; // 排序後的商品列表 const sortedProducts = [...products].sort((a, b) => { if (!sortField || !sortDirection) return 0; let aValue: string | number = a[sortField]; let bValue: string | number = b[sortField]; // 字串比較(不區分大小寫) if (typeof aValue === "string" && typeof bValue === "string") { aValue = aValue.toLowerCase(); bValue = bValue.toLowerCase(); } if (aValue < bValue) { return sortDirection === "asc" ? -1 : 1; } if (aValue > bValue) { return sortDirection === "asc" ? 1 : -1; } return 0; }); // 查看條碼 const handleViewBarcode = (product: Product) => { setSelectedProduct(product); setBarcodeDialogOpen(true); }; return ( <>
handleSort("product_code")} > 商品編號 {getSortIcon("product_code")} handleSort("name")} > 商品名稱 {getSortIcon("name")} handleSort("type")} > 類型 {getSortIcon("type")} handleSort("unit")} > 單位 {getSortIcon("unit")} handleSort("barcode_value")} > 商品條碼 {getSortIcon("barcode_value")} 操作 {sortedProducts.length === 0 ? ( 無符合條件的商品資料 ) : ( sortedProducts.map((product) => ( {product.product_code} {product.name} {productTypeLabels[product.type]} {productUnitLabels[product.unit]}
確認刪除 確定要刪除「{product.name}」嗎?此操作無法復原。 取消 onDelete(product.id)} className="bg-red-600 hover:bg-red-700" > 刪除
)) )}
{/* 條碼查看對話框 */} {selectedProduct && ( )} ); }