import { useState } from 'react'; import { TransportEventType, TransportMethod, TransportLocation } from '../../lib/transport/types'; interface TransportEventFormProps { onSubmit: (data: TransportEventFormData) => void; plantId?: string; batchId?: string; loading?: boolean; defaultEventType?: TransportEventType; } export interface TransportEventFormData { eventType: TransportEventType; fromLocation: Partial; toLocation: Partial; transportMethod: TransportMethod; notes?: string; plantIds?: string[]; seedBatchId?: string; } const EVENT_TYPES: { value: TransportEventType; label: string; icon: string }[] = [ { value: 'seed_acquisition', label: 'Seed Acquisition', icon: '🌱' }, { value: 'planting', label: 'Planting', icon: '🌿' }, { value: 'growing_transport', label: 'Growing Transport', icon: '🚚' }, { value: 'harvest', label: 'Harvest', icon: '🥬' }, { value: 'processing', label: 'Processing', icon: '⚙️' }, { value: 'distribution', label: 'Distribution', icon: '📦' }, { value: 'consumer_delivery', label: 'Consumer Delivery', icon: '🏠' }, { value: 'seed_saving', label: 'Seed Saving', icon: '💾' }, { value: 'seed_sharing', label: 'Seed Sharing', icon: '🤝' }, ]; const TRANSPORT_METHODS: { value: TransportMethod; label: string; carbonInfo: string }[] = [ { value: 'walking', label: 'Walking', carbonInfo: '0 kg CO2/km' }, { value: 'bicycle', label: 'Bicycle', carbonInfo: '0 kg CO2/km' }, { value: 'electric_vehicle', label: 'Electric Vehicle', carbonInfo: '0.02 kg CO2/km' }, { value: 'hybrid_vehicle', label: 'Hybrid Vehicle', carbonInfo: '0.08 kg CO2/km' }, { value: 'gasoline_vehicle', label: 'Gasoline Vehicle', carbonInfo: '0.12 kg CO2/km' }, { value: 'diesel_truck', label: 'Diesel Truck', carbonInfo: '0.15 kg CO2/km' }, { value: 'electric_truck', label: 'Electric Truck', carbonInfo: '0.03 kg CO2/km' }, { value: 'refrigerated_truck', label: 'Refrigerated Truck', carbonInfo: '0.25 kg CO2/km' }, { value: 'rail', label: 'Rail', carbonInfo: '0.01 kg CO2/km' }, { value: 'ship', label: 'Ship', carbonInfo: '0.008 kg CO2/km' }, { value: 'air', label: 'Air Freight', carbonInfo: '0.5 kg CO2/km' }, { value: 'drone', label: 'Drone', carbonInfo: '0.01 kg CO2/km' }, { value: 'local_delivery', label: 'Local Delivery', carbonInfo: '0.05 kg CO2/km' }, { value: 'customer_pickup', label: 'Customer Pickup', carbonInfo: '0.1 kg CO2/km' }, ]; const LOCATION_TYPES = [ 'farm', 'greenhouse', 'vertical_farm', 'warehouse', 'hub', 'market', 'consumer', 'seed_bank', 'other', ] as const; export default function TransportEventForm({ onSubmit, plantId, batchId, loading = false, defaultEventType = 'harvest', }: TransportEventFormProps) { const [eventType, setEventType] = useState(defaultEventType); const [transportMethod, setTransportMethod] = useState('electric_vehicle'); const [notes, setNotes] = useState(''); const [useCurrentLocation, setUseCurrentLocation] = useState(false); const [gettingLocation, setGettingLocation] = useState(false); const [fromLocation, setFromLocation] = useState>({ latitude: 0, longitude: 0, locationType: 'farm', city: '', region: '', }); const [toLocation, setToLocation] = useState>({ latitude: 0, longitude: 0, locationType: 'market', city: '', region: '', }); const getCurrentLocation = async (target: 'from' | 'to') => { if (!navigator.geolocation) { alert('Geolocation is not supported by your browser'); return; } setGettingLocation(true); navigator.geolocation.getCurrentPosition( (position) => { const update = { latitude: position.coords.latitude, longitude: position.coords.longitude, }; if (target === 'from') { setFromLocation((prev) => ({ ...prev, ...update })); } else { setToLocation((prev) => ({ ...prev, ...update })); } setGettingLocation(false); }, (error) => { console.error('Error getting location:', error); alert('Unable to get your location. Please enter coordinates manually.'); setGettingLocation(false); } ); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); onSubmit({ eventType, fromLocation, toLocation, transportMethod, notes: notes || undefined, plantIds: plantId ? [plantId] : undefined, seedBatchId: batchId, }); }; const LocationInput = ({ label, value, onChange, onGetCurrent, }: { label: string; value: Partial; onChange: (loc: Partial) => void; onGetCurrent: () => void; }) => (

{label}

onChange({ ...value, latitude: parseFloat(e.target.value) || 0 })} className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500" placeholder="0.0000" />
onChange({ ...value, longitude: parseFloat(e.target.value) || 0 })} className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500" placeholder="0.0000" />
onChange({ ...value, city: e.target.value })} className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500" placeholder="City name" />
onChange({ ...value, region: e.target.value })} className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500" placeholder="State/Province" />
); return (

Record Transport Event

{/* Event Type */}
{EVENT_TYPES.map((type) => ( ))}
{/* Locations */}
getCurrentLocation('from')} /> getCurrentLocation('to')} />
{/* Transport Method */}

Carbon emissions are calculated based on distance and transport method

{/* Notes */}