"use client" import { useState } from "react" import { useMutation } from "@tanstack/react-query" import { useAuthApi } from "@/shared/useApi" import { useRouter } from "next/navigation" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "@/shared/components/ui/dialog" import { Popover, PopoverContent, PopoverTrigger, } from "@/shared/components/ui/popover" import { Calendar } from "@/shared/components/ui/calendar" import { toast } from "sonner" import { Pencil, CalendarIcon, UserCog, UserCheck, Loader2 } from "lucide-react" import { format } from "date-fns" import { EmployeeCombobox, type EmployeeOption } from "@/modules/employees/employee-combobox" type JobCardActionsProps = { jobCardId: string orderDate?: string | null serviceWriterName?: string | null salesPersonName?: string | null primaryTechnicianName?: string | null } // ── Informative Action Card ── function ActionCard({ icon: Icon, label, value, isPending, }: { icon: React.ComponentType<{ className?: string }> label: string value?: string | null isPending?: boolean }) { return (
{label} {value ?? Not set}
{isPending ? ( ) : ( )}
) } // ── Employee Picker Dialog ── type EmployeePickerDialogProps = { open: boolean onOpenChange: (open: boolean) => void title: string description: string isPending: boolean onSelect: (employeeId: number) => void } function EmployeePickerDialog({ open, onOpenChange, title, description, isPending, onSelect, }: EmployeePickerDialogProps) { const handleSelect = (emp: EmployeeOption | null) => { console.log('Selected employee object:', emp, 'Value:', emp?.value, 'Type of value:', typeof emp?.value) if (emp && emp.value) { const employeeId = Number(emp.value) console.log('Parsed employee ID:', employeeId, 'Type:', typeof employeeId) onSelect(employeeId) onOpenChange(false) } } return ( {title} {description} ) } // ── Main Component ── export function JobCardActions({ jobCardId, orderDate, serviceWriterName, salesPersonName, primaryTechnicianName }: JobCardActionsProps) { const api = useAuthApi() const router = useRouter() const [datePickerOpen, setDatePickerOpen] = useState(false) const [serviceWriterDialogOpen, setServiceWriterDialogOpen] = useState(false) const [salesPersonDialogOpen, setSalesPersonDialogOpen] = useState(false) const [primaryTechnicianDialogOpen, setPrimaryTechnicianDialogOpen] = useState(false) const changeDateMutation = useMutation({ mutationFn: (date: Date) => { const order_date = format(date, "yyyy-MM-dd") const promise = api.jobCards.changeDate(jobCardId, { order_date }) toast.promise(promise, { loading: "Updating date...", success: "Date updated successfully", error: "Failed to update date", }) return promise }, onSuccess: () => { setDatePickerOpen(false) router.refresh() }, }) const changeServiceWriterMutation = useMutation({ mutationFn: async (employeeId: number) => { console.log('Sending service writer ID:', employeeId, 'Type:', typeof employeeId) const promise = api.jobCards.changeServiceWriter(jobCardId, { service_writer_id: employeeId }) toast.promise(promise, { loading: "Updating service writer...", success: "Service writer updated successfully", error: "Failed to update service writer", }) return promise }, onSuccess: () => { setServiceWriterDialogOpen(false) router.refresh() }, }) const changeSalesPersonMutation = useMutation({ mutationFn: async (employeeId: number) => { console.log('Sending sales person ID:', employeeId, 'Type:', typeof employeeId) const promise = api.jobCards.changeSalesPerson(jobCardId, { sales_person_id: employeeId }) toast.promise(promise, { loading: "Updating sales person...", success: "Sales person updated successfully", error: "Failed to update sales person", }) return promise }, onSuccess: () => { setSalesPersonDialogOpen(false) router.refresh() }, }) const changePrimaryTechnicianMutation = useMutation({ mutationFn: async (employeeId: number) => { console.log('Sending primary technician ID:', employeeId, 'Type:', typeof employeeId) const promise = api.jobCards.changeTechnician(jobCardId, { technician_id: employeeId }) toast.promise(promise, { loading: "Updating primary technician...", success: "Primary technician updated successfully", error: "Failed to update primary technician", }) return promise }, onSuccess: () => { setPrimaryTechnicianDialogOpen(false) router.refresh() }, }) return (
{/* Check-in Date Action Card */} { if (date) changeDateMutation.mutate(date) }} disabled={changeDateMutation.isPending} /> {/* Service Writer Action Card */} {/* Sales Person Action Card */} {/* Primary Technician Action Card */} {/* Edit / Delete Dropdown */} { console.log('Service Writer dialog onSelect called with:', id, 'Type:', typeof id) changeServiceWriterMutation.mutate(id) }} /> { console.log('Dialog onSelect called with:', id, 'Type:', typeof id) changeSalesPersonMutation.mutate(id) }} /> { console.log('Primary Technician dialog onSelect called with:', id, 'Type:', typeof id) changePrimaryTechnicianMutation.mutate(id) }} />
) }