import React, { useState, useEffect } from "react"; import { Plus, Trash, User, Truck, Car } from "lucide-react"; import { useAuth } from "../contexts/AuthContext"; import { listCarros, createCarro, deleteCarro, addPassenger, removePassenger, listPassengers, listCarros as fetchCarrosApi } from "../services/apiService"; import { useData } from "../contexts/DataContext"; import { UserRole } from "../types"; interface EventLogisticsProps { agendaId: string; assignedProfessionals?: string[]; } interface Carro { id: string; driver_id: string; driver_name: string; driver_avatar: string; arrival_time: string; notes: string; passengers: any[]; // We will fetch and attach } const EventLogistics: React.FC = ({ agendaId, assignedProfessionals }) => { const { token, user } = useAuth(); const { professionals } = useData(); const [carros, setCarros] = useState([]); const [loading, setLoading] = useState(false); // New Car State const [driverId, setDriverId] = useState(""); const [arrivalTime, setArrivalTime] = useState("07:00"); const [notes, setNotes] = useState(""); const isEditable = user?.role === UserRole.SUPERADMIN || user?.role === UserRole.BUSINESS_OWNER; useEffect(() => { if (agendaId && token) { loadCarros(); } }, [agendaId, token]); const loadCarros = async () => { setLoading(true); const res = await fetchCarrosApi(agendaId, token!); if (res.data) { // For each car, fetch passengers const carsWithPassengers = await Promise.all(res.data.map(async (car: any) => { const passRes = await listPassengers(car.id, token!); return { ...car, passengers: passRes.data || [] }; })); setCarros(carsWithPassengers); } setLoading(false); }; const handleAddCarro = async () => { // Driver ID is optional (could be external), but for now select from professionals const input = { agenda_id: agendaId, motorista_id: driverId || undefined, horario_chegada: arrivalTime, observacoes: notes }; const res = await createCarro(input, token!); if (res.data) { loadCarros(); setDriverId(""); setNotes(""); } else { alert("Erro ao criar carro: " + res.error); } }; const handleDeleteCarro = async (id: string) => { if (confirm("Remover este carro e passageiros?")) { await deleteCarro(id, token!); loadCarros(); } }; const handleAddPassenger = async (carId: string, profId: string) => { if (!profId) return; const res = await addPassenger(carId, profId, token!); if (!res.error) { loadCarros(); } else { alert("Erro ao adicionar passageiro: " + res.error); } }; const handleRemovePassenger = async (carId: string, profId: string) => { const res = await removePassenger(carId, profId, token!); if (!res.error) { loadCarros(); } }; return (

Logística de Transporte

{/* Add Car Form - Only for Admins */} {isEditable && (
setArrivalTime(e.target.value)} />
)} {/* Cars List */}
{loading ?

Carregando...

: carros.map(car => (

{car.driver_name || "Motorista não definido"}

Chegada: {car.arrival_time}

{isEditable && ( )}
{/* Passengers */}

Passageiros

{car.passengers.length === 0 &&

Vazio

} {car.passengers.map((p: any) => (
{p.name || "Desconhecido"} {isEditable && ( )}
))}
{/* Add Passenger - Only for Admins */} {isEditable && ( )}
))}
); }; export default EventLogistics;