/** * 編輯配方頁面 */ import { useEffect } from "react"; import { BookOpen, Plus, Trash2, ArrowLeft, Save } from 'lucide-react'; import { Button } from "@/Components/ui/button"; import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout"; import { Head, useForm, Link } from "@inertiajs/react"; import { getBreadcrumbs } from "@/utils/breadcrumb"; import { SearchableSelect } from "@/Components/ui/searchable-select"; import { Input } from "@/Components/ui/input"; import { Label } from "@/Components/ui/label"; import { Textarea } from "@/Components/ui/textarea"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/Components/ui/table"; interface Product { id: number; name: string; code: string; base_unit_id?: number; large_unit_id?: number; } interface Unit { id: number; name: string; } // Backend Model Structure interface RecipeItemModel { id: number; product_id: number; quantity: number; unit_id: number; remark: string | null; product?: Product; unit?: Unit; } interface RecipeModel { id: number; product_id: number; code: string; name: string; description: string | null; yield_quantity: number; items: RecipeItemModel[]; product?: Product; } // Form State Structure interface RecipeItemForm { product_id: string; quantity: string; unit_id: string; remark: string; // UI Helpers ui_product_name?: string; ui_product_code?: string; ui_unit_name?: string; } interface Props { recipe: RecipeModel; products: Product[]; units: Unit[]; } export default function RecipeEdit({ recipe, products, units }: Props) { const { data, setData, put, processing, errors } = useForm({ product_id: String(recipe.product_id), code: recipe.code, name: recipe.name, description: recipe.description || "", yield_quantity: String(recipe.yield_quantity), items: recipe.items.map(item => ({ product_id: String(item.product_id), quantity: String(item.quantity), unit_id: String(item.unit_id), remark: item.remark || "", ui_product_name: item.product?.name, ui_product_code: item.product?.code, ui_unit_name: item.unit?.name })) as RecipeItemForm[], }); // 自動產生配方名稱 (當選擇商品時) - 僅在名稱為空時觸發,避免覆蓋舊資料 useEffect(() => { if (data.product_id && !data.name) { const product = products.find(p => String(p.id) === data.product_id); if (product) { setData(d => ({ ...d, name: `${product.name} 標準配方` })); } } }, [data.product_id]); const addItem = () => { setData('items', [ ...data.items, { product_id: "", quantity: "1", unit_id: "", remark: "" } ]); }; const removeItem = (index: number) => { setData('items', data.items.filter((_, i) => i !== index)); }; const updateItem = (index: number, field: keyof RecipeItemForm, value: string) => { const newItems = [...data.items]; newItems[index] = { ...newItems[index], [field]: value }; // Auto-fill unit when product selected if (field === 'product_id') { const product = products.find(p => String(p.id) === value); if (product) { newItems[index].ui_product_name = product.name; newItems[index].ui_product_code = product.code; // Default to base unit if not set if (product.base_unit_id) { newItems[index].unit_id = String(product.base_unit_id); const unit = units.find(u => u.id === product.base_unit_id); newItems[index].ui_unit_name = unit?.name; } } } setData('items', newItems); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); put(route('recipes.update', recipe.id)); }; return (

編輯配方

修改 {recipe.name} ({recipe.code})

{/* 左側:基本資料 */}

基本資料

setData('product_id', v)} options={products.map(p => ({ label: `${p.name} (${p.code})`, value: String(p.id), }))} placeholder="選擇商品" className="w-full" /> {errors.product_id &&

{errors.product_id}

}
setData('code', e.target.value)} placeholder="例如: REC-P001" /> {errors.code &&

{errors.code}

}
setData('name', e.target.value)} placeholder="例如: 草莓冰標準配方" /> {errors.name &&

{errors.name}

}
setData('yield_quantity', e.target.value)} placeholder="1" />
{errors.yield_quantity &&

{errors.yield_quantity}

}