/** * 採購單列表表格 */ import { useState, useMemo } from "react"; import { ArrowUpDown, ArrowUp, ArrowDown } from "lucide-react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/Components/ui/table"; import { PurchaseOrderActions } from "./PurchaseOrderActions"; import PurchaseOrderStatusBadge from "./PurchaseOrderStatusBadge"; import CopyButton from "@/Components/shared/CopyButton"; import type { PurchaseOrder } from "@/types/purchase-order"; import { formatCurrency, formatDateTime } from "@/utils/format"; import { STATUS_CONFIG } from "@/constants/purchase-order"; interface PurchaseOrderTableProps { orders: PurchaseOrder[]; } type SortField = "poNumber" | "warehouse_name" | "supplierName" | "createdAt" | "totalAmount" | "status"; type SortDirection = "asc" | "desc" | null; export default function PurchaseOrderTable({ orders, }: PurchaseOrderTableProps) { const [sortField, setSortField] = useState(null); const [sortDirection, setSortDirection] = useState(null); // 處理排序 const handleSort = (field: SortField) => { if (sortField === field) { if (sortDirection === "asc") { setSortDirection("desc"); } else if (sortDirection === "desc") { setSortDirection(null); setSortField(null); } else { setSortDirection("asc"); } } else { setSortField(field); setSortDirection("asc"); } }; // 排序後的訂單列表 const sortedOrders = useMemo(() => { if (!sortField || !sortDirection) { return orders; } return [...orders].sort((a, b) => { let aValue: string | number; let bValue: string | number; switch (sortField) { case "poNumber": aValue = a.poNumber; bValue = b.poNumber; break; case "warehouse_name": aValue = a.warehouse_name || ""; bValue = b.warehouse_name || ""; break; case "supplierName": aValue = a.supplierName; bValue = b.supplierName; break; case "createdAt": aValue = a.createdAt; bValue = b.createdAt; break; case "totalAmount": aValue = a.totalAmount; bValue = b.totalAmount; break; case "status": aValue = STATUS_CONFIG[a.status].label; bValue = STATUS_CONFIG[b.status].label; break; default: return 0; } if (typeof aValue === "string" && typeof bValue === "string") { return sortDirection === "asc" ? aValue.localeCompare(bValue, "zh-TW") : bValue.localeCompare(aValue, "zh-TW"); } else { return sortDirection === "asc" ? (aValue as number) - (bValue as number) : (bValue as number) - (aValue as number); } }); }, [orders, sortField, sortDirection]); const SortIcon = ({ field }: { field: SortField }) => { if (sortField !== field) { return ; } if (sortDirection === "asc") { return ; } if (sortDirection === "desc") { return ; } return ; }; return (
# 操作 {sortedOrders.length === 0 ? ( 尚無採購單 ) : ( sortedOrders.map((order, index) => ( {index + 1}
{order.poNumber}
{order.warehouse_name}
{order.createdBy}
{order.supplierName} {formatDateTime(order.createdAt)} {formatCurrency(order.totalAmount)}
)) )}
); }