<?php
$rows = $rows ?? [];
$withSerial = 0;
$withLoc = 0;
$withInstall = 0;
foreach ($rows as $r) {
    if (trim((string) ($r['serial_no'] ?? '')) !== '') {
        $withSerial++;
    }
    if (trim((string) ($r['location'] ?? '')) !== '') {
        $withLoc++;
    }
    if (!empty($r['installed_at'])) {
        $withInstall++;
    }
}
?>
<div class="cpt-head">
  <div>
    <h1 class="cpt-title">My equipment</h1>
    <p class="cpt-lead mb-0">Machines serviced at your site.</p>
  </div>
</div>
<?php if ($rows): ?>
<div class="cpt-kpis">
  <div class="cpt-kpi" style="--kpi-accent:#0f766e;--kpi-tint:#f0fdfa">
    <span class="cpt-kpi-ico"><i class="bi bi-cpu-fill"></i></span>
    <span><span class="cpt-kpi-n"><?= count($rows) ?></span><span class="cpt-kpi-l d-block">Machines</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#2563eb;--kpi-tint:#eff6ff">
    <span class="cpt-kpi-ico"><i class="bi bi-upc"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $withSerial ?></span><span class="cpt-kpi-l d-block">With serial</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#ea580c;--kpi-tint:#fff7ed">
    <span class="cpt-kpi-ico"><i class="bi bi-geo"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $withLoc ?></span><span class="cpt-kpi-l d-block">With location</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#7c3aed;--kpi-tint:#f5f3ff">
    <span class="cpt-kpi-ico"><i class="bi bi-calendar3"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $withInstall ?></span><span class="cpt-kpi-l d-block">Installed date</span></span>
  </div>
</div>
<?php endif; ?>
<?php if (empty($rows)): ?>
  <div class="cpt-empty"><i class="bi bi-cpu"></i><h2>No equipment listed</h2><p class="mb-0">Machines on your site will show here.</p></div>
<?php else: foreach ($rows as $r):
    $st = cpt_status_label((string) ($r['status'] ?? 'active'));
    $model = trim((string) ($r['model'] ?? ''));
    $serial = trim((string) ($r['serial_no'] ?? ''));
    $loc = trim((string) ($r['location'] ?? ''));
    $installed = !empty($r['installed_at']) ? dt_fmt($r['installed_at']) : '';
?>
  <div class="cpt-item cpt-tone-<?= e($st['tone']) ?>">
    <div class="cpt-item-main">
      <div class="cpt-item-ico"><i class="bi bi-cpu"></i></div>
      <div class="min-w-0 flex-grow-1">
        <div class="cpt-item-name"><?= e($r['name']) ?></div>
        <div class="cpt-item-sub">
          <?php if ($model !== ''): ?><span><i class="bi bi-tag"></i> <?= e($model) ?></span><?php endif; ?>
          <?php if ($serial !== ''): ?><span><i class="bi bi-upc"></i> <?= e($serial) ?></span><?php endif; ?>
          <?php if ($loc !== ''): ?><span><i class="bi bi-geo"></i> <?= e($loc) ?></span><?php endif; ?>
          <?php if ($installed !== '' && $installed !== '—'): ?><span><i class="bi bi-calendar3"></i> <?= e($installed) ?></span><?php endif; ?>
        </div>
      </div>
      <span class="<?= e($st['pill']) ?>"><?= e($st['text']) ?></span>
    </div>
  </div>
<?php endforeach; endif; ?>
