import React, {useState, useEffect} from "react"; import { useSelector } from "react-redux"; import { useParams, useNavigate } from "react-router-dom"; import { DailyRoutesTemplateService } from "../../services"; import { selectAllActiveDrivers, selectAllActiveVehicles } from "./../../store"; import { Breadcrumb, Spinner, Modal, Button } from "react-bootstrap"; import { Pencil, Trash } from "react-bootstrap-icons"; import RouteCustomerTable from "./RouteCustomerTable"; const ViewDailyTemplateRoute = () => { const params = useParams(); const navigate = useNavigate(); const drivers = useSelector(selectAllActiveDrivers); const vehicles = useSelector(selectAllActiveVehicles); const [template, setTemplate] = useState(null); const [currentRoute, setCurrentRoute] = useState(null); const [showDeleteModal, setShowDeleteModal] = useState(false); const [deleting, setDeleting] = useState(false); const currentVehicle = vehicles.find(item => item.id === currentRoute?.vehicle); const currentDriver = drivers.find(item => item.id === currentRoute?.driver); useEffect(() => { fetchTemplateAndRoute(); }, [params.id, params.routeId]); const fetchTemplateAndRoute = () => { DailyRoutesTemplateService.getDailyRoutesTemplate(params.id).then(data => { setTemplate(data.data); const route = data.data.routes?.find(r => r._id === params.routeId); setCurrentRoute(route); }); }; const goBack = () => { navigate(`/trans-routes/daily-templates/view/${params.id}`); }; const goToEdit = () => { navigate(`/trans-routes/daily-templates/${params.id}/update-route/${params.routeId}`); }; const confirmDelete = () => { setShowDeleteModal(true); }; const deleteRoute = () => { setDeleting(true); const updatedRoutes = template.routes.filter(r => r._id !== params.routeId); DailyRoutesTemplateService.updateDailyRoutesTemplate(params.id, { ...template, routes: updatedRoutes }).then(() => { setDeleting(false); setShowDeleteModal(false); navigate(`/trans-routes/daily-templates/view/${params.id}`); }).catch(err => { setDeleting(false); alert('Failed to delete route'); }); }; if (!template || !currentRoute) { return (
Loading...
); } return ( <>
Transportation Daily Route Templates {template.name} {currentRoute.name}

View Route in Template: {currentRoute.name}

Route Name: {currentRoute.name}
Type: {currentRoute.type}
Vehicle: {currentVehicle?.vehicle_number || 'Not assigned'}
Driver: {currentDriver?.name || 'Not assigned'}
Start Mileage: {currentRoute.start_mileage || 'N/A'}
End Mileage: {currentRoute.end_mileage || 'N/A'}
Customers: {currentRoute.route_customer_list?.length || 0}
{currentRoute.route_customer_list && currentRoute.route_customer_list.length > 0 && (
Customer List
)}
setShowDeleteModal(false)}> Delete Route Are you sure you want to delete the route "{currentRoute.name}" from this template? ); }; export default ViewDailyTemplateRoute;