39038-vm/truck_owner_dashboard.php
2026-03-24 03:30:29 +00:00

109 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/layout.php'; require_role('truck_owner');
$userId = $_SESSION['user_id'];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_truck') {
$truckType = trim($_POST['truck_type'] ?? '');
$loadCapacity = trim($_POST['load_capacity'] ?? '');
$plateNo = trim($_POST['plate_no'] ?? '');
$regExpiry = trim($_POST['registration_expiry_date'] ?? '');
$insExpiry = trim($_POST['insurance_expiry_date'] ?? '');
if ($truckType === '' || $loadCapacity === '' || $plateNo === '' || $regExpiry === '' || $insExpiry === '') {
set_flash('error', 'All truck details including expiry dates are required.');
} elseif (!is_numeric($loadCapacity) || (float)$loadCapacity <= 0) {
set_flash('error', 'Load capacity must be numeric and greater than zero.');
} else {
$stmt = db()->prepare("INSERT INTO trucks (user_id, truck_type, load_capacity, plate_no, registration_expiry_date, insurance_expiry_date, is_approved) VALUES (?, ?, ?, ?, ?, ?, 0)");
$stmt->execute([$userId, $truckType, $loadCapacity, $plateNo, $regExpiry, $insExpiry]);
set_flash('success', 'Truck added successfully, pending admin approval.');
}
header('Location: ' . url_with_lang('truck_owner_dashboard.php'));
exit;
}
$trucks = db()->prepare("SELECT * FROM trucks WHERE user_id = ?");
$trucks->execute([$userId]);
$myTrucks = $trucks->fetchAll();
render_header('Truck Owner Dashboard', 'owner');
$flash = get_flash();
?>
<div class="page-intro mb-4">
<h1 class="section-title mb-1">Truck Owner Dashboard</h1>
<p class="muted mb-0">Manage your trucks and view their approval status.</p>
</div>
<?php if ($flash): ?>
<div class="alert alert-<?= $flash['type'] ?>" data-auto-dismiss="true"><?= e($flash['message']) ?></div>
<?php endif; ?>
<div class="panel p-4 mb-4">
<h5 class="mb-3">Add New Truck</h5>
<form method="post" class="row g-3"> <?= csrf_field() ?>
<input type="hidden" name="action" value="add_truck">
<div class="col-md-2">
<input type="text" name="truck_type" class="form-control" placeholder="Truck Type" required>
</div>
<div class="col-md-2">
<input type="number" name="load_capacity" class="form-control" placeholder="Capacity (T)" step="0.01" required>
</div>
<div class="col-md-2">
<input type="text" name="plate_no" class="form-control" placeholder="Plate No" required>
</div>
<div class="col-md-2">
<input type="date" name="registration_expiry_date" class="form-control" required title="Registration Expiry Date">
</div>
<div class="col-md-2">
<input type="date" name="insurance_expiry_date" class="form-control" required title="Insurance Expiry Date">
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-primary w-100">Add Truck</button>
</div>
</form>
</div>
<div class="panel p-4">
<h5 class="mb-3">My Trucks</h5>
<table class="table table-bordered">
<thead>
<tr>
<th>Truck Type</th>
<th>Capacity (T)</th>
<th>Plate No</th>
<th>Reg Expiry</th>
<th>Ins Expiry</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($myTrucks as $truck): ?>
<?php
$isExpired = (strtotime($truck['registration_expiry_date']) < time()) || (strtotime($truck['insurance_expiry_date']) < time());
?>
<tr class="<?= $isExpired ? 'table-danger' : '' ?>">
<td><?= e($truck['truck_type']) ?></td>
<td><?= e($truck['load_capacity']) ?></td>
<td><?= e($truck['plate_no']) ?></td>
<td><?= e($truck['registration_expiry_date']) ?></td>
<td><?= e($truck['insurance_expiry_date']) ?></td>
<td>
<?php if ($isExpired): ?>
<span class="badge bg-danger">Expired</span>
<?php elseif ($truck['is_approved']): ?>
<span class="badge bg-success">Approved</span>
<?php else: ?>
<span class="badge bg-warning text-dark">Pending</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php render_footer(); ?>