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

140 lines
6.2 KiB
PHP

<?php
include 'includes/header.php';
$message = '';
// Handle form submission for adding a new vehicle
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_vehicle'])) {
$make = $_POST['make'] ?? '';
$model = $_POST['model'] ?? '';
$year = $_POST['year'] ?? '';
$license_plate = $_POST['license_plate'] ?? '';
$vin = $_POST['vin'] ?? '';
$status = $_POST['status'] ?? 'active';
if ($make && $model && $year && $license_plate && $vin) {
try {
$pdo = db();
$stmt = $pdo->prepare(
'INSERT INTO vehicles (make, model, year, license_plate, vin, status) VALUES (?, ?, ?, ?, ?, ?)'
);
$stmt->execute([$make, $model, $year, $license_plate, $vin, $status]);
$message = '<div class="alert alert-success">Vehicle 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 vehicles from the database
try {
$pdo = db();
$stmt = $pdo->query('SELECT * FROM vehicles ORDER BY created_at DESC');
$vehicles = $stmt->fetchAll();
} catch (PDOException $e) {
$vehicles = [];
$message .= '<div class="alert alert-danger">Error fetching vehicles: ' . $e->getMessage() . '</div>';
}
include 'includes/sidebar.php';
?>
<div class="container-fluid">
<h2 class="mb-4">Vehicle 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 Vehicles</h5>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addVehicleModal">+ Add New Vehicle</button>
</div>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Make</th>
<th>Model</th>
<th>Year</th>
<th>License Plate</th>
<th>VIN</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($vehicles)): ?>
<tr>
<td colspan="7" class="text-center">No vehicles found.</td>
</tr>
<?php else: ?>
<?php foreach ($vehicles as $vehicle): ?>
<tr>
<td><?php echo htmlspecialchars($vehicle['make']); ?></td>
<td><?php echo htmlspecialchars($vehicle['model']); ?></td>
<td><?php echo htmlspecialchars($vehicle['year']); ?></td>
<td><?php echo htmlspecialchars($vehicle['license_plate']); ?></td>
<td><?php echo htmlspecialchars($vehicle['vin']); ?></td>
<td><span class="badge bg-success"><?php echo htmlspecialchars(ucfirst($vehicle['status'])); ?></span></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 Vehicle Modal -->
<div class="modal fade" id="addVehicleModal" tabindex="-1" aria-labelledby="addVehicleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<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">
<form action="vehicles.php" method="POST">
<div class="mb-3">
<label for="make" class="form-label">Make</label>
<input type="text" class="form-control" id="make" name="make" required>
</div>
<div class="mb-3">
<label for="model" class="form-label">Model</label>
<input type="text" class="form-control" id="model" name="model" required>
</div>
<div class="mb-3">
<label for="year" class="form-label">Year</label>
<input type="number" class="form-control" id="year" name="year" required>
</div>
<div class="mb-3">
<label for="license_plate" class="form-label">License Plate</label>
<input type="text" class="form-control" id="license_plate" name="license_plate" required>
</div>
<div class="mb-3">
<label for="vin" class="form-label">VIN</label>
<input type="text" class="form-control" id="vin" name="vin" required>
</div>
<div class="mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" name="status">
<option value="active">Active</option>
<option value="in_maintenance">In Maintenance</option>
<option value="decommissioned">Decommissioned</option>
</select>
</div>
<button type="submit" name="add_vehicle" class="btn btn-primary">Add Vehicle</button>
</form>
</div>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>