/** * 庫存表格元件 (Warehouse 版本) * 顯示庫存項目列表(依商品分組並支援折疊) */ import { useState } from "react"; import { AlertTriangle, Trash2, Eye, ChevronDown, ChevronRight, CheckCircle, Package } from "lucide-react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/Components/ui/table"; import { Button } from "@/Components/ui/button"; import { Badge } from "@/Components/ui/badge"; import { Collapsible, CollapsibleContent, } from "@/Components/ui/collapsible"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/Components/ui/tooltip"; import { GroupedInventory } from "@/types/warehouse"; import { formatDate } from "@/utils/format"; import { Can } from "@/Components/Permission/Can"; interface InventoryTableProps { inventories: GroupedInventory[]; onView: (id: string) => void; onDelete: (id: string) => void; onViewProduct?: (productId: string) => void; } export default function InventoryTable({ inventories, onView, onDelete, onViewProduct, warehouse, }: InventoryTableProps & { warehouse: any }) { // 判斷是否為販賣機倉庫 const isVending = warehouse?.type === "vending"; // 每個商品的展開/折疊狀態 const [expandedProducts, setExpandedProducts] = useState>(new Set()); if (inventories.length === 0) { return (

無符合條件的品項

請調整搜尋或篩選條件

); } // 按商品名稱排序 const sortedInventories = [...inventories].sort((a, b) => a.productName.localeCompare(b.productName, "zh-TW") ); const toggleProduct = (productId: string) => { setExpandedProducts((prev) => { const newSet = new Set(prev); if (newSet.has(productId)) { newSet.delete(productId); } else { newSet.add(productId); } return newSet; }); }; // 獲取狀態徽章 const getStatusBadge = (status: string) => { switch (status) { case "正常": return ( 正常 ); case "低於": return ( 低於 ); default: return null; } }; return (
{sortedInventories.map((group) => { const totalQuantity = group.totalQuantity; // 使用後端提供的狀態 const status = group.status; const isLowStock = status === "低於"; const isExpanded = expandedProducts.has(group.productId); const hasInventory = group.batches.length > 0; return ( toggleProduct(group.productId)} >
{/* 商品標題 - 可點擊折疊 */}
toggleProduct(group.productId)} className={`px-4 py-3 border-b cursor-pointer hover:bg-gray-100 transition-colors ${isLowStock ? "bg-red-50" : "bg-gray-50" }`} >
{/* 折疊圖示 */} {isExpanded ? ( ) : ( )}

{group.productName} {isVending && group.batches.length > 0 && (() => { const locations = Array.from(new Set(group.batches.map(b => b.location).filter(Boolean))); return locations.length > 0 ? ( {locations.map(loc => `[${loc}]`).join('')} ) : null; })()}

{isVending ? '' : (hasInventory ? `${group.batches.length} 個批號` : '無庫存')} {group.batches.some(b => b.expiryDate && new Date(b.expiryDate) < new Date()) && ( 含過期項目 )}
總庫存:{totalQuantity} {group.baseUnit}
總價值:${group.totalValue?.toLocaleString()}
{group.safetyStock !== null ? ( <>
安全庫存:{group.safetyStock} {group.baseUnit}
{status && getStatusBadge(status)}
) : ( 未設定 )} {onViewProduct && ( )}
{/* 商品表格 - 可折疊內容 */} {hasInventory ? (
# 批號 {isVending ? "貨道" : "儲位"} 庫存數量 單位成本 總價值 保存期限 最新入庫 最新出庫 操作 {group.batches.map((batch, index) => { return ( {index + 1} {batch.batchNumber || "-"} {batch.location || "-"} {batch.quantity} {batch.unit} ${batch.unit_cost?.toLocaleString()} ${batch.total_value?.toLocaleString()} {batch.expiryDate ? (
{formatDate(batch.expiryDate)} {new Date(batch.expiryDate) < new Date() && (

此批號已過期

)}
) : "-"}
{batch.lastInboundDate ? formatDate(batch.lastInboundDate) : "-"} {batch.lastOutboundDate ? formatDate(batch.lastOutboundDate) : "-"}

{batch.quantity > 0 ? "庫存須為 0 才可刪除" : "刪除"}

); })}
) : (

此商品尚無庫存批號

請點擊「新增庫存」進行入庫

)}
); })}
); }