39038-vm/truck_owner_dashboard.php
2026-03-07 13:28:03 +00:00

111 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/layout.php';
ensure_schema();
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'submit_offer') {
$shipmentId = (int) ($_POST['shipment_id'] ?? 0);
$offerOwner = trim($_POST['offer_owner'] ?? '');
$offerPrice = trim($_POST['offer_price'] ?? '');
if ($shipmentId <= 0 || $offerOwner === '' || $offerPrice === '') {
$errors[] = t('error_required');
} elseif (!is_numeric($offerPrice)) {
$errors[] = t('error_invalid');
}
if (!$errors) {
$stmt = db()->prepare(
"UPDATE shipments SET offer_owner = :offer_owner, offer_price = :offer_price, status = 'offered'
WHERE id = :id AND status IN ('posted','offered')"
);
$stmt->execute([
':offer_owner' => $offerOwner,
':offer_price' => $offerPrice,
':id' => $shipmentId,
]);
if ($stmt->rowCount() > 0) {
set_flash('success', t('success_offer'));
header('Location: ' . url_with_lang('truck_owner_dashboard.php'));
exit;
} else {
$errors[] = t('error_invalid');
}
}
}
$shipments = [];
try {
$stmt = db()->query("SELECT * FROM shipments WHERE status IN ('posted','offered') ORDER BY created_at DESC LIMIT 20");
$shipments = $stmt->fetchAll();
} catch (Throwable $e) {
$shipments = [];
}
render_header(t('owner_dashboard'), 'owner');
$flash = get_flash();
?>
<div class="panel p-3">
<div class="d-flex justify-content-between align-items-center mb-2">
<h2 class="section-title mb-0"><?= e(t('available_shipments')) ?></h2>
<span class="small text-muted"><?= e(count($shipments)) ?> total</span>
</div>
<?php if ($flash): ?>
<div class="alert alert-success" data-auto-dismiss="true"><?= e($flash['message']) ?></div>
<?php endif; ?>
<?php if ($errors): ?>
<div class="alert alert-warning"><?= e(implode(' ', $errors)) ?></div>
<?php endif; ?>
<?php if (!$shipments): ?>
<p class="muted mb-0"><?= e(t('no_shipments')) ?></p>
<?php else: ?>
<div class="table-responsive">
<table class="table align-middle mb-0">
<thead>
<tr>
<th><?= e(t('shipper_company')) ?></th>
<th><?= e(t('origin')) ?></th>
<th><?= e(t('destination')) ?></th>
<th><?= e(t('weight')) ?></th>
<th><?= e(t('offer')) ?></th>
<th><?= e(t('actions')) ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($shipments as $row): ?>
<tr>
<td><?= e($row['shipper_company']) ?></td>
<td><?= e($row['origin_city']) ?></td>
<td><?= e($row['destination_city']) ?></td>
<td><?= e($row['weight_tons']) ?></td>
<td><?= $row['offer_price'] ? e($row['offer_price'] . ' / ' . ($row['offer_owner'] ?? '')) : e(t('no_offers')) ?></td>
<td>
<form class="d-flex flex-column gap-2" method="post">
<input type="hidden" name="action" value="submit_offer">
<input type="hidden" name="shipment_id" value="<?= e($row['id']) ?>">
<input class="form-control form-control-sm" name="offer_owner" placeholder="<?= e(t('offer_owner')) ?>" required>
<input class="form-control form-control-sm" name="offer_price" type="number" step="0.01" min="0.1" placeholder="<?= e(t('offer_price')) ?>" required>
<div class="d-flex gap-2">
<button class="btn btn-sm btn-primary" type="submit"><?= e(t('submit_offer')) ?></button>
<a class="btn btn-sm btn-outline-dark" href="<?= e(url_with_lang('shipment_detail.php', ['id' => $row['id']])) ?>">
<?= e(t('view')) ?>
</a>
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
<?php render_footer(); ?>