"use client" import * as React from "react" import { IconChevronDown, IconChevronLeft, IconChevronRight, IconChevronsLeft, IconChevronsRight, IconDotsVertical, IconLayoutColumns, IconSearch, } from "@tabler/icons-react" import { ColumnDef, ColumnFiltersState, flexRender, getCoreRowModel, getFacetedRowModel, getFacetedUniqueValues, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, SortingState, useReactTable, VisibilityState, } from "@tanstack/react-table" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Checkbox } from "@/components/ui/checkbox" import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" type PatientPayment = { id: string amount: number method: string status: string transactionId: string | null paidAt: Date | null createdAt: Date appointment: { service: { name: string } date: Date dentist: { name: string } } } const getPaymentBadge = (status: string) => { const variants: Record = { paid: "default", pending: "secondary", failed: "destructive", refunded: "outline", } return ( {status.toUpperCase()} ) } const getMethodBadge = (method: string) => { const labels: Record = { card: "Card", e_wallet: "E-Wallet", bank_transfer: "Bank Transfer", cash: "Cash", } return ( {labels[method] || method} ) } const columns: ColumnDef[] = [ { id: "select", header: ({ table }) => (
table.toggleAllPageRowsSelected(!!value)} aria-label="Select all" />
), cell: ({ row }) => (
row.toggleSelected(!!value)} aria-label="Select row" />
), enableSorting: false, enableHiding: false, }, { id: "serviceName", accessorFn: (row) => row.appointment.service.name, header: "Service", cell: ({ row }) => (

{row.original.appointment.service.name}

Dr. {row.original.appointment.dentist.name}

), enableHiding: false, }, { accessorKey: "amount", header: () =>
Amount
, cell: ({ row }) => (
₱{row.original.amount.toFixed(2)}
), }, { accessorKey: "method", header: "Payment Method", cell: ({ row }) => getMethodBadge(row.original.method), }, { accessorKey: "status", header: "Status", cell: ({ row }) => getPaymentBadge(row.original.status), }, { accessorKey: "transactionId", header: "Transaction ID", cell: ({ row }) => (
{row.original.transactionId || "-"}
), }, { accessorKey: "paidAt", header: "Payment Date", cell: ({ row }) => (
{row.original.paidAt ? ( <>

{new Date(row.original.paidAt).toLocaleDateString()}

{new Date(row.original.paidAt).toLocaleTimeString()}

) : ( - )}
), }, { id: "actions", cell: ({ row }) => ( View Receipt Download Invoice {row.original.status === "pending" && ( Pay Now )} ), }, ] type PatientPaymentsTableProps = { payments: PatientPayment[] } export function PatientPaymentsTable({ payments }: PatientPaymentsTableProps) { const [rowSelection, setRowSelection] = React.useState({}) const [columnVisibility, setColumnVisibility] = React.useState({}) const [columnFilters, setColumnFilters] = React.useState([]) const [sorting, setSorting] = React.useState([]) const [pagination, setPagination] = React.useState({ pageIndex: 0, pageSize: 10, }) const table = useReactTable({ data: payments, columns, state: { sorting, columnVisibility, rowSelection, columnFilters, pagination, }, enableRowSelection: true, onRowSelectionChange: setRowSelection, onSortingChange: setSorting, onColumnFiltersChange: setColumnFilters, onColumnVisibilityChange: setColumnVisibility, onPaginationChange: setPagination, getCoreRowModel: getCoreRowModel(), getFilteredRowModel: getFilteredRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFacetedRowModel: getFacetedRowModel(), getFacetedUniqueValues: getFacetedUniqueValues(), }) return (
table.getColumn("serviceName")?.setFilterValue(event.target.value) } className="pl-8" />
{table .getAllColumns() .filter( (column) => typeof column.accessorFn !== "undefined" && column.getCanHide() ) .map((column) => { return ( column.toggleVisibility(!!value)} > {column.id} ) })}
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ) })} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) ) : ( No payments found. )}
{table.getFilteredSelectedRowModel().rows.length} of{" "} {table.getFilteredRowModel().rows.length} row(s) selected.
Page {table.getState().pagination.pageIndex + 1} of{" "} {table.getPageCount()}
) }