import React, { useState } from 'react'; interface Flight { flightNumber: string; flightDate: string; segment: number; validated?: boolean; validationData?: any; } interface VipFormData { name: string; organization: string; department: 'Office of Development' | 'Admin'; transportMode: 'flight' | 'self-driving'; flights?: Flight[]; expectedArrival?: string; needsAirportPickup?: boolean; needsVenueTransport: boolean; notes: string; } interface VipFormProps { onSubmit: (vipData: VipFormData) => void; onCancel: () => void; } const VipForm: React.FC = ({ onSubmit, onCancel }) => { const [formData, setFormData] = useState({ name: '', organization: '', department: 'Office of Development', transportMode: 'flight', flights: [{ flightNumber: '', flightDate: '', segment: 1 }], expectedArrival: '', needsAirportPickup: true, needsVenueTransport: true, notes: '' }); const [flightValidating, setFlightValidating] = useState<{ [key: number]: boolean }>({}); const [flightErrors, setFlightErrors] = useState<{ [key: number]: string }>({}); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); // Only include flights with flight numbers const validFlights = formData.flights?.filter(f => f.flightNumber) || []; onSubmit({ ...formData, flights: validFlights.length > 0 ? validFlights : undefined }); }; const handleChange = (e: React.ChangeEvent) => { const { name, value, type } = e.target; if (type === 'checkbox') { const checked = (e.target as HTMLInputElement).checked; setFormData(prev => ({ ...prev, [name]: checked })); } else { setFormData(prev => ({ ...prev, [name]: value })); } }; const handleTransportModeChange = (mode: 'flight' | 'self-driving') => { setFormData(prev => ({ ...prev, transportMode: mode, flights: mode === 'flight' ? [{ flightNumber: '', flightDate: '', segment: 1 }] : undefined, expectedArrival: mode === 'self-driving' ? prev.expectedArrival : '', needsAirportPickup: mode === 'flight' ? true : false })); // Clear flight errors when switching away from flight mode if (mode !== 'flight') { setFlightErrors({}); } }; const handleFlightChange = (index: number, field: 'flightNumber' | 'flightDate', value: string) => { setFormData(prev => ({ ...prev, flights: prev.flights?.map((flight, i) => i === index ? { ...flight, [field]: value, validated: false } : flight ) || [] })); // Clear validation for this flight when it changes setFlightErrors(prev => ({ ...prev, [index]: '' })); }; const addConnectingFlight = () => { const currentFlights = formData.flights || []; if (currentFlights.length < 3) { setFormData(prev => ({ ...prev, flights: [...currentFlights, { flightNumber: '', flightDate: currentFlights[currentFlights.length - 1]?.flightDate || '', segment: currentFlights.length + 1 }] })); } }; const removeConnectingFlight = (index: number) => { setFormData(prev => ({ ...prev, flights: prev.flights?.filter((_, i) => i !== index).map((flight, i) => ({ ...flight, segment: i + 1 })) || [] })); // Clear errors for removed flight setFlightErrors(prev => { const newErrors = { ...prev }; delete newErrors[index]; return newErrors; }); }; const validateFlight = async (index: number) => { const flight = formData.flights?.[index]; if (!flight || !flight.flightNumber || !flight.flightDate) { setFlightErrors(prev => ({ ...prev, [index]: 'Please enter flight number and date' })); return; } setFlightValidating(prev => ({ ...prev, [index]: true })); setFlightErrors(prev => ({ ...prev, [index]: '' })); try { const url = `/api/flights/${flight.flightNumber}?date=${flight.flightDate}`; const response = await fetch(url); if (response.ok) { const data = await response.json(); // Update flight with validation data setFormData(prev => ({ ...prev, flights: prev.flights?.map((f, i) => i === index ? { ...f, validated: true, validationData: data } : f ) || [] })); setFlightErrors(prev => ({ ...prev, [index]: '' })); } else { const errorData = await response.json(); setFlightErrors(prev => ({ ...prev, [index]: errorData.error || 'Invalid flight number' })); } } catch (error) { setFlightErrors(prev => ({ ...prev, [index]: 'Error validating flight' })); } finally { setFlightValidating(prev => ({ ...prev, [index]: false })); } }; return (
{/* Modal Header */}

Add New VIP

Enter VIP details and travel information

{/* Modal Body */}
{/* Basic Information Section */}

Basic Information

{/* Transportation Section */}

Transportation Details

handleTransportModeChange('flight')} > handleTransportModeChange('flight')} className="form-radio mr-3" /> Arriving by Flight
handleTransportModeChange('self-driving')} > handleTransportModeChange('self-driving')} className="form-radio mr-3" /> Self-Driving
{/* Flight Mode Fields */} {formData.transportMode === 'flight' && formData.flights && (
{formData.flights.map((flight, index) => (

{index === 0 ? 'Primary Flight' : `Connecting Flight ${index}`}

{index > 0 && ( )}
handleFlightChange(index, 'flightNumber', e.target.value)} className="form-input" placeholder="e.g., AA123" required={index === 0} />
handleFlightChange(index, 'flightDate', e.target.value)} className="form-input" required={index === 0} min={new Date().toISOString().split('T')[0]} />
{/* Flight Validation Results */} {flightErrors[index] && (
{flightErrors[index]}
)} {flight.validated && flight.validationData && (
Valid: {flight.validationData.airline || 'Flight'} - {flight.validationData.departure?.airport} → {flight.validationData.arrival?.airport}
{flight.validationData.flightDate !== flight.flightDate && (
Live tracking starts 4 hours before departure on {new Date(flight.flightDate).toLocaleDateString()}
)}
)}
))} {formData.flights.length < 3 && ( )}
Needs Airport Pickup (from final destination)
)} {/* Self-Driving Mode Fields */} {formData.transportMode === 'self-driving' && (
)} {/* Universal Transportation Option */}
Needs Transportation Between Venues
Check this if the VIP needs rides between different event locations
{/* Additional Information Section */}

Additional Information