<?php $rows = $rows ?? []; $waiting = count($rows);
$withPdf = 0;
foreach ($rows as $r) {
    if (!empty($r['file_name'])) {
        $withPdf++;
    }
}
?>
<div class="svc-page">
  <div class="mb-3">
    <h1 class="page-title">Approvals</h1>
    <p class="page-sub mb-0">Say yes or no before a quote or bid goes to the customer. Until you approve, it cannot be sent.</p>
  </div>

  <div class="task-summary mb-3">
    <?php foreach ([['Waiting', $waiting, 'bi-hourglass-split', '#ea580c', '#fff7ed'], ['With PDF', $withPdf, 'bi-file-earmark-pdf', '#2563eb', '#eff6ff'], ['No PDF', $waiting - $withPdf, 'bi-folder2-open', '#7c3aed', '#f5f3ff'], ['Decide now', $waiting, 'bi-patch-check-fill', '#0f766e', '#f0fdfa']] as $s): ?>
      <div class="task-summary-card" style="--task-accent: <?= e($s[3]) ?>; --task-tint: <?= e($s[4]) ?>">
        <div class="task-summary-icon"><i class="bi <?= e($s[2]) ?>"></i></div>
        <div><div class="task-summary-n"><?= (int) $s[1] ?></div><div class="task-summary-l"><?= e($s[0]) ?></div></div>
      </div>
    <?php endforeach; ?>
  </div>

  <?php if (!$rows): ?>
    <div class="card-soft task-empty"><div class="task-empty-icon"><i class="bi bi-patch-check"></i></div><h2>Nothing waiting</h2><p>When a salesperson submits a quote, it shows here.</p></div>
  <?php else: foreach ($rows as $r):
      $st = commerce_status_label('pending_approval', 'Waiting');
  ?>
    <div class="svc-visit svc-tone-<?= e($st['tone']) ?>">
      <a class="svc-visit-main" href="<?= e(url('quotes/' . $r['id'])) ?>">
        <div class="svc-visit-icon"><i class="bi bi-file-earmark-richtext"></i></div>
        <div class="min-w-0 flex-grow-1">
          <div class="svc-visit-name"><?= e($r['title']) ?></div>
          <div class="svc-visit-sub">
            <span><i class="bi bi-hash"></i> <?= e($r['number']) ?></span>
            <?php if (!empty($r['account_name'])): ?><span><i class="bi bi-buildings"></i> <?= e($r['account_name']) ?></span><?php endif; ?>
            <?php if (!empty($r['owner_name'])): ?><span><i class="bi bi-person"></i> <?= e($r['owner_name']) ?></span><?php endif; ?>
          </div>
        </div>
        <div class="task-meta">
          <div class="svc-visit-amt"><strong><?= e(money_fmt($r['amount'])) ?></strong><span>Amount</span></div>
          <span class="due-pill <?= e($st['class']) ?>"><?= e($st['text']) ?></span>
        </div>
      </a>
      <div class="svc-attend d-flex flex-column gap-2 align-items-stretch">
        <?php if (!empty($r['file_name'])): ?>
          <a class="btn btn-sm btn-outline-primary" href="<?= e(url('quotes/' . $r['id'] . '/file')) ?>" target="_blank" rel="noopener noreferrer">Open PDF</a>
        <?php endif; ?>
        <form class="ajax-form" action="<?= e(url('approvals/' . $r['id'])) ?>" method="post">
          <?= csrf_field() ?>
          <input type="hidden" name="decision" value="approve">
          <button class="btn btn-sm btn-primary w-100" type="submit">Approve</button>
        </form>
        <form class="ajax-form d-flex gap-2" action="<?= e(url('approvals/' . $r['id'])) ?>" method="post">
          <?= csrf_field() ?>
          <input type="hidden" name="decision" value="reject">
          <input class="form-control form-control-sm" name="reason" placeholder="Reason" required style="max-width:140px">
          <button class="btn btn-sm btn-outline-danger" type="submit">Reject</button>
        </form>
      </div>
    </div>
  <?php endforeach; endif; ?>
</div>
