<?php
$page_title = 'Shipment Tracking';
require_once __DIR__ . '/header.php';

$shipments = $db->query("SELECT * FROM orders WHERE customer_id={$dealer['id']} AND order_status IN ('processing', 'shipped', 'delivered') ORDER BY created_at DESC")->fetchAll();
?>

<div class="card">
  <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
    <h3 style="margin:0;color:#0f172a;">My Shipments</h3>
    <span style="font-size:.85rem;color:#64748b;">Track your recent orders and delivery status</span>
  </div>
  
  <div class="table-wrap" style="margin-top:0;">
    <table>
      <thead>
        <tr>
          <th>Order ID</th>
          <th>Order Date</th>
          <th>Status</th>
          <th>Logistics Partner</th>
          <th>Tracking Number</th>
          <th>Dispatch Date</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($shipments as $s): ?>
        <tr>
          <td><strong>#<?= str_pad($s['id'], 5, '0', STR_PAD_LEFT) ?></strong></td>
          <td><?= date('d M Y', strtotime($s['created_at'])) ?></td>
          <td>
            <?php 
              $c = 'info';
              if($s['order_status']==='delivered') $c='success';
              if($s['order_status']==='pending') $c='warning';
            ?>
            <span class="badge badge-<?= $c ?>"><?= ucfirst($s['order_status']) ?></span>
          </td>
          <td style="color:#475569;font-weight:500;">
            <?= $s['logistics_partner'] ? h($s['logistics_partner']) : '<span style="color:#94a3b8;font-size:.85rem;">Not Assigned</span>' ?>
          </td>
          <td>
            <?php if($s['tracking_number']): ?>
              <span style="background:#f1f5f9;padding:4px 8px;border-radius:4px;font-family:monospace;letter-spacing:1px;color:#0f172a;border:1px solid #cbd5e1;">
                <?= h($s['tracking_number']) ?>
              </span>
            <?php else: ?>
              <span style="color:#94a3b8;font-size:.85rem;">Pending</span>
            <?php endif; ?>
          </td>
          <td style="color:#64748b;font-size:.9rem;">
            <?= $s['shipped_at'] ? date('d M Y', strtotime($s['shipped_at'])) : '-' ?>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if(!$shipments): ?>
        <tr><td colspan="6" style="text-align:center;padding:30px;color:#64748b;">No active shipments to track.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?php require_once __DIR__ . '/footer.php'; ?>
