/**
* 採購單商品表格元件
*/
import { Trash2 } from "lucide-react";
import { Button } from "@/Components/ui/button";
import { Input } from "@/Components/ui/input";
import { SearchableSelect } from "@/Components/ui/searchable-select";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/Components/ui/table";
import type { PurchaseOrderItem, Supplier } from "@/types/purchase-order";
import { formatCurrency } from "@/utils/purchase-order";
interface PurchaseOrderItemsTableProps {
items: PurchaseOrderItem[];
supplier?: Supplier;
isReadOnly?: boolean;
isDisabled?: boolean;
onAddItem?: () => void;
onRemoveItem?: (index: number) => void;
onItemChange?: (index: number, field: keyof PurchaseOrderItem, value: string | number) => void;
}
export function PurchaseOrderItemsTable({
items,
supplier,
isReadOnly = false,
isDisabled = false,
onRemoveItem,
onItemChange,
}: PurchaseOrderItemsTableProps) {
return (
商品名稱
數量
單位
換算基本單位
小計
單價 / 基本單位
{!isReadOnly && }
{items.length === 0 ? (
{isDisabled ? "請先選擇供應商後才能新增商品" : "尚未新增任何商品項"}
) : (
items.map((item, index) => {
// 計算換算後的單價 (基本單位單價)
// unitPrice is derived from subtotal / quantity
const currentUnitPrice = item.unitPrice;
const convertedUnitPrice = item.selectedUnit === 'large' && item.conversion_rate
? currentUnitPrice / item.conversion_rate
: currentUnitPrice;
return (
{/* 商品選擇 */}
{isReadOnly ? (
{item.productName}
) : (
onItemChange?.(index, "productId", value)
}
disabled={isDisabled}
options={supplier?.commonProducts.map((p) => ({ label: p.productName, value: p.productId })) || []}
placeholder="選擇商品"
searchPlaceholder="搜尋商品..."
emptyText="無可用商品"
className="w-full"
/>
)}
{/* 數量 */}
{isReadOnly ? (
{Math.floor(item.quantity)}
) : (
onItemChange?.(index, "quantity", Math.floor(Number(e.target.value)))
}
disabled={isDisabled}
className="h-10 text-left border-gray-200 w-24"
/>
)}
{/* 單位選擇 */}
{!isReadOnly && item.large_unit_id ? (
onItemChange?.(index, "selectedUnit", value)
}
disabled={isDisabled}
options={[
{ label: item.base_unit_name || "個", value: "base" },
{ label: item.large_unit_name || "", value: "large" }
]}
className="w-24"
/>
) : (
{item.selectedUnit === 'large' && item.large_unit_name
? item.large_unit_name
: (item.base_unit_name || "個")}
)}
{/* 換算基本單位 */}
{item.selectedUnit === 'large' && item.conversion_rate
? item.quantity * item.conversion_rate
: item.quantity}
{item.base_unit_name || "個"}
{/* 總金額 (主要輸入欄位) */}
{isReadOnly ? (
{formatCurrency(item.subtotal)}
) : (
onItemChange?.(index, "subtotal", Number(e.target.value))
}
disabled={isDisabled}
className={`h-10 text-left w-32 ${
// 如果有數量但沒有金額,顯示錯誤樣式
item.quantity > 0 && (!item.subtotal || item.subtotal <= 0)
? "border-red-400 bg-red-50 focus-visible:ring-red-500"
: "border-gray-200"
}`}
/>
{/* 錯誤提示 */}
{item.quantity > 0 && (!item.subtotal || item.subtotal <= 0) && (
❌ 請填寫總金額
)}
)}
{/* 換算採購單價 / 基本單位 (顯示換算結果) */}
{formatCurrency(convertedUnitPrice)} / {item.base_unit_name || "個"}
{convertedUnitPrice > 0 && item.previousPrice && item.previousPrice > 0 && (
<>
{convertedUnitPrice > item.previousPrice && (
⚠️ 高於上次: {formatCurrency(item.previousPrice)}
)}
{convertedUnitPrice < item.previousPrice && (
📉 低於上次: {formatCurrency(item.previousPrice)}
)}
>
)}
{/* 刪除按鈕 */}
{!isReadOnly && onRemoveItem && (
)}
);
})
)}
);
}