<?php
$rows = $rows ?? [];
$nOpen = $nDone = $nCancel = 0;
foreach ($rows as $r) {
    $st = (string) ($r['status'] ?? '');
    if ($st === 'fulfilled') {
        $nDone++;
    } elseif ($st === 'cancelled') {
        $nCancel++;
    } else {
        $nOpen++;
    }
}
?>
<div class="cpt-head">
  <div>
    <h1 class="cpt-title">My orders</h1>
    <p class="cpt-lead mb-0">Recent orders for your company.</p>
  </div>
</div>
<?php if ($rows): ?>
<div class="cpt-kpis">
  <div class="cpt-kpi" style="--kpi-accent:#2563eb;--kpi-tint:#eff6ff">
    <span class="cpt-kpi-ico"><i class="bi bi-bag-fill"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $nOpen ?></span><span class="cpt-kpi-l d-block">Open</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#16a34a;--kpi-tint:#f0fdf4">
    <span class="cpt-kpi-ico"><i class="bi bi-check-circle-fill"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $nDone ?></span><span class="cpt-kpi-l d-block">Fulfilled</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#ef4444;--kpi-tint:#fef2f2">
    <span class="cpt-kpi-ico"><i class="bi bi-x-circle-fill"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $nCancel ?></span><span class="cpt-kpi-l d-block">Cancelled</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#0f766e;--kpi-tint:#f0fdfa">
    <span class="cpt-kpi-ico"><i class="bi bi-collection-fill"></i></span>
    <span><span class="cpt-kpi-n"><?= count($rows) ?></span><span class="cpt-kpi-l d-block">All</span></span>
  </div>
</div>
<?php endif; ?>
<?php if (empty($rows)): ?>
  <div class="cpt-empty"><i class="bi bi-bag-check"></i><h2>No orders yet</h2><p class="mb-0">Orders placed for your company will show here.</p></div>
<?php else: foreach ($rows as $r):
    $st = cpt_status_label((string) $r['status']);
?>
  <div class="cpt-item cpt-tone-<?= e($st['tone']) ?>">
    <div class="cpt-item-main">
      <div class="cpt-item-ico"><i class="bi bi-bag-check"></i></div>
      <div class="min-w-0 flex-grow-1">
        <div class="cpt-item-name"><?= e($r['title']) ?></div>
        <div class="cpt-item-sub">
          <span><i class="bi bi-hash"></i> <?= e($r['number']) ?></span>
          <span><i class="bi bi-calendar3"></i> <?= e(dt_fmt($r['ordered_at'])) ?></span>
        </div>
      </div>
      <div class="cpt-item-amt"><strong><?= e(money_fmt($r['amount'])) ?></strong><span>Amount</span></div>
      <span class="<?= e($st['pill']) ?>"><?= e($st['text']) ?></span>
    </div>
  </div>
<?php endforeach; endif; ?>
