"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 } 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) {
return (
)
}
// ── 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: (employeeId: number) => {
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: (employeeId: number) => {
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()
},
})
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 */}
changeServiceWriterMutation.mutate(id)}
/>
changeSalesPersonMutation.mutate(id)}
/>
changeSalesPersonMutation.mutate(id)}
/>
{}}
// onSelect={(id) => changePrimaryTechnicianMutation.mutate(id)}
/>
)
}