39038-vm/truck_owner_dashboard.php
2026-03-24 06:27:15 +00:00

177 lines
9.2 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/layout.php'; require_role('truck_owner');
$userId = $_SESSION['user_id'];
$settings = get_settings();
$pricingModel = $settings['pricing_model'] ?? 'percentage';
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();
// Fetch Available Shipments
$availShipments = [];
try {
$stmt = db()->query("SELECT * FROM shipments WHERE status = 'posted' ORDER BY created_at DESC");
$availShipments = $stmt->fetchAll();
} catch (Throwable $e) {}
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 available shipments.</p>
</div>
<?php if ($flash): ?>
<div class="alert alert-<?= $flash['type'] ?>" data-auto-dismiss="true"><?= e($flash['message']) ?></div>
<?php endif; ?>
<div class="row g-4">
<div class="col-lg-4">
<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-12">
<label class="form-label small text-muted">Truck Type</label>
<input type="text" name="truck_type" class="form-control" placeholder="e.g. Refrigerated" required>
</div>
<div class="col-6">
<label class="form-label small text-muted">Capacity (T)</label>
<input type="number" name="load_capacity" class="form-control" step="0.01" required>
</div>
<div class="col-6">
<label class="form-label small text-muted">Plate No</label>
<input type="text" name="plate_no" class="form-control" required>
</div>
<div class="col-6">
<label class="form-label small text-muted">Reg. Expiry</label>
<input type="date" name="registration_expiry_date" class="form-control" required>
</div>
<div class="col-6">
<label class="form-label small text-muted">Ins. Expiry</label>
<input type="date" name="insurance_expiry_date" class="form-control" required>
</div>
<div class="col-12 pt-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>
<?php if (!$myTrucks): ?>
<p class="text-muted small">No trucks registered yet.</p>
<?php else: ?>
<div class="list-group list-group-flush">
<?php foreach ($myTrucks as $truck): ?>
<?php
$isExpired = (strtotime($truck['registration_expiry_date']) < time()) || (strtotime($truck['insurance_expiry_date']) < time());
?>
<div class="list-group-item px-0 py-3">
<div class="d-flex justify-content-between align-items-center mb-1">
<strong class="text-dark"><?= e($truck['plate_no']) ?></strong>
<?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; ?>
</div>
<div class="small text-muted"><?= e($truck['truck_type']) ?> • <?= e($truck['load_capacity']) ?> Tons</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
<div class="col-lg-8">
<div class="panel p-4 h-100">
<h5 class="mb-3">Available Shipments</h5>
<?php if (!$availShipments): ?>
<div class="text-center p-5 text-muted">
<i class="bi bi-inbox fs-1 mb-3 d-block opacity-50"></i>
<p class="mb-0">No shipments available at the moment.</p>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table align-middle mb-0">
<thead>
<tr>
<th class="text-uppercase small text-muted border-top-0">Route</th>
<th class="text-uppercase small text-muted border-top-0">Details</th>
<th class="text-uppercase small text-muted border-top-0">Dates</th>
<th class="text-uppercase small text-muted border-top-0 text-end">Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($availShipments as $row): ?>
<tr>
<td>
<div class="d-flex align-items-center gap-2">
<span class="fw-medium"><?= e($row['origin_city']) ?></span>
<i class="bi bi-arrow-right text-muted small"></i>
<span class="fw-medium"><?= e($row['destination_city']) ?></span>
</div>
<?php if (!empty($row['shipment_type'])): ?>
<div class="text-xs text-muted mt-1">
<span class="badge bg-light text-secondary border"><?= e(t('type_' . strtolower($row['shipment_type']))) ?></span>
</div>
<?php endif; ?>
<?php if (!empty($row['target_price'])): ?>
<div class="text-success small fw-bold mt-1">
Price: $<?= e($row['target_price']) ?>
</div>
<?php endif; ?>
</td>
<td>
<div class="small text-dark fw-bold"><?= e($row['cargo_description']) ?></div>
<div class="small text-muted"><?= e($row['weight_tons']) ?> Tons • <?= e($row['payment_method']) ?></div>
</td>
<td>
<div class="small"><span class="text-muted">Pick:</span> <?= e(date('M d', strtotime($row['pickup_date']))) ?></div>
<div class="small"><span class="text-muted">Drop:</span> <?= e(date('M d', strtotime($row['delivery_date']))) ?></div>
</td>
<td class="text-end">
<a href="<?= e(url_with_lang('shipment_detail.php', ['id' => $row['id']])) ?>" class="btn btn-sm btn-outline-primary rounded-pill px-3">
View
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php render_footer(); ?>