import { useState, useEffect } from "react"; import { useForm } from "@inertiajs/react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/Components/ui/dialog"; import { Button } from "@/Components/ui/button"; import { Input } from "@/Components/ui/input"; import { Label } from "@/Components/ui/label"; import { Textarea } from "@/Components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/Components/ui/select"; import { WarehouseInventory } from "@/types/warehouse"; import { toast } from "sonner"; import { Minus, Plus, Equal } from "lucide-react"; interface InventoryAdjustmentDialogProps { warehouseId: string; item: WarehouseInventory | null; isOpen: boolean; onClose: () => void; } type Operation = "add" | "subtract" | "set"; export default function InventoryAdjustmentDialog({ warehouseId, item, isOpen, onClose, }: InventoryAdjustmentDialogProps) { const [operation, setOperation] = useState("add"); const { data, setData, put, processing, reset, errors } = useForm({ quantity: 0, reason: "盤點調整", notes: "", operation: "add" as Operation, type: "adjustment", // 預設類型 }); // 重新開放時重置 useEffect(() => { if (isOpen) { reset(); setOperation("add"); } }, [isOpen]); useEffect(() => { setData("operation", operation); }, [operation]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!item) return; put(route("warehouses.inventory.update", { warehouse: warehouseId, product: item.productId // 這裡後端接收 product (或可用 inventory ID 擴充) }), { onSuccess: () => { onClose(); }, onError: () => { toast.error("調整失敗,請檢查欄位資料"); } }); }; if (!item) return null; // 計算剩餘庫存預覽 const getResultQuantity = () => { const inputQty = Number(data.quantity) || 0; switch (operation) { case "add": return item.quantity + inputQty; case "subtract": return Math.max(0, item.quantity - inputQty); case "set": return inputQty; default: return item.quantity; } }; return ( 手動庫存調整 調整商品:{item.productName} (批號: {item.batchNumber || "無"})
{/* 現有庫存 */}
現有庫存 {item.quantity}
{/* 調整方式 */}
{/* 調整數量 */}
setData("quantity", Number(e.target.value))} placeholder="請輸入數量" className={errors.quantity ? "border-red-500" : ""} /> {errors.quantity &&

{errors.quantity}

}
{/* 預計剩餘庫存 */}
調整後庫存 {getResultQuantity()}
{/* 調整原因 */}
{/* 備註 */}