<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/db.php';
require_login();

// Stats
$total     = $pdo->query('SELECT COUNT(*) FROM shipments')->fetchColumn();
$delivered = $pdo->query("SELECT COUNT(*) FROM shipments WHERE status='delivered'")->fetchColumn();
$transit   = $pdo->query("SELECT COUNT(*) FROM shipments WHERE status='in_transit'")->fetchColumn();
$on_hold   = $pdo->query("SELECT COUNT(*) FROM shipments WHERE status='on_hold'")->fetchColumn();

// Recent shipments
$recent = $pdo->query('SELECT s.*,sn.full_name AS sender_name,r.full_name AS receiver_name
    FROM shipments s
    LEFT JOIN senders sn ON sn.shipment_id=s.id
    LEFT JOIN receivers r ON r.shipment_id=s.id
    ORDER BY s.created_at DESC LIMIT 10')->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Dashboard | Turi Express Admin</title>
  <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
  <link rel="stylesheet" href="vendors/base/vendor.bundle.base.css">
  <link rel="stylesheet" href="css/style.css">
  <link rel="shortcut icon" href="images/favicon.png">
  <style>
    .stat-card{background:#fff;border-radius:8px;padding:24px;box-shadow:0 2px 8px rgba(0,0,0,.08);text-align:center}
    .stat-card .num{font-size:36px;font-weight:700;color:#FF440A}
    .stat-card .lbl{font-size:13px;color:#888;margin-top:4px}
    .badge-status{padding:4px 10px;border-radius:12px;font-size:12px;font-weight:600}
    .bs-delivered{background:#d4edda;color:#155724}
    .bs-in_transit{background:#cce5ff;color:#004085}
    .bs-on_hold{background:#f8d7da;color:#721c24}
    .bs-order_placed{background:#e2e3e5;color:#383d41}
    .bs-out_for_delivery{background:#fff3cd;color:#856404}
    .bs-returned,.bs-cancelled{background:#343a40;color:#fff}
  </style>
</head>
<body>
<div class="container-scroller">
  <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row" style="background:#FF440A;padding:0 20px!important;">
    <div class="text-center navbar-brand-wrapper d-flex align-items-center" style="width:250px;">
      <a href="dashboard.php" style="color:#fff;font-weight:700;font-size:18px;text-decoration:none;">Turi Express Admin</a>
    </div>
    <div class="navbar-menu-wrapper d-flex align-items-center justify-content-end" style="flex:1;padding:10px 0;">
      <span style="color:#fff;margin-right:20px;">Welcome, <?= htmlspecialchars($_SESSION['admin_name']) ?></span>
      <a href="logout.php" style="color:#fff;text-decoration:none;border:1px solid #fff;padding:6px 14px;border-radius:4px;font-size:13px;">Logout</a>
    </div>
  </nav>

  <div class="container-fluid page-body-wrapper" style="padding-top:60px;">
    <nav class="sidebar sidebar-offcanvas" style="background:#2c2c2c;min-height:100vh;width:250px;position:fixed;padding-top:20px;">
      <ul class="nav" style="list-style:none;padding:0;">
        <li style="padding:8px 20px;"><a href="dashboard.php" style="color:#ccc;text-decoration:none;"><i class="mdi mdi-home"></i> Dashboard</a></li>
        <li style="padding:8px 20px;"><a href="shipments.php" style="color:#ccc;text-decoration:none;"><i class="mdi mdi-package-variant"></i> Shipments</a></li>
        <li style="padding:8px 20px;"><a href="add-shipment.php" style="color:#ccc;text-decoration:none;"><i class="mdi mdi-plus-circle"></i> Add Shipment</a></li>
        <li style="padding:8px 20px;"><a href="../track.php" style="color:#ccc;text-decoration:none;" target="_blank"><i class="mdi mdi-earth"></i> View Website</a></li>
      </ul>
    </nav>

    <div class="main-panel" style="margin-left:250px;padding:30px;">
      <h3 style="margin-bottom:24px;">Dashboard Overview</h3>

      <div class="row" style="margin-bottom:30px;">
        <div class="col-md-3"><div class="stat-card"><div class="num"><?= $total ?></div><div class="lbl">Total Shipments</div></div></div>
        <div class="col-md-3"><div class="stat-card"><div class="num" style="color:#27ae60;"><?= $delivered ?></div><div class="lbl">Delivered</div></div></div>
        <div class="col-md-3"><div class="stat-card"><div class="num" style="color:#2980b9;"><?= $transit ?></div><div class="lbl">In Transit</div></div></div>
        <div class="col-md-3"><div class="stat-card"><div class="num" style="color:#e74c3c;"><?= $on_hold ?></div><div class="lbl">On Hold</div></div></div>
      </div>

      <div style="background:#fff;border-radius:8px;padding:24px;box-shadow:0 2px 8px rgba(0,0,0,.08);">
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
          <h5 style="margin:0;">Recent Shipments</h5>
          <a href="add-shipment.php" style="background:#FF440A;color:#fff;padding:8px 16px;border-radius:4px;text-decoration:none;font-size:13px;">+ Add Shipment</a>
        </div>
        <table class="table table-hover">
          <thead style="background:#f5f5f5;">
            <tr><th>Tracking #</th><th>Sender</th><th>Receiver</th><th>Origin</th><th>Destination</th><th>Status</th><th>Actions</th></tr>
          </thead>
          <tbody>
          <?php foreach ($recent as $s): $st=str_replace(' ','_',strtolower($s['status'])); ?>
          <tr>
            <td><strong><?= htmlspecialchars($s['tracking_number']) ?></strong></td>
            <td><?= htmlspecialchars($s['sender_name'] ?? '—') ?></td>
            <td><?= htmlspecialchars($s['receiver_name'] ?? '—') ?></td>
            <td><?= htmlspecialchars($s['origin_city'].', '.$s['origin_country']) ?></td>
            <td><?= htmlspecialchars($s['destination_city'].', '.$s['destination_country']) ?></td>
            <td><span class="badge-status bs-<?= $st ?>"><?= ucwords(str_replace('_',' ',$s['status'])) ?></span></td>
            <td>
              <a href="edit-shipment.php?id=<?= $s['id'] ?>" style="font-size:12px;color:#FF440A;">Edit</a> |
              <a href="../track.php" target="_blank" style="font-size:12px;color:#555;">View</a>
            </td>
          </tr>
          <?php endforeach ?>
          <?php if (empty($recent)): ?><tr><td colspan="7" style="text-align:center;color:#888;">No shipments yet.</td></tr><?php endif ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<script src="vendors/base/vendor.bundle.base.js"></script>
<script src="js/template.js"></script>
</body>
</html>
