import { useState, useEffect } from "react"; import { Head, Link, router } from "@inertiajs/react"; import { Phone, Mail, Plus, ArrowLeft, Contact2 } from "lucide-react"; import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout"; import { Label } from "@/Components/ui/label"; import { Button } from "@/Components/ui/button"; import SupplyProductList from "@/Components/Vendor/SupplyProductList"; import type { Vendor } from "@/Pages/Vendor/Index"; import type { SupplyProduct } from "@/types/vendor"; import { getShowBreadcrumbs } from "@/utils/breadcrumb"; import { toast } from "sonner"; interface Pivot { last_price: number | null; } interface VendorProduct { id: number; name: string; unit?: string; baseUnit?: { name: string }; base_unit?: { name: string }; largeUnit?: { name: string }; large_unit?: { name: string }; purchaseUnit?: string; purchase_unit?: string; conversion_rate?: number; pivot: Pivot; } interface ExtendedVendor extends Vendor { products: VendorProduct[]; } interface ShowProps { vendor: ExtendedVendor; products: any[]; } export default function VendorShow({ vendor, products: allProducts }: ShowProps) { const [items, setItems] = useState([]); // 初始化資料 useEffect(() => { const initialItems: SupplyProduct[] = vendor.products.map(p => { const baseUnitName = p.baseUnit?.name || p.base_unit?.name; const largeUnitName = p.largeUnit?.name || p.large_unit?.name; return { id: String(p.id) + "_" + Math.random().toString(36).substr(2, 9), // 加上隨機碼以確保 key 唯一 productId: String(p.id), productName: p.name, unit: p.purchase_unit || baseUnitName || "個", baseUnit: baseUnitName, largeUnit: largeUnitName, conversionRate: p.conversion_rate, lastPrice: p.pivot.last_price || undefined, }; }); setItems(initialItems); }, [vendor.products]); const handleAddItem = () => { const newItem: SupplyProduct = { id: "new_" + Math.random().toString(36).substr(2, 9), productId: "", productName: "", unit: "個", lastPrice: undefined, }; setItems([...items, newItem]); }; const handleRemoveItem = (index: number) => { const newItems = [...items]; newItems.splice(index, 1); setItems(newItems); }; const handleItemChange = (index: number, field: keyof SupplyProduct, value: any) => { const newItems = [...items]; const item = { ...newItems[index] }; if (field === "productId") { const product = allProducts.find(p => String(p.id) === String(value)); if (product) { item.productId = String(product.id); item.productName = product.name; item.baseUnit = product.baseUnit?.name || product.base_unit?.name || product.base_unit || "個"; item.largeUnit = product.largeUnit?.name || product.large_unit?.name; item.conversionRate = product.conversion_rate; item.unit = item.baseUnit || "個"; } else { item.productId = value; item.productName = ""; } } else { (item as any)[field] = value; } newItems[index] = item; setItems(newItems); }; const handleSaveAll = () => { // 過濾掉沒有選擇商品的項目 const validItems = items.filter(item => item.productId !== ""); if (validItems.length === 0 && items.length > 0) { toast.error("請至少選擇一個有效的商品,或移除空白列"); return; } // 檢查重複商品 const productIds = validItems.map(i => i.productId); if (new Set(productIds).size !== productIds.length) { toast.error("供貨清單中有重複的商品,請檢查"); return; } router.put(route('vendors.products.sync', vendor.id), { products: validItems.map(item => ({ product_id: item.productId, last_price: item.lastPrice, })) }, { onSuccess: () => { toast.success("供貨商品已更新"); }, onError: () => { toast.error("更新失敗,請檢查欄位格式"); } }); }; return (
{/* 返回按鈕 */}

廠商詳細資訊

查看並管理供應商的詳細資料與供貨商品

{/* 基本資料 */}

基本資料

{vendor.name} ({vendor.code})

{vendor.shortName || "-"}

{vendor.taxId || "-"}

{vendor.owner || "-"}

{vendor.remark || "-"}

{/* 聯絡資料 */}

聯絡資料

{vendor.contactName || "-"}

{vendor.phone || vendor.tel || "-"}

{vendor.email || "-"}

{vendor.address || "-"}

{/* 供貨商品列表 */}

供貨商品

{/* 底部按鈕 - 移至容器外 */}
); }