import { useQuery } from '@tanstack/react-query'; import { api } from '@/lib/api'; import { X, Calendar, Clock, MapPin, Car, User, ChevronLeft, ChevronRight } from 'lucide-react'; import { Driver } from '@/types'; import { useState } from 'react'; import { useFormattedDate } from '@/hooks/useFormattedDate'; interface ScheduleEvent { id: string; title: string; startTime: string; endTime: string; pickupLocation?: string; dropoffLocation?: string; location?: string; status: string; type: string; vipIds: string[]; vehicle?: { name: string; licensePlate?: string; }; } interface DriverScheduleResponse { id: string; name: string; phone: string; events: ScheduleEvent[]; } interface DriverScheduleModalProps { driver: Driver | null; isOpen: boolean; onClose: () => void; } export function DriverScheduleModal({ driver, isOpen, onClose }: DriverScheduleModalProps) { const [selectedDate, setSelectedDate] = useState(new Date()); const { formatDate, formatTime } = useFormattedDate(); const dateString = selectedDate.toISOString().split('T')[0]; const { data: schedule, isLoading } = useQuery({ queryKey: ['driver-schedule', driver?.id, dateString], queryFn: async () => { const { data } = await api.get(`/drivers/${driver?.id}/schedule`, { params: { date: dateString }, }); return data; }, enabled: isOpen && !!driver?.id, }); // Fetch VIP names for the events const allVipIds = schedule?.events?.flatMap((e) => e.vipIds) || []; const uniqueVipIds = [...new Set(allVipIds)]; const { data: vips } = useQuery({ queryKey: ['vips-for-schedule', uniqueVipIds.join(',')], queryFn: async () => { const { data } = await api.get('/vips'); return data; }, enabled: uniqueVipIds.length > 0, }); const vipMap = new Map(vips?.map((v: any) => [v.id, v.name]) || []); if (!isOpen || !driver) return null; const goToPreviousDay = () => { const newDate = new Date(selectedDate); newDate.setDate(newDate.getDate() - 1); setSelectedDate(newDate); }; const goToNextDay = () => { const newDate = new Date(selectedDate); newDate.setDate(newDate.getDate() + 1); setSelectedDate(newDate); }; const goToToday = () => { setSelectedDate(new Date()); }; const isToday = selectedDate.toDateString() === new Date().toDateString(); const getStatusColor = (status: string) => { switch (status) { case 'COMPLETED': return 'bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400'; case 'IN_PROGRESS': return 'bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400'; case 'CANCELLED': return 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400'; default: return 'bg-gray-100 text-gray-800 dark:bg-gray-800 dark:text-gray-300'; } }; const getTypeLabel = (type: string) => { const labels: Record = { TRANSPORT: 'Transport', MEETING: 'Meeting', EVENT: 'Event', MEAL: 'Meal', ACCOMMODATION: 'Accommodation', }; return labels[type] || type; }; // Filter events for the selected date const dayEvents = schedule?.events?.filter((event) => { const eventDate = new Date(event.startTime).toDateString(); return eventDate === selectedDate.toDateString(); }) || []; // Sort events by start time const sortedEvents = [...dayEvents].sort( (a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime() ); return (
{/* Header */}

{driver.name}'s Schedule

{driver.phone}

{/* Date Navigation */}
{formatDate(selectedDate)} {!isToday && ( )}
{/* Content */}
{isLoading ? (
) : sortedEvents.length === 0 ? (

No events scheduled for this day

) : (
{sortedEvents.map((event) => { const vipNames = event.vipIds .map((id) => vipMap.get(id) || 'Unknown VIP') .join(', '); return (
{/* Timeline dot */}
{/* Time and Status */}
{formatTime(event.startTime)} - {formatTime(event.endTime)}
{getTypeLabel(event.type)} {event.status}
{/* Title */}

{event.title}

{/* VIP */} {vipNames && (
{vipNames}
)} {/* Location */} {(event.pickupLocation || event.dropoffLocation || event.location) && (
{event.pickupLocation && event.dropoffLocation ? ( <>
{event.pickupLocation}
{event.dropoffLocation}
) : ( {event.location} )}
)} {/* Vehicle */} {event.vehicle && (
{event.vehicle.name} {event.vehicle.licensePlate && ` (${event.vehicle.licensePlate})`}
)}
); })}
)}
{/* Footer */}
{sortedEvents.length} event{sortedEvents.length !== 1 ? 's' : ''} scheduled
); }