39038-vm/admin_dashboard.php
2026-03-07 17:29:29 +00:00

115 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/layout.php';
ensure_schema();
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'update_status') {
$shipmentId = (int) ($_POST['shipment_id'] ?? 0);
$status = $_POST['status'] ?? 'posted';
$allowed = ['posted', 'offered', 'confirmed', 'in_transit', 'delivered'];
if ($shipmentId <= 0 || !in_array($status, $allowed, true)) {
$errors[] = t('error_invalid');
} else {
$stmt = db()->prepare("UPDATE shipments SET status = :status WHERE id = :id");
$stmt->execute([':status' => $status, ':id' => $shipmentId]);
set_flash('success', t('success_status'));
header('Location: ' . url_with_lang('admin_dashboard.php'));
exit;
}
}
$shipments = [];
try {
$stmt = db()->query("SELECT * FROM shipments ORDER BY created_at DESC LIMIT 30");
$shipments = $stmt->fetchAll();
} catch (Throwable $e) {
$shipments = [];
}
$flash = get_flash();
render_header(t('admin_dashboard'), 'admin');
?>
<div class="row g-4">
<div class="col-lg-3">
<?php render_admin_sidebar('dashboard'); ?>
</div>
<div class="col-lg-9">
<div class="page-intro">
<h1 class="section-title mb-1"><?= e(t('admin_dashboard')) ?></h1>
<p class="muted mb-0">Control shipment status, manage locations, and onboard new users from one place.</p>
</div>
<div class="panel p-4 mb-4">
<h2 class="h5 mb-3">Quick actions</h2>
<div class="d-flex flex-wrap gap-2">
<a href="<?= e(url_with_lang('admin_countries.php')) ?>" class="btn btn-outline-dark">Manage Countries</a>
<a href="<?= e(url_with_lang('admin_cities.php')) ?>" class="btn btn-outline-dark">Manage Cities</a>
<a href="<?= e(url_with_lang('register.php')) ?>" class="btn btn-primary">Register New User</a>
</div>
</div>
<div class="panel p-4">
<div class="d-flex justify-content-between align-items-center mb-2">
<h2 class="section-title h4 mb-0"><?= e(t('admin_dashboard')) ?></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('payment_method')) ?></th>
<th><?= e(t('status')) ?></th>
<th><?= e(t('update_status')) ?></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['payment_method'] === 'bank_transfer' ? t('payment_bank') : t('payment_thawani')) ?></td>
<td><span class="badge <?= e($row['status']) ?>"><?= e(status_label($row['status'])) ?></span></td>
<td>
<form class="d-flex gap-2" method="post">
<input type="hidden" name="action" value="update_status">
<input type="hidden" name="shipment_id" value="<?= e($row['id']) ?>">
<select class="form-select form-select-sm" name="status">
<option value="posted" <?= $row['status'] === 'posted' ? 'selected' : '' ?>><?= e(t('status_posted')) ?></option>
<option value="offered" <?= $row['status'] === 'offered' ? 'selected' : '' ?>><?= e(t('status_offered')) ?></option>
<option value="confirmed" <?= $row['status'] === 'confirmed' ? 'selected' : '' ?>><?= e(t('status_confirmed')) ?></option>
<option value="in_transit" <?= $row['status'] === 'in_transit' ? 'selected' : '' ?>><?= e(t('status_in_transit')) ?></option>
<option value="delivered" <?= $row['status'] === 'delivered' ? 'selected' : '' ?>><?= e(t('status_delivered')) ?></option>
</select>
<button class="btn btn-sm btn-primary" type="submit"><?= e(t('save')) ?></button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php render_footer(); ?>