import { Head, router } from '@inertiajs/react'; import { Plus } from 'lucide-react'; import { useMemo, useState } from 'react'; import { CardTable } from '@/components/card-table'; import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog'; import { FilterPopover } from '@/components/filter-popover'; import { useCardTableExpand } from '@/components/hooks/use-card-table-expand'; import { PageHeader } from '@/components/page-header'; import { Button } from '@/components/ui/button'; import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList, } from '@/components/ui/combobox'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { useServerTable } from '@/hooks/use-server-table'; import { destroy, create as transactionCreate, index as transactionIndex, edit as transactionEdit, } from '@/routes/admin/manage/transactions'; import type { Transaction } from './columns'; import { TransactionCardRow } from './transaction-card'; import { TransactionItemSubRow } from './transaction-sub-row'; type FilterOption = { id: number; name: string; }; type Props = { transactions: { data: Transaction[]; current_page: number; last_page: number; per_page: number; total: number; }; filters: { status?: string; channel?: string; payment_type?: string; customer_id?: string; marketing_id?: string; created_by_id?: string; }; filterOptions: { customers: FilterOption[]; employees: FilterOption[]; }; }; export default function TransactionIndex({ transactions, filters, filterOptions, }: Props) { const [deleting, setDeleting] = useState(null); const expand = useCardTableExpand(true); const pagination = { current_page: transactions.current_page, last_page: transactions.last_page, per_page: transactions.per_page, total: transactions.total, }; const { search, filterOpen, setFilterOpen, handlePageChange, handlePerPageChange, handleSearchChange, applyFilter, clearFilters, } = useServerTable({ route: () => transactionIndex.url(), pagination, filters, }); const selectedCustomer = useMemo( () => filterOptions.customers.find( (c) => String(c.id) === filters.customer_id, ) ?? null, [filterOptions.customers, filters.customer_id], ); const selectedMarketing = useMemo( () => filterOptions.employees.find( (e) => String(e.id) === filters.marketing_id, ) ?? null, [filterOptions.employees, filters.marketing_id], ); const selectedEmployee = useMemo( () => filterOptions.employees.find( (e) => String(e.id) === filters.created_by_id, ) ?? null, [filterOptions.employees, filters.created_by_id], ); function handleDelete() { if (!deleting) { return; } router.delete(destroy.url(deleting.id), { onSuccess: () => setDeleting(null), }); } const filterToolbar = (
c.name} value={selectedCustomer} onValueChange={(value) => applyFilter( 'customer_id', value ? String(value.id) : '', ) } > Tidak ada pelanggan ditemukan. {(customer) => ( {customer.name} )}
e.name} value={selectedMarketing} onValueChange={(value) => applyFilter( 'marketing_id', value ? String(value.id) : '', ) } > Tidak ada marketing ditemukan. {(employee) => ( {employee.name} )}
e.name} value={selectedEmployee} onValueChange={(value) => applyFilter( 'created_by_id', value ? String(value.id) : '', ) } > Tidak ada pegawai ditemukan. {(employee) => ( {employee.name} )}
); return ( <>
Tambah } /> t.id} expandedKeys={expand.expandedKeys} onToggleExpand={expand.toggleExpand} searchValue={search} onSearchChange={handleSearchChange} searchPlaceholder="Cari berdasarkan produk atau nomor transaksi..." toolbar={filterToolbar} pagination={pagination} onPageChange={handlePageChange} onPerPageChange={handlePerPageChange} renderCard={({ item, index, isExpanded, onToggleExpand, }) => ( { window.location.href = transactionEdit.url(t.id); }} onDelete={(t) => setDeleting(t)} /> )} renderSubContent={(transaction) => ( )} /> { if (!open) { setDeleting(null); } }} title="Hapus Transaksi" description="Apakah Anda yakin ingin menghapus transaksi ini? Stok akan dikembalikan. Tindakan ini tidak dapat dibatalkan." onConfirm={handleDelete} />
); }