"use client" import { useState } from "react" import { useMutation } from "@tanstack/react-query" import { useRouter } from "next/navigation" import { Plus, Trash2, MessageSquare } from "lucide-react" import { toast } from "sonner" import { useAuthApi } from "@/shared/useApi" import { confirm } from "@/shared/components/confirm-dialog" import { Button } from "@/shared/components/ui/button" import { Card, CardContent, CardHeader, CardTitle, } from "@/shared/components/ui/card" import { Textarea } from "@/shared/components/ui/textarea" type CustomerRemark = { id: number remark: string created_at?: string } type Props = { jobCardId: string remarks: CustomerRemark[] } export function JobCardRemarksList({ jobCardId, remarks }: Props) { const api = useAuthApi() const router = useRouter() const [newRemark, setNewRemark] = useState("") const addMutation = useMutation({ mutationFn: (remark: string) => api.jobCards.addCustomerRemark(jobCardId, { remark }), onSuccess: () => { toast.success("Customer remark added") setNewRemark("") router.refresh() }, onError: () => { toast.error("Failed to add customer remark") }, }) const deleteMutation = useMutation({ mutationFn: (remarkId: number) => api.jobCards.deleteCustomerRemark(jobCardId, remarkId), onSuccess: () => { toast.success("Customer remark deleted") router.refresh() }, onError: () => { toast.error("Failed to delete customer remark") }, }) const handleAdd = () => { const trimmed = newRemark.trim() if (!trimmed) return addMutation.mutate(trimmed) } const handleDelete = async (remark: CustomerRemark) => { const confirmed = await confirm({ title: "Delete Customer Remark", description: "Are you sure you want to delete this remark?", confirmLabel: "Delete", variant: "destructive", }) if (confirmed) { deleteMutation.mutate(remark.id) } } return ( Customer Remarks {remarks.length === 0 && (

No customer remarks yet.

)} {remarks.map((remark) => (

{remark.remark}

))}