/** * 新增供貨商品對話框 */ import { useState, useMemo } from "react"; import { Button } from "@/Components/ui/button"; import { Input } from "@/Components/ui/input"; import { Label } from "@/Components/ui/label"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from "@/Components/ui/dialog"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/Components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/Components/ui/popover"; import { Check, ChevronsUpDown } from "lucide-react"; import { cn } from "@/lib/utils"; import type { Product } from "@/types/product"; import type { SupplyProduct } from "@/types/vendor"; interface AddSupplyProductDialogProps { open: boolean; products: Product[]; existingSupplyProducts: SupplyProduct[]; onClose: () => void; onAdd: (productId: string, lastPrice?: number) => void; } export default function AddSupplyProductDialog({ open, products, existingSupplyProducts, onClose, onAdd, }: AddSupplyProductDialogProps) { const [selectedProductId, setSelectedProductId] = useState(""); const [lastPrice, setLastPrice] = useState(""); const [openCombobox, setOpenCombobox] = useState(false); // 過濾掉已經在供貨列表中的商品 const availableProducts = useMemo(() => { const existingIds = new Set(existingSupplyProducts.map(sp => String(sp.productId))); return products.filter(p => !existingIds.has(String(p.id))); }, [products, existingSupplyProducts]); const selectedProduct = availableProducts.find(p => p.id === selectedProductId); const handleAdd = () => { if (!selectedProductId) return; const price = lastPrice ? parseFloat(lastPrice) : undefined; onAdd(selectedProductId, price); // 重置表單 setSelectedProductId(""); setLastPrice(""); }; const handleCancel = () => { setSelectedProductId(""); setLastPrice(""); onClose(); }; return ( 新增供貨商品 選擇該廠商可供應的商品並設定採購價格。
{/* 商品選擇 */}
找不到符合的商品 {availableProducts.map((product) => ( { setSelectedProductId(product.id); setOpenCombobox(false); }} className="cursor-pointer aria-selected:bg-primary/5 aria-selected:text-primary py-3" >
{product.name} {product.baseUnit?.name || (product.base_unit as any)?.name || product.base_unit || "個"}
))}
{/* 單位(自動帶入) */}
{selectedProduct ? (selectedProduct.baseUnit?.name || (selectedProduct.base_unit as any)?.name || selectedProduct.base_unit || "個") : "-"}
{/* 上次採購價格 */}
setLastPrice(e.target.value)} className="mt-1" />
); }