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

// Handle delete
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
    $pdo->prepare('DELETE FROM shipments WHERE id=?')->execute([$_GET['delete']]);
    header('Location: shipments.php?deleted=1'); exit;
}

$search = trim($_GET['q'] ?? '');
if ($search) {
    $stmt = $pdo->prepare('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
        WHERE s.tracking_number LIKE ? OR sn.full_name LIKE ? OR r.full_name LIKE ?
        ORDER BY s.created_at DESC');
    $like = "%$search%";
    $stmt->execute([$like,$like,$like]);
} else {
    $stmt = $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');
}
$shipments = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Shipments | 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>
    .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 style="background:#FF440A;padding:0 20px;position:fixed;top:0;width:100%;z-index:999;display:flex;align-items:center;justify-content:space-between;height:56px;">
    <a href="dashboard.php" style="color:#fff;font-weight:700;font-size:18px;text-decoration:none;">Turi Express Admin</a>
    <div><span style="color:#fff;margin-right:16px;"><?= htmlspecialchars($_SESSION['admin_name']) ?></span><a href="logout.php" style="color:#fff;border:1px solid #fff;padding:5px 12px;border-radius:4px;font-size:13px;text-decoration:none;">Logout</a></div>
  </nav>
  <div style="display:flex;padding-top:56px;">
    <nav style="background:#2c2c2c;min-height:100vh;width:220px;position:fixed;padding-top:20px;">
      <ul style="list-style:none;padding:0;">
        <li style="padding:8px 20px;"><a href="dashboard.php" style="color:#ccc;text-decoration:none;">Dashboard</a></li>
        <li style="padding:8px 20px;"><a href="shipments.php" style="color:#FF440A;text-decoration:none;font-weight:600;">Shipments</a></li>
        <li style="padding:8px 20px;"><a href="add-shipment.php" style="color:#ccc;text-decoration:none;">Add Shipment</a></li>
        <li style="padding:8px 20px;"><a href="../track.php" style="color:#ccc;text-decoration:none;" target="_blank">View Website</a></li>
      </ul>
    </nav>
    <div style="margin-left:220px;padding:30px;flex:1;">
      <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;">
        <h3 style="margin:0;">All Shipments</h3>
        <a href="add-shipment.php" style="background:#FF440A;color:#fff;padding:8px 16px;border-radius:4px;text-decoration:none;">+ Add Shipment</a>
      </div>
      <?php if (isset($_GET['deleted'])): ?><div style="background:#d4edda;padding:10px 16px;border-radius:4px;margin-bottom:16px;color:#155724;">Shipment deleted successfully.</div><?php endif ?>
      <form method="GET" style="margin-bottom:16px;display:flex;gap:10px;">
        <input type="text" name="q" value="<?= htmlspecialchars($search) ?>" placeholder="Search tracking #, sender, receiver..." style="flex:1;padding:8px 12px;border:1px solid #ddd;border-radius:4px;">
        <button type="submit" style="background:#FF440A;color:#fff;border:none;padding:8px 16px;border-radius:4px;cursor:pointer;">Search</button>
        <?php if ($search): ?><a href="shipments.php" style="padding:8px 16px;border:1px solid #ddd;border-radius:4px;color:#555;text-decoration:none;">Clear</a><?php endif ?>
      </form>
      <div style="background:#fff;border-radius:8px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,.08);overflow-x:auto;">
        <table class="table table-hover">
          <thead style="background:#f5f5f5;">
            <tr><th>Tracking #</th><th>Sender</th><th>Receiver</th><th>Route</th><th>Ship Date</th><th>Status</th><th style="width:120px;">Actions</th></tr>
          </thead>
          <tbody>
          <?php foreach ($shipments 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 style="font-size:12px;"><?= htmlspecialchars($s['origin_city']) ?> → <?= htmlspecialchars($s['destination_city']) ?></td>
            <td><?= date('d M Y', strtotime($s['ship_date'])) ?></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="color:#FF440A;font-size:12px;">Edit</a> |
              <a href="shipments.php?delete=<?= $s['id'] ?>" onclick="return confirm('Delete this shipment?')" style="color:#e74c3c;font-size:12px;">Delete</a>
            </td>
          </tr>
          <?php endforeach ?>
          <?php if (empty($shipments)): ?><tr><td colspan="7" style="text-align:center;color:#888;padding:30px;">No shipments found.</td></tr><?php endif ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<script src="vendors/base/vendor.bundle.base.js"></script>
</body>
</html>
