/** * 採購單商品表格元件 */ 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 { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/Components/ui/alert-dialog"; 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) => { // 計算換算後的單價 (基本單位單價) // 單價由 小計 / 數量 推導得出 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="text-left 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 || "個"}
{/* 換算採購單價 / 基本單位 (顯示換算結果 - SWAPPED HERE) */}
{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)}

)} )}
{/* 總金額 (主要輸入欄位 - SWAPPED HERE) */} {isReadOnly ? ( {formatCurrency(item.subtotal)} ) : (
onItemChange?.(index, "subtotal", Number(e.target.value)) } disabled={isDisabled} className={`text-left w-32 ${ // 如果有數量但沒有金額,顯示錯誤樣式 item.quantity > 0 && (!item.subtotal || item.subtotal <= 0) ? "border-red-400 bg-red-50 focus-visible:ring-red-500" : "" }`} /> {/* 錯誤提示 */} {item.quantity > 0 && (!item.subtotal || item.subtotal <= 0) && (

❌ 請填寫總金額

)}
)}
{/* 刪除按鈕 */} {!isReadOnly && onRemoveItem && ( 確定要移除此商品嗎? 此動作將從清單中移除該商品,您之後需要重新搜尋才能再次加入。 取消 onRemoveItem(index)} className="bg-red-600 hover:bg-red-700" > 確定移除 )}
); }) )}
); }