170 lines
7.7 KiB
PHP
170 lines
7.7 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$pdo = db();
|
|
$message = null;
|
|
$error = null;
|
|
|
|
// Handle form submission for adding a new vehicle
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$license_plate = trim($_POST['license_plate'] ?? '');
|
|
$model = trim($_POST['model'] ?? '');
|
|
$current_mileage = filter_input(INPUT_POST, 'current_mileage', FILTER_VALIDATE_INT, ["options" => ["min_range"=>0]]);
|
|
$assigned_driver = trim($_POST['assigned_driver'] ?? '');
|
|
$next_service_due = trim($_POST['next_service_due'] ?? '');
|
|
|
|
if (empty($license_plate) || empty($model)) {
|
|
$error = "License plate and model are required.";
|
|
} else {
|
|
try {
|
|
$stmt = $pdo->prepare(
|
|
"INSERT INTO vehicles (license_plate, model, current_mileage, assigned_driver, next_service_due) VALUES (?, ?, ?, ?, ?)"
|
|
);
|
|
$stmt->execute([
|
|
$license_plate,
|
|
$model,
|
|
$current_mileage ?: null,
|
|
$assigned_driver ?: null,
|
|
empty($next_service_due) ? null : $next_service_due
|
|
]);
|
|
$message = "Vehicle '{$license_plate}' added successfully!";
|
|
} catch (PDOException $e) {
|
|
$error = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch all vehicles to display
|
|
$vehicles = [];
|
|
try {
|
|
$stmt = $pdo->query("SELECT id, license_plate, model, current_mileage, assigned_driver, next_service_due FROM vehicles ORDER BY created_at DESC");
|
|
$vehicles = $stmt->fetchAll();
|
|
} catch (PDOException $e) {
|
|
$error = "Could not fetch vehicles: " . $e->getMessage();
|
|
}
|
|
|
|
include 'includes/header.php';
|
|
?>
|
|
|
|
<!-- Toast notifications -->
|
|
<div class="toast-container position-fixed top-0 end-0 p-3">
|
|
<?php if ($message): ?>
|
|
<div class="toast align-items-center text-white bg-success border-0" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="d-flex">
|
|
<div class="toast-body">
|
|
<i class="bi bi-check-circle-fill"></i> <?php echo htmlspecialchars($message); ?>
|
|
</div>
|
|
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<div class="toast align-items-center text-white bg-danger border-0" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="d-flex">
|
|
<div class="toast-body">
|
|
<i class="bi bi-exclamation-triangle-fill"></i> <?php echo htmlspecialchars($error); ?>
|
|
</div>
|
|
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1 class="h2">Fleet Overview</h1>
|
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addVehicleModal">
|
|
<i class="bi bi-plus-circle"></i> Add New Vehicle
|
|
</button>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
All Vehicles
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (empty($vehicles)): ?>
|
|
<div class="empty-state">
|
|
<i class="bi bi-truck-front"></i>
|
|
<h5>No vehicles yet</h5>
|
|
<p>Get started by adding your first vehicle to the fleet.</p>
|
|
<button type="button" class="btn btn-primary mt-2" data-bs-toggle="modal" data-bs-target="#addVehicleModal">
|
|
Add Vehicle
|
|
</button>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th scope="col">License Plate</th>
|
|
<th scope="col">Model</th>
|
|
<th scope="col">Mileage</th>
|
|
<th scope="col">Assigned Driver</th>
|
|
<th scope="col">Next Service Due</th>
|
|
<th scope="col">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($vehicles as $vehicle): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($vehicle['license_plate']); ?></td>
|
|
<td><?php echo htmlspecialchars($vehicle['model']); ?></td>
|
|
<td><?php echo $vehicle['current_mileage'] ? htmlspecialchars(number_format($vehicle['current_mileage'])) . ' km' : 'N/A'; ?></td>
|
|
<td><?php echo $vehicle['assigned_driver'] ? htmlspecialchars($vehicle['assigned_driver']) : 'N/A'; ?></td>
|
|
<td><?php echo $vehicle['next_service_due'] ? htmlspecialchars(date("M d, Y", strtotime($vehicle['next_service_due']))) : 'N/A'; ?></td>
|
|
<td>
|
|
<a href="#" class="btn btn-sm btn-outline-secondary"><i class="bi bi-pencil"></i></a>
|
|
<a href="#" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add Vehicle Modal -->
|
|
<div class="modal fade" id="addVehicleModal" tabindex="-1" aria-labelledby="addVehicleModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form action="vehicles.php" method="POST">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="addVehicleModalLabel">Add New Vehicle</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label for="license_plate" class="form-label">License Plate <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="license_plate" name="license_plate" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="model" class="form-label">Model <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="model" name="model" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="current_mileage" class="form-label">Current Mileage (km)</label>
|
|
<input type="number" class="form-control" id="current_mileage" name="current_mileage" min="0">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="assigned_driver" class="form-label">Assigned Driver</label>
|
|
<input type="text" class="form-control" id="assigned_driver" name="assigned_driver">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="next_service_due" class="form-label">Next Service Due</label>
|
|
<input type="date" class="form-control" id="next_service_due" name="next_service_due">
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" class="btn btn-primary">Save Vehicle</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|