<?php
require_once __DIR__ . "/../config.inc.php";
require_user_login();

$orders = [];
$stmt = db()->prepare("SELECT id, order_number, status, subtotal, created_at FROM orders WHERE user_id = ? ORDER BY id DESC");
$stmt->bind_param("i", $_SESSION['user_id']);
$stmt->execute();
$res = $stmt->get_result();
while ($r = $res->fetch_assoc()) $orders[] = $r;
$stmt->close();

require_once __DIR__ . "/partials/header.php";
?>

<?php if (!empty($_GET['placed'])): ?>
  <div class="alert alert-success">Order placed successfully. Admin has been notified.</div>
<?php endif; ?>

<div class="card shadow-sm">
  <div class="card-body">
    <h5 class="mb-3">My Orders</h5>

    <?php if (!$orders): ?>
      <div class="text-muted">No orders yet. <a href="index.php">Place an order</a>.</div>
    <?php else: ?>
      <div class="table-responsive">
        <table class="table table-striped align-middle">
          <thead>
            <tr>
              <th>Order #</th>
              <th>Status</th>
              <th>Total</th>
              <th>Date</th>
              <th>Items</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($orders as $o): ?>
              <tr>
                <td class="fw-semibold"><?= e($o['order_number']) ?></td>
                <td><span class="badge bg-<?= $o['status']==='pending'?'warning':'success' ?>"><?= e($o['status']) ?></span></td>
                <td><?= number_format((float)$o['subtotal'], 2) ?></td>
                <td><?= e($o['created_at']) ?></td>
                <td>
                  <?php
                  $items = [];
                  $stmt = db()->prepare("
                      SELECT oi.quantity, oi.unit_price, oi.phone_model, oi.match_source, dt.name AS display_type,
                             p.display_model, b.name AS display_brand
                      FROM order_items oi
                      JOIN display_types dt ON dt.id = oi.display_type_id
                      JOIN products p ON p.id = oi.product_id
                      JOIN brands b ON b.id = p.brand_id
                      WHERE oi.order_id = ?
                    ");
                  $stmt->bind_param("i", $o['id']);
                  $stmt->execute();
                  $r2 = $stmt->get_result();
                  while ($row = $r2->fetch_assoc()) $items[] = $row;
                  $stmt->close();
                  ?>

                  <?php foreach ($items as $it): ?>
                    <div class="small">
                      <?= (int)$it['quantity'] ?> × <?= e($it['display_brand']) ?> / <?= e($it['display_model']) ?>
                      (<?= e($it['display_type']) ?>) —
                      for <?= e($it['phone_model']) ?> [<?= e($it['match_source']) ?>]
                    </div>
                  <?php endforeach; ?>
                </td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    <?php endif; ?>
  </div>
</div>

<?php require_once __DIR__ . "/partials/footer.php"; ?>