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

165 lines
7.6 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/layout.php';
ensure_schema();
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'create_shipment') {
$shipperName = trim($_POST['shipper_name'] ?? '');
$shipperCompany = trim($_POST['shipper_company'] ?? '');
$origin = trim($_POST['origin_city'] ?? '');
$destination = trim($_POST['destination_city'] ?? '');
$cargo = trim($_POST['cargo_description'] ?? '');
$weight = trim($_POST['weight_tons'] ?? '');
$pickupDate = trim($_POST['pickup_date'] ?? '');
$deliveryDate = trim($_POST['delivery_date'] ?? '');
$payment = $_POST['payment_method'] ?? 'thawani';
if ($shipperName === '' || $shipperCompany === '' || $origin === '' || $destination === '' || $cargo === '' || $weight === '' || $pickupDate === '' || $deliveryDate === '') {
$errors[] = t('error_required');
} elseif (!is_numeric($weight)) {
$errors[] = t('error_invalid');
}
if (!$errors) {
$stmt = db()->prepare(
"INSERT INTO shipments (shipper_name, shipper_company, origin_city, destination_city, cargo_description, weight_tons, pickup_date, delivery_date, payment_method)
VALUES (:shipper_name, :shipper_company, :origin_city, :destination_city, :cargo_description, :weight_tons, :pickup_date, :delivery_date, :payment_method)"
);
$stmt->execute([
':shipper_name' => $shipperName,
':shipper_company' => $shipperCompany,
':origin_city' => $origin,
':destination_city' => $destination,
':cargo_description' => $cargo,
':weight_tons' => $weight,
':pickup_date' => $pickupDate,
':delivery_date' => $deliveryDate,
':payment_method' => $payment,
]);
$_SESSION['shipper_name'] = $shipperName;
set_flash('success', t('success_shipment'));
header('Location: ' . url_with_lang('shipper_dashboard.php'));
exit;
}
}
$shipments = [];
try {
$stmt = db()->query("SELECT * FROM shipments ORDER BY created_at DESC LIMIT 20");
$shipments = $stmt->fetchAll();
} catch (Throwable $e) {
$shipments = [];
}
render_header(t('shipper_dashboard'), 'shipper');
$flash = get_flash();
?>
<div class="row g-4">
<div class="col-lg-5">
<div class="panel p-3">
<h2 class="section-title"><?= e(t('new_shipment')) ?></h2>
<?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; ?>
<form method="post">
<input type="hidden" name="action" value="create_shipment">
<div class="mb-3">
<label class="form-label"><?= e(t('shipper_name')) ?></label>
<input class="form-control" name="shipper_name" value="<?= e($_SESSION['shipper_name'] ?? '') ?>" required>
</div>
<div class="mb-3">
<label class="form-label"><?= e(t('shipper_company')) ?></label>
<input class="form-control" name="shipper_company" required>
</div>
<div class="mb-3">
<label class="form-label"><?= e(t('origin')) ?></label>
<input class="form-control" name="origin_city" required>
</div>
<div class="mb-3">
<label class="form-label"><?= e(t('destination')) ?></label>
<input class="form-control" name="destination_city" required>
</div>
<div class="mb-3">
<label class="form-label"><?= e(t('cargo')) ?></label>
<input class="form-control" name="cargo_description" required>
</div>
<div class="row g-3">
<div class="col-md-6">
<label class="form-label"><?= e(t('weight')) ?></label>
<input class="form-control" name="weight_tons" type="number" step="0.01" min="0.1" required>
</div>
<div class="col-md-6">
<label class="form-label"><?= e(t('payment_method')) ?></label>
<select class="form-select" name="payment_method">
<option value="thawani"><?= e(t('payment_thawani')) ?></option>
<option value="bank_transfer"><?= e(t('payment_bank')) ?></option>
</select>
</div>
</div>
<div class="row g-3 mt-1">
<div class="col-md-6">
<label class="form-label"><?= e(t('pickup_date')) ?></label>
<input class="form-control" name="pickup_date" type="date" required>
</div>
<div class="col-md-6">
<label class="form-label"><?= e(t('delivery_date')) ?></label>
<input class="form-control" name="delivery_date" type="date" required>
</div>
</div>
<button class="btn btn-primary w-100 mt-4" type="submit"><?= e(t('submit_shipment')) ?></button>
</form>
</div>
</div>
<div class="col-lg-7">
<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('shipments_list')) ?></h2>
<span class="small text-muted"><?= e(count($shipments)) ?> total</span>
</div>
<?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('origin')) ?></th>
<th><?= e(t('destination')) ?></th>
<th><?= e(t('status')) ?></th>
<th><?= e(t('offer')) ?></th>
<th><?= e(t('actions')) ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($shipments as $row): ?>
<tr>
<td><?= e($row['origin_city']) ?></td>
<td><?= e($row['destination_city']) ?></td>
<td><span class="badge-status <?= e($row['status']) ?>"><?= e(status_label($row['status'])) ?></span></td>
<td><?= $row['offer_price'] ? e($row['offer_price'] . ' / ' . ($row['offer_owner'] ?? '')) : e(t('no_offers')) ?></td>
<td>
<a class="btn btn-sm btn-outline-dark" href="<?= e(url_with_lang('shipment_detail.php', ['id' => $row['id']])) ?>">
<?= e(t('view')) ?>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php render_footer(); ?>