<?php
$rows = $rows ?? [];
$nUnpaid = 0; $nPartial = 0; $nPaid = 0; $dueTotal = 0.0;
foreach ($rows as $r) {
    $st = (string) $r['status'];
    $left = max(0, (float) $r['amount'] - (float) $r['paid_amount']);
    if ($st === 'paid') { $nPaid++; }
    elseif ($st === 'partial') { $nPartial++; $dueTotal += $left; }
    elseif ($st !== 'void') { $nUnpaid++; $dueTotal += $left; }
}
?>
<div class="cpt-head">
  <div>
    <h1 class="cpt-title">Invoices</h1>
    <p class="cpt-lead mb-0">Bills and payment status for your company only.</p>
  </div>
</div>
<?php if ($rows): ?>
<div class="cpt-kpis">
  <div class="cpt-kpi" style="--kpi-accent:#ef4444;--kpi-tint:#fef2f2">
    <span class="cpt-kpi-ico"><i class="bi bi-exclamation-circle-fill"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $nUnpaid ?></span><span class="cpt-kpi-l d-block">Unpaid</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#ea580c;--kpi-tint:#fff7ed">
    <span class="cpt-kpi-ico"><i class="bi bi-pie-chart-fill"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $nPartial ?></span><span class="cpt-kpi-l d-block">Partial</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) $nPaid ?></span><span class="cpt-kpi-l d-block">Paid</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#2563eb;--kpi-tint:#eff6ff">
    <span class="cpt-kpi-ico"><i class="bi bi-wallet2"></i></span>
    <span><span class="cpt-kpi-n"><?= e(money_fmt($dueTotal)) ?></span><span class="cpt-kpi-l d-block">Still due</span></span>
  </div>
</div>
<?php endif; ?>
<?php if (empty($rows)): ?>
  <div class="cpt-empty"><i class="bi bi-receipt"></i><h2>No invoices yet</h2><p class="mb-0">When your company sends a bill, it shows here.</p></div>
<?php else: foreach ($rows as $r):
    $stCode = (string) $r['status'];
    $st = cpt_status_label($stCode);
    $amt = (float) $r['amount'];
    $paid = (float) $r['paid_amount'];
    $left = max(0, $amt - $paid);
    $pct = $amt > 0 ? min(100, round(($paid / $amt) * 100)) : ($stCode === 'paid' ? 100 : 0);
    $overdue = !in_array($stCode, ['paid', 'void'], true) && !empty($r['due_date']) && date('Y-m-d', strtotime((string) $r['due_date'])) < date('Y-m-d');
    $tone = $overdue ? 'alert' : $st['tone'];
?>
  <div class="cpt-item cpt-tone-<?= e($tone) ?>">
    <div class="cpt-item-main">
      <div class="cpt-item-ico"><i class="bi bi-receipt"></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> Due <?= e(dt_fmt($r['due_date'])) ?></span>
          <?php if ($overdue): ?><span><i class="bi bi-exclamation-circle"></i> Overdue</span><?php endif; ?>
        </div>
        <div class="cpt-inv-bar cpt-inv-<?= e($stCode) ?>" aria-hidden="true"><span style="width: <?= (int) $pct ?>%"></span></div>
      </div>
      <div class="cpt-item-amt">
        <strong><?= e(money_fmt($stCode === 'paid' ? $amt : $left)) ?></strong>
        <span><?= $stCode === 'paid' ? 'Paid' : 'Still due' ?></span>
      </div>
      <span class="<?= e($overdue ? 'cpt-pill is-unpaid' : $st['pill']) ?>"><?= e($overdue ? 'Overdue' : $st['text']) ?></span>
    </div>
    <?php if (!empty($r['file_name'])): ?>
      <div class="cpt-item-actions">
        <a class="btn btn-sm btn-outline-primary" href="<?= e(url('invoices/' . $r['id'] . '/file')) ?>" target="_blank" rel="noopener noreferrer"><i class="bi bi-file-earmark-pdf"></i> Open PDF</a>
      </div>
    <?php endif; ?>
  </div>
<?php endforeach; endif; ?>
