'use client' import { useState, useEffect } from 'react' import { WorldMap } from '@/components/ui/world-map' import { MapPin, AlertTriangle, X, Save } from 'lucide-react' import { motion } from 'framer-motion' interface OutbreakReport { id: string lat: number lng: number crop: string disease: string severity: 'low' | 'medium' | 'high' date: string description: string } export default function OutbreakMap() { const [reports, setReports] = useState([]) const [selectedLocation, setSelectedLocation] = useState<{ lat: number; lng: number } | null>(null) const [showReportForm, setShowReportForm] = useState(false) const [formData, setFormData] = useState({ crop: '', disease: '', severity: 'medium' as 'low' | 'medium' | 'high', description: '', }) // Convert reports to map dots format const mapDots = reports.map((report) => ({ start: { lat: report.lat, lng: report.lng, label: report.disease }, end: { lat: report.lat, lng: report.lng, label: report.disease }, })) const handleMapClick = (e: React.MouseEvent) => { // Find the map container const mapContainer = e.currentTarget.querySelector('[class*="aspect-[2/1]"]') if (!mapContainer) return const rect = mapContainer.getBoundingClientRect() const x = e.clientX - rect.left const y = e.clientY - rect.top // Convert pixel coordinates to lat/lng (map is 800x400 viewBox) const lng = (x / rect.width) * 360 - 180 const lat = 90 - (y / rect.height) * 180 // Clamp values to valid ranges const clampedLat = Math.max(-90, Math.min(90, lat)) const clampedLng = Math.max(-180, Math.min(180, lng)) setSelectedLocation({ lat: clampedLat, lng: clampedLng }) setShowReportForm(true) } const handleSubmitReport = () => { if (!selectedLocation || !formData.crop || !formData.disease) { alert('Please add the crop and what the issue looks like.') return } const newReport: OutbreakReport = { id: Date.now().toString(), lat: selectedLocation.lat, lng: selectedLocation.lng, crop: formData.crop, disease: formData.disease, severity: formData.severity, date: new Date().toISOString(), description: formData.description, } setReports([...reports, newReport]) setShowReportForm(false) setSelectedLocation(null) setFormData({ crop: '', disease: '', severity: 'medium', description: '', }) } const getSeverityColor = (severity: string) => { switch (severity) { case 'high': return 'text-red-600 bg-red-50 border-red-200' case 'medium': return 'text-orange-600 bg-orange-50 border-orange-200' case 'low': return 'text-yellow-600 bg-yellow-50 border-yellow-200' default: return 'text-slate-600 bg-slate-50 border-slate-200' } } return (
{/* Header */}

Local crop issue map

Tap the map where you are seeing crop trouble so nearby farms can watch their fields.

{/* Map Container */}
{/* Clickable overlay for map interaction */}
{selectedLocation && (

Selected Location

Lat: {selectedLocation.lat.toFixed(4)}, Lng: {selectedLocation.lng.toFixed(4)}

)}

Tap map to report crop trouble

{/* Report Form Modal — avoid motion opacity on full-screen layer (invisible overlays still capture clicks). */} {showReportForm && (
{ setShowReportForm(false) setSelectedLocation(null) }} />
e.stopPropagation()} className="relative z-10 max-h-[90dvh] w-full max-w-md overflow-y-auto overscroll-contain rounded-xl border border-slate-200 bg-white p-6 shadow-2xl" >

Report crop trouble

setFormData({ ...formData, crop: e.target.value })} placeholder="Corn, wheat, rice" className="w-full px-4 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent" />
setFormData({ ...formData, disease: e.target.value })} placeholder="Rust, blight, yellowing" className="w-full px-4 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent" />