34471-vm/routes.php
Flatlogic Bot 2948106bb3 1.0
2025-09-29 07:42:02 +00:00

178 lines
8.8 KiB
PHP

<?php
include 'includes/header.php';
$message = '';
// Handle form submission for adding a new route
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_route'])) {
$origin = $_POST['origin'] ?? '';
$destination = $_POST['destination'] ?? '';
$distance = $_POST['distance'] ?? null;
$status = $_POST['status'] ?? 'planned';
$departure_time = $_POST['departure_time'] ?? null;
$arrival_time = $_POST['arrival_time'] ?? null;
$driver_id = $_POST['driver_id'] ?? null;
$vehicle_id = $_POST['vehicle_id'] ?? null;
if ($origin && $destination) {
try {
$pdo = db();
$stmt = $pdo->prepare(
'INSERT INTO routes (origin, destination, distance, status, departure_time, arrival_time, driver_id, vehicle_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
);
$stmt->execute([$origin, $destination, $distance, $status, $departure_time, $arrival_time, $driver_id, $vehicle_id]);
$message = '<div class="alert alert-success">Route added successfully!</div>';
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
} else {
$message = '<div class="alert alert-warning">Please fill in all required fields.</div>';
}
}
// Fetch all routes from the database
try {
$pdo = db();
$stmt = $pdo->query('SELECT r.*, d.name as driver_name, v.make, v.model, v.license_plate FROM routes r LEFT JOIN drivers d ON r.driver_id = d.id LEFT JOIN vehicles v ON r.vehicle_id = v.id ORDER BY r.created_at DESC');
$routes = $stmt->fetchAll();
// Fetch drivers and vehicles for the form
$drivers_stmt = $pdo->query('SELECT id, name FROM drivers ORDER BY name');
$drivers = $drivers_stmt->fetchAll();
$vehicles_stmt = $pdo->query('SELECT id, make, model, license_plate FROM vehicles ORDER BY make, model');
$vehicles = $vehicles_stmt->fetchAll();
} catch (PDOException $e) {
$routes = [];
$drivers = [];
$vehicles = [];
$message .= '<div class="alert alert-danger">Error fetching data: ' . $e->getMessage() . '</div>';
}
include 'includes/sidebar.php';
?>
<div class="container-fluid">
<h2 class="mb-4">Route Management</h2>
<?php echo $message; ?>
<div class="card p-3 mb-4">
<div class="d-flex justify-content-between align-items-center mb-3">
<h5 class="mb-0">All Routes</h5>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addRouteModal">+ Add New Route</button>
</div>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Origin</th>
<th>Destination</th>
<th>Distance</th>
<th>Status</th>
<th>Driver</th>
<th>Vehicle</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($routes)): ?>
<tr>
<td colspan="7" class="text-center">No routes found.</td>
</tr>
<?php else: ?>
<?php foreach ($routes as $route): ?>
<tr>
<td><?php echo htmlspecialchars($route['origin']); ?></td>
<td><?php echo htmlspecialchars($route['destination']); ?></td>
<td><?php echo htmlspecialchars($route['distance']); ?> km</td>
<td><span class="badge bg-info"><?php echo htmlspecialchars(ucfirst($route['status'])); ?></span></td>
<td><?php echo htmlspecialchars($route['driver_name'] ?? 'N/A'); ?></td>
<td><?php echo htmlspecialchars(($route['make'] ?? 'N/A') . ' ' . ($route['model'] ?? '') . ' (' . ($route['license_plate'] ?? '') . ')'); ?></td>
<td>
<a href="#" class="btn btn-sm btn-outline-primary"><i data-feather="edit-2"></i></a>
<a href="#" class="btn btn-sm btn-outline-danger"><i data-feather="trash-2"></i></a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Add Route Modal -->
<div class="modal fade" id="addRouteModal" tabindex="-1" aria-labelledby="addRouteModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addRouteModalLabel">Add New Route</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form action="routes.php" method="POST">
<div class="row">
<div class="col-md-6 mb-3">
<label for="origin" class="form-label">Origin</label>
<input type="text" class="form-control" id="origin" name="origin" required>
</div>
<div class="col-md-6 mb-3">
<label for="destination" class="form-label">Destination</label>
<input type="text" class="form-control" id="destination" name="destination" required>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="distance" class="form-label">Distance (km)</label>
<input type="number" step="0.01" class="form-control" id="distance" name="distance">
</div>
<div class="col-md-6 mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" name="status">
<option value="planned">Planned</option>
<option value="in_progress">In Progress</option>
<option value="completed">Completed</option>
<option value="cancelled">Cancelled</option>
</select>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="departure_time" class="form-label">Departure Time</label>
<input type="datetime-local" class="form-control" id="departure_time" name="departure_time">
</div>
<div class="col-md-6 mb-3">
<label for="arrival_time" class="form-label">Arrival Time</label>
<input type="datetime-local" class="form-control" id="arrival_time" name="arrival_time">
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="driver_id" class="form-label">Driver</label>
<select class="form-select" id="driver_id" name="driver_id">
<option value="">Select Driver</option>
<?php foreach ($drivers as $driver): ?>
<option value="<?php echo $driver['id']; ?>"><?php echo htmlspecialchars($driver['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="vehicle_id" class="form-label">Vehicle</label>
<select class="form-select" id="vehicle_id" name="vehicle_id">
<option value="">Select Vehicle</option>
<?php foreach ($vehicles as $vehicle): ?>
<option value="<?php echo $vehicle['id']; ?>"><?php echo htmlspecialchars($vehicle['make'] . ' ' . $vehicle['model'] . ' (' . $vehicle['license_plate'] . ')'); ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<button type="submit" name="add_route" class="btn btn-primary">Add Route</button>
</form>
</div>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>