"use client" import { useParams } from "next/navigation" import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query" import { type ColumnDef } from "@tanstack/react-table" import { useState } from "react" import { Download, ExternalLink, Plus, Trash2 } from "lucide-react" import { toast } from "sonner" import { useAuthApi } from "@/shared/useApi" import { DataTable, ColumnHeader } from "@/shared/data-view/table-view" import { confirm } from "@/shared/components/confirm-dialog" import { Button } from "@/shared/components/ui/button" import { Card, CardContent } from "@/shared/components/ui/card" import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/shared/components/ui/dialog" import { VehicleDocumentForm } from "@/modules/vehicles/vehicle-document-form" import { formatDate, formatDateTime } from "@/shared/utils/formatters" import { getFullName } from "@/shared/utils/getFullName" type VehicleDocument = { id: number document_number?: string document_type_id?: number document_type?: { id?: number name?: string } | null customer_id?: number customer?: { id?: number first_name?: string last_name?: string name?: string } | null document_expire?: string document_file?: string document_file_url?: string | null created_at: string updated_at: string } export default function VehicleDocumentsPage() { const { id: vehicleId } = useParams<{ id: string }>() const api = useAuthApi() const queryClient = useQueryClient() const [dialogOpen, setDialogOpen] = useState(false) const queryKey = ["vehicle-documents", vehicleId] const { data, isLoading } = useQuery({ queryKey, queryFn: () => api.vehicleDocuments.listDocuments({ vehicle_id: vehicleId }), }) const deleteMutation = useMutation({ mutationFn: (id: number) => api.vehicleDocuments.destroyDocument(String(id)), onSuccess: () => { toast.success("Document deleted successfully.") queryClient.invalidateQueries({ queryKey }) }, onError: () => { toast.error("Failed to delete document.") }, }) const handleDelete = async (doc: VehicleDocument) => { const confirmed = await confirm({ title: "Delete Document", description: `Are you sure you want to delete "${doc.document_number || `Document #${doc.id}`}"?`, confirmLabel: "Delete", variant: "destructive", }) if (confirmed) { deleteMutation.mutate(doc.id) } } const columns: ColumnDef[] = [ { accessorKey: "document_number", header: ({ column }) => , cell: ({ getValue, row }) => { const number = getValue() const fallbackName = row.original.document_file?.split("/").pop() return number || fallbackName || `Document #${row.original.id}` }, }, { accessorKey: "document_type_id", header: ({ column }) => , cell: ({ row, getValue }) => { const typeName = row.original.document_type?.name const value = getValue() return typeName || (value ? `#${value}` : "—") }, }, { accessorKey: "customer_id", header: ({ column }) => , cell: ({ row, getValue }) => { const name = getFullName(row.original.customer as any) || row.original.customer?.name const value = getValue() return name || (value ? `#${value}` : "—") }, }, { accessorKey: "document_expire", header: ({ column }) => , cell: ({ getValue }) => formatDate(getValue()), }, { id: "file", header: ({ column }) => , cell: ({ row }) => { const url = row.original.document_file_url if (!url) return "No file" return (
e.stopPropagation()}> Open Download
) }, enableSorting: false, }, { accessorKey: "created_at", header: ({ column }) => , cell: ({ getValue }) => formatDateTime(getValue()), }, { id: "actions", header: () => Actions, cell: ({ row }) => ( ), enableSorting: false, }, ] const handleRowClick = (doc: VehicleDocument) => { if (doc.document_file_url) { window.open(doc.document_file_url, "_blank", "noopener,noreferrer") return } toast.info("This document has no file to open.") } const documents = (data as any)?.data ?? [] const meta = (data as any)?.meta const pagination = { page: meta?.current_page ?? 1, pageSize: meta?.per_page ?? 15, pageCount: meta?.last_page ?? 1, total: meta?.total ?? 0, } return (
}} columns={columns} data={documents} pagination={pagination} sorting={[]} onChange={() => { }} onRowClick={handleRowClick} isLoading={isLoading} /> Upload Document { queryClient.invalidateQueries({ queryKey }) setDialogOpen(false) }} /> ) }