import { useEffect } from "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 { SearchableSelect } from "@/Components/ui/searchable-select"; import { useForm } from "@inertiajs/react"; import { toast } from "sonner"; import { Calendar } from "lucide-react"; import { getCurrentDate } from "@/utils/format"; export interface UtilityFee { id: number; transaction_date: string; category: string; amount: number | string; invoice_number?: string; description?: string; created_at: string; updated_at: string; } interface UtilityFeeDialogProps { open: boolean; onOpenChange: (open: boolean) => void; fee: UtilityFee | null; availableCategories: string[]; } const DEFAULT_CATEGORIES = [ "電費", "水費", "瓦斯費", "電話費", "網路費", "清潔費", "管理費", ]; export default function UtilityFeeDialog({ open, onOpenChange, fee, availableCategories, }: UtilityFeeDialogProps) { const { data, setData, post, put, processing, errors, reset, clearErrors } = useForm({ transaction_date: getCurrentDate(), category: "", amount: "", invoice_number: "", description: "", }); // Combine default and available categories const categories = Array.from(new Set([...DEFAULT_CATEGORIES, ...availableCategories])); useEffect(() => { if (open) { clearErrors(); if (fee) { setData({ transaction_date: fee.transaction_date.split("T")[0].split(" ")[0], category: fee.category, amount: fee.amount.toString(), invoice_number: fee.invoice_number || "", description: fee.description || "", }); } else { reset(); setData("transaction_date", getCurrentDate()); } } }, [open, fee]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (fee) { // Validate invoice number format if present if (data.invoice_number && !/^[A-Z]{2}-\d{8}$/.test(data.invoice_number)) { toast.error("發票號碼格式錯誤,應為:AB-12345678"); return; } put(route("utility-fees.update", fee.id), { onSuccess: () => { toast.success("紀錄已更新"); onOpenChange(false); reset(); }, onError: () => { toast.error("更新失敗,請檢查輸入資料"); } }); } else { // Validate invoice number format if present if (data.invoice_number && !/^[A-Z]{2}-\d{8}$/.test(data.invoice_number)) { toast.error("發票號碼格式錯誤,應為:AB-12345678"); return; } post(route("utility-fees.store"), { onSuccess: () => { toast.success("公共事業費已記錄"); onOpenChange(false); reset(); }, onError: () => { toast.error("紀錄失敗,請檢查輸入資料"); } }); } }; return ( {fee ? "編輯費用紀錄" : "新增費用紀錄"} {fee ? "修改此筆公共事業費的詳細資訊" : "記錄一筆新的公共事業費支出"}
setData("transaction_date", e.target.value)} className={`pl-9 block w-full ${errors.transaction_date ? "border-red-500" : ""}`} required />
{errors.transaction_date &&

{errors.transaction_date}

}
setData("category", value)} options={categories.map((c) => ({ label: c, value: c }))} placeholder="選擇或輸入類別" searchPlaceholder="搜尋類別..." className={errors.category ? "border-red-500" : ""} /> {errors.category &&

{errors.category}

}
setData("amount", e.target.value)} placeholder="0.00" className={errors.amount ? "border-red-500" : ""} required /> {errors.amount &&

{errors.amount}

}
setData("invoice_number", e.target.value)} placeholder="例:AB-12345678" />

格式範例:AB-12345678

{errors.invoice_number &&

{errors.invoice_number}

}