"use client" import { use, useState } from "react" import { useQuery, useQueryClient } from "@tanstack/react-query" import { useAuthApi } from "@/shared/useApi" import { ColumnHeader, DataTable } from "@/shared/data-view/table-view" import { Button } from "@/shared/components/ui/button" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/shared/components/ui/dialog" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/shared/components/ui/dropdown-menu" import { confirm } from "@/shared/components/confirm-dialog" import { toast } from "sonner" import { Ellipsis, Plus } from "lucide-react" import type { ColumnDef } from "@tanstack/react-table" import { JobCardServiceForm } from "@/modules/job-cards/job-card-service-form" import { formatDate } from "@/shared/utils/formatters" import { Badge } from "@/shared/components/ui/badge" export default function JobCardServicesPage({ params, }: { params: Promise<{ id: string }> }) { const { id: jobCardId } = use(params) const api = useAuthApi() const queryClient = useQueryClient() const queryKey = ["job-card-services", jobCardId] const [dialogOpen, setDialogOpen] = useState(false) const [editItem, setEditItem] = useState(null) const { data, isLoading } = useQuery({ queryKey, queryFn: () => api.jobCards.getServices(jobCardId), }) const rows = (data as any)?.data ?? [] const invalidate = () => queryClient.invalidateQueries({ queryKey }) async function handleDelete(row: any) { const confirmed = await confirm({ title: "Delete this service?", description: `Remove service "${row.service?.labor_name ?? "this service"}" from the job card?`, }) if (!confirmed) return const promise = api.jobCards.deleteService(jobCardId, row.id) toast.promise(promise, { loading: "Deleting...", success: "Service deleted", error: "Failed to delete service", }) await promise invalidate() } const columns: ColumnDef[] = [ { accessorKey: "service.labor_name", header: ({ column }) => , cell: ({ row }) => { const service = row.original.service return service ? (
{service.labor_name} {service.service_code && ( {service.service_code} )}
) : "—" }, }, { accessorKey: "rate_type", header: ({ column }) => , cell: ({ row }) => { const val = row.original.rate_type if (!val) return "—" return ( {val === "flat_rate" ? "Flat Rate" : val === "hourly" ? "Hourly" : val} ) }, }, { accessorKey: "quantity", header: ({ column }) => , cell: ({ row }) => row.original.quantity ?? "—", }, { accessorKey: "rate", header: ({ column }) => , cell: ({ row }) => { const val = row.original.rate return val != null ? `$${Number(val).toFixed(2)}` : "—" }, }, { accessorKey: "labor_rate.title", header: ({ column }) => , cell: ({ row }) => { const lr = row.original.labor_rate return lr ? `${lr.title} ($${Number(lr.rate).toFixed(2)})` : "—" }, }, { accessorKey: "working_hours", header: ({ column }) => , cell: ({ row }) => row.original.working_hours ?? "—", }, { accessorKey: "labor_hours", header: ({ column }) => , cell: ({ row }) => row.original.labor_hours ?? "—", }, { accessorKey: "tax", header: ({ column }) => , cell: ({ row }) => row.original.tax || "—", }, { accessorKey: "department.name", header: ({ column }) => , cell: ({ row }) => row.original.department?.name || "—", }, { accessorKey: "description", header: ({ column }) => , cell: ({ row }) => row.original.description || "—", }, { accessorKey: "created_at", header: ({ column }) => , cell: ({ row }) => formatDate(row.original.created_at), }, { id: "actions", cell: ({ row }) => ( { setEditItem(row.original) setDialogOpen(true) }} > Edit handleDelete(row.original)} > Delete ), }, ] return (
{ setDialogOpen(open) if (!open) setEditItem(null) }} > {editItem ? "Edit Service" : "Add Service"} { setDialogOpen(false) setEditItem(null) invalidate() }} onCancel={() => { setDialogOpen(false) setEditItem(null) }} />
) }