import { useState } from "react"; import { Button } from "@/Components/ui/button"; import { Input } from "@/Components/ui/input"; import { SearchableSelect } from "@/Components/ui/searchable-select"; import { Plus, Search, X, Pencil, Trash2, FileText, Calendar, Filter, ArrowUpDown, ArrowUp, ArrowDown } from 'lucide-react'; import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout"; import { Head, router } from "@inertiajs/react"; import Pagination from "@/Components/shared/Pagination"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/Components/ui/table"; import { Badge } from "@/Components/ui/badge"; import { toast } from "sonner"; import UtilityFeeDialog, { UtilityFee } from "@/Components/UtilityFee/UtilityFeeDialog"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/Components/ui/alert-dialog"; import { Can } from "@/Components/Permission/Can"; import { formatDateWithDayOfWeek, formatInvoiceNumber } from "@/utils/format"; interface PageProps { fees: { data: UtilityFee[]; links: any[]; from: number; to: number; total: number; current_page: number; last_page: number; per_page: number; }; availableCategories: string[]; filters: { search?: string; category?: string; date_start?: string; date_end?: string; sort_field?: string | null; sort_direction?: "asc" | "desc" | null; per_page?: string; }; } export default function UtilityFeeIndex({ fees, availableCategories, filters }: PageProps) { const [searchTerm, setSearchTerm] = useState(filters.search || ""); const [categoryFilter, setCategoryFilter] = useState(filters.category || "all"); const [dateStart, setDateStart] = useState(filters.date_start || ""); const [dateEnd, setDateEnd] = useState(filters.date_end || ""); const [isDialogOpen, setIsDialogOpen] = useState(false); const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [editingFee, setEditingFee] = useState(null); const [deletingFeeId, setDeletingFeeId] = useState(null); // Sorting const [sortField, setSortField] = useState(filters.sort_field || 'transaction_date'); const [sortDirection, setSortDirection] = useState<"asc" | "desc" | null>(filters.sort_direction as "asc" | "desc" || 'desc'); const handleSearch = () => { router.get( route("utility-fees.index"), { search: searchTerm, category: categoryFilter, date_start: dateStart, date_end: dateEnd, sort_field: sortField, sort_direction: sortDirection, }, { preserveState: true } ); }; const handleClearFilters = () => { setSearchTerm(""); setCategoryFilter("all"); setDateStart(""); setDateEnd(""); router.get(route("utility-fees.index")); }; const handleSort = (field: string) => { let newField: string | null = field; let newDirection: "asc" | "desc" | null = "asc"; if (sortField === field) { if (sortDirection === "asc") { newDirection = "desc"; } else { newDirection = null; newField = null; } } setSortField(newField); setSortDirection(newDirection); router.get( route("utility-fees.index"), { search: searchTerm, category: categoryFilter, date_start: dateStart, date_end: dateEnd, sort_field: newField, sort_direction: newDirection, }, { preserveState: true } ); }; const openAddDialog = () => { setEditingFee(null); setIsDialogOpen(true); }; const openEditDialog = (fee: UtilityFee) => { setEditingFee(fee); setIsDialogOpen(true); }; const confirmDelete = (id: number) => { setDeletingFeeId(id); setIsDeleteDialogOpen(true); }; const handleDelete = () => { if (deletingFeeId) { router.delete(route("utility-fees.destroy", deletingFeeId), { onSuccess: () => { toast.success("紀錄已刪除"); setIsDeleteDialogOpen(false); }, }); } }; const SortIcon = ({ field }: { field: string }) => { if (sortField !== field) { return ; } if (sortDirection === "asc") { return ; } if (sortDirection === "desc") { return ; } return ; }; return (

公共事業費管理

管理店鋪水、電、瓦斯等各項公共事業費用支出

{/* Toolbar */}
{/* Search */}
setSearchTerm(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSearch()} className="pl-10" /> {searchTerm && ( )}
{/* Category Filter */} ({ label: c, value: c })) ]} placeholder="篩選類別" /> {/* Date Range Start */}
setDateStart(e.target.value)} className="pl-9 bg-white block w-full" placeholder="開始日期" />
{/* Date Range End */}
setDateEnd(e.target.value)} className="pl-9 bg-white block w-full" placeholder="結束日期" />
{/* Table */}
#
說明 / 備註 操作
{fees.data.length === 0 ? (

找不到符合條件的費用紀錄

) : ( fees.data.map((fee, index) => ( {fees.from + index} {formatDateWithDayOfWeek(fee.transaction_date)} {fee.category} $ {Number(fee.amount).toLocaleString()} {formatInvoiceNumber(fee.invoice_number)} {fee.description || '-'}
)) )}
確認刪除? 這將永久刪除此筆費用紀錄,此操作無法撤銷。 取消 確認刪除
); }