import React, { useState } from 'react'; import { MapPin, Truck, AlertTriangle, Navigation, Filter, Search, RefreshCw, Radio, Gauge, Clock, X, ChevronRight, History as HistoryIcon, Layers } from 'lucide-react'; import { useTelemetry } from '../hooks/useTelemetry'; const Modal = ({ isOpen, onClose, title, children }) => { if (!isOpen) return null; return (

{title}

{children}
); }; const GrTelemetryView = () => { const [selectedClient, setSelectedClient] = useState('ALL'); const [searchTerm, setSearchTerm] = useState(''); const [activeVehicle, setActiveVehicle] = useState(null); const [historyData, setHistoryData] = useState([]); const [isHistoryModalOpen, setIsHistoryModalOpen] = useState(false); const [isHistoryLoading, setIsHistoryLoading] = useState(false); const { vehicles, alerts, loading, error, refresh, getVehicleHistory } = useTelemetry(); const filteredVehicles = vehicles.filter(vehicle => { const matchesClient = selectedClient === 'ALL' || vehicle.client === selectedClient; const matchesSearch = vehicle.plate.toLowerCase().includes(searchTerm.toLowerCase()) || vehicle.driver.toLowerCase().includes(searchTerm.toLowerCase()); return matchesClient && matchesSearch; }); const getRiskColor = (status) => { switch (status) { case 'CRITICAL': return 'bg-red-500 text-white'; case 'HIGH': return 'bg-orange-500 text-white'; case 'NORMAL': return 'bg-green-500 text-white'; default: return 'bg-slate-500 text-white'; } }; const handleShowHistory = async (plate) => { setIsHistoryLoading(true); setIsHistoryModalOpen(true); try { const history = await getVehicleHistory(plate); setHistoryData(history); } catch (err) { alert('Erro ao carregar histórico'); } finally { setIsHistoryLoading(false); } }; const handleShowOnMap = (plate) => { alert(`Focando veículo ${plate} no mapa dinâmico...`); }; const clients = ['ALL', 'PRALOG', 'PROFARMA', 'PETY', 'MERCADO_LIVRE']; return (
{/* Header */}

Telemetria & Rastreamento

Monitoramento de posições em tempo real e alertas de geofencing

{/* Stats Cards */}
Total Veículos
{vehicles.length}
Ativos
{vehicles.filter(v => v.status === 'ATIVO').length}
Alertas
{alerts.length}
Críticos
{vehicles.filter(v => v.riskStatus === 'CRITICAL').length}
{/* Filtros */}
{clients.map((client) => ( ))}
setSearchTerm(e.target.value)} className="pl-10 pr-4 py-2 bg-white dark:bg-[#141414] border border-slate-200 dark:border-white/5 rounded-full text-[10px] font-bold focus:outline-none focus:ring-4 focus:ring-[var(--gr-primary)]/10 transition-all uppercase outline-none" />
{/* Lista de Veículos */} {loading ? (
) : filteredVehicles.length === 0 ? (

Nenhum veículo encontrado

) : (
{filteredVehicles.map((vehicle) => (

{vehicle.plate}

{vehicle.driver}

{vehicle.riskStatus}
{vehicle.address}

Velocidade

{vehicle.speed} km/h

Provedor

{vehicle.provider}

{vehicle.alert && (
{vehicle.alert.replace('_', ' ')}
)}
))}
)} {/* Modal de Histórico */} setIsHistoryModalOpen(false)} title={`Histórico de Posições - ${filteredVehicles.find(v => v.id === activeVehicle)?.plate || ''}`} >
{isHistoryLoading ? (

Carregando trajeto...

) : historyData.length === 0 ? (

Nenhum histórico disponível para este veículo.

) : (
{historyData.map((pos, i) => (

Lat: {pos.lat.toFixed(4)} • Lng: {pos.lng.toFixed(4)}

{new Date(pos.timestamp).toLocaleString('pt-BR')}

{pos.speed}km/h
))}
)}
); }; export default GrTelemetryView;