"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, DropdownMenuSeparator, 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 PatientAppointment = { id: string date: Date timeSlot: string status: string notes: string | null dentist: { name: string specialization: string | null } service: { name: string price: number } } const getStatusBadge = (status: string) => { const variants: Record = { pending: "secondary", confirmed: "default", cancelled: "destructive", completed: "outline", rescheduled: "secondary", } return ( {status.toUpperCase()} ) } 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.service.name, header: "Service", cell: ({ row }) => (

{row.original.service.name}

₱{row.original.service.price.toFixed(2)}

), enableHiding: false, }, { id: "dentistName", accessorFn: (row) => row.dentist.name, header: "Dentist", cell: ({ row }) => (

Dr. {row.original.dentist.name}

{row.original.dentist.specialization && (

{row.original.dentist.specialization}

)}
), }, { accessorKey: "date", header: "Date & Time", cell: ({ row }) => (

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

{row.original.timeSlot}

), }, { accessorKey: "status", header: "Status", cell: ({ row }) => getStatusBadge(row.original.status), }, { accessorKey: "notes", header: "Notes", cell: ({ row }) => (
{row.original.notes || "-"}
), }, { id: "actions", cell: ({ row }) => { const canModify = row.original.status === "pending" || row.original.status === "confirmed" return ( View Details {canModify && ( <> Reschedule Cancel )} ) }, }, ] type PatientAppointmentsTableProps = { appointments: PatientAppointment[] } export function PatientAppointmentsTable({ appointments }: PatientAppointmentsTableProps) { 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: appointments, 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 appointments found. )}
{table.getFilteredSelectedRowModel().rows.length} of{" "} {table.getFilteredRowModel().rows.length} row(s) selected.
Page {table.getState().pagination.pageIndex + 1} of{" "} {table.getPageCount()}
) }