import { useState } from 'react'; import { PackageCheck } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { useMutation } from '@tanstack/react-query'; import { api } from '@/services/api'; import { useToast } from '@/hooks/use-toast'; /** * Customer Pickup + Proof of Delivery capture for a LOADED cargo. * Records receiver name, pickup date and remarks, then marks the cargo DELIVERED. */ export function DeliverCargoDialog({ cargoId, onSuccess }: { cargoId: string; onSuccess?: () => void }) { const [open, setOpen] = useState(false); const [receiverName, setReceiverName] = useState(''); const [pickupDate, setPickupDate] = useState(''); const [deliveryRemarks, setDeliveryRemarks] = useState(''); const deliver = useMutation(api.cargoes.deliver.mutationOptions()); const { toast } = useToast(); const handleDeliver = async () => { if (!receiverName.trim()) { toast({ title: 'Receiver name is required', variant: 'destructive' }); return; } try { await deliver.mutateAsync({ id: cargoId, payload: { receiverName: receiverName.trim(), pickupDate: pickupDate ? new Date(pickupDate).toISOString() : undefined, deliveryRemarks: deliveryRemarks.trim() || undefined, }, }); toast({ title: 'Delivered', description: 'Proof of delivery recorded; cargo marked delivered.' }); setOpen(false); setReceiverName(''); setPickupDate(''); setDeliveryRemarks(''); onSuccess?.(); } catch (error) { const message = (error as { response?: { data?: { message?: string } } })?.response?.data?.message ?? 'Could not record delivery.'; toast({ title: 'Delivery failed', description: String(message), variant: 'destructive' }); } }; return ( Customer Pickup & Proof of Delivery
setReceiverName(e.target.value)} />
setPickupDate(e.target.value)} />