/** * 廠商詳細資訊頁面 */ import { useState } 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 { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/Components/ui/alert-dialog"; import SupplyProductList from "@/Components/Vendor/SupplyProductList"; import AddSupplyProductDialog from "@/Components/Vendor/AddSupplyProductDialog"; import EditSupplyProductDialog from "@/Components/Vendor/EditSupplyProductDialog"; import type { Vendor } from "@/Pages/Vendor/Index"; import type { SupplyProduct } from "@/types/vendor"; import { getShowBreadcrumbs } from "@/utils/breadcrumb"; interface Pivot { last_price: number | null; } interface VendorProduct { id: number; name: string; unit?: string; // Relations might be camelCase or snake_case depending on serialization settings baseUnit?: { name: string }; base_unit?: { name: string }; largeUnit?: { name: string }; large_unit?: { name: string }; purchaseUnit?: string; // Note: if it's a relation it might be an object, but original code treated it as 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 }: ShowProps) { const [showAddDialog, setShowAddDialog] = useState(false); const [showEditDialog, setShowEditDialog] = useState(false); const [showRemoveDialog, setShowRemoveDialog] = useState(false); const [selectedProduct, setSelectedProduct] = useState(null); // 轉換後端資料格式為前端組件需要的格式 const supplyProducts: SupplyProduct[] = vendor.products.map(p => { // Laravel load('relationName') usually results in camelCase key in JSON if method is camelCase const baseUnitName = p.baseUnit?.name || p.base_unit?.name; const largeUnitName = p.largeUnit?.name || p.large_unit?.name; // Check purchase unit - seemingly originally a field string, but if relation, check if object // Assuming purchase_unit is a string field on product table here based on original code usage? // Wait, original code usage: p.purchase_unit || ... // In Product model: purchase_unit_id exists, purchaseUnit is relation. // If p.purchase_unit was working before, it might be an attribute (accessors). // Let's stick to safe access. return { id: String(p.id), 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, }; }); const handleAddProduct = (productId: string, lastPrice?: number) => { router.post(route('vendors.products.store', vendor.id), { product_id: productId, last_price: lastPrice, }, { onSuccess: () => setShowAddDialog(false), }); }; const handleEditProduct = (product: SupplyProduct) => { setSelectedProduct(product); setShowEditDialog(true); }; const handleUpdateProduct = (productId: string, lastPrice?: number) => { router.put(route('vendors.products.update', [vendor.id, productId]), { last_price: lastPrice, }, { onSuccess: () => { setShowEditDialog(false); setSelectedProduct(null); } }); }; const handleRemoveProduct = (product: SupplyProduct) => { setSelectedProduct(product); setShowRemoveDialog(true); }; const handleConfirmRemove = () => { if (selectedProduct) { router.delete(route('vendors.products.destroy', [vendor.id, selectedProduct.productId]), { onSuccess: () => { setShowRemoveDialog(false); setSelectedProduct(null); } }); } }; return (
{/* 返回按鈕 */}

廠商詳細資訊

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

{/* 基本資料 */}

基本資料

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

{vendor.short_name || "-"}

{vendor.tax_id || "-"}

{vendor.owner || "-"}

{vendor.remark || "-"}

{/* 聯絡資料 */}

聯絡資料

{vendor.contact_name || "-"}

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

{vendor.email || "-"}

{vendor.address || "-"}

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

供貨商品

{/* 新增供貨商品對話框 */} setShowAddDialog(false)} onAdd={handleAddProduct} /> {/* 編輯供貨商品對話框 */} { setShowEditDialog(false); setSelectedProduct(null); }} onSave={handleUpdateProduct} /> {/* 取消供貨確認對話框 */} 確認取消供貨 確定要將「{selectedProduct?.productName}」從供貨列表中移除嗎?此操作無法撤銷。 取消 確認移除
); }