<?php
$rows = $rows ?? [];
$tab = $tab ?? 'quotes';
$counts = $counts ?? ['quotes' => 0, 'contracts' => 0, 'files' => 0];
$tabs = [
    ['quotes', 'Quotes', 'bi-file-earmark-richtext', (int) ($counts['quotes'] ?? 0), '#2563eb', '#eff6ff'],
    ['contracts', 'Contracts', 'bi-file-earmark-ruled', (int) ($counts['contracts'] ?? 0), '#0f766e', '#f0fdfa'],
    ['files', 'Files', 'bi-folder2-open', (int) ($counts['files'] ?? 0), '#7c3aed', '#f5f3ff'],
];
$empty = [
    'quotes' => ['No quotes yet', 'Quotes shared with your company will land here.'],
    'contracts' => ['No contracts yet', 'Contracts shared with your company will land here.'],
    'files' => ['No files yet', 'PDFs shared with your company will land here.'],
];
$emptyCopy = $empty[$tab] ?? $empty['files'];
$icos = ['quotes' => 'bi-file-earmark-richtext', 'contracts' => 'bi-file-earmark-ruled', 'files' => 'bi-folder2-open'];
$ico = $icos[$tab] ?? 'bi-file-earmark-text';
$all = (int) ($counts['quotes'] ?? 0) + (int) ($counts['contracts'] ?? 0) + (int) ($counts['files'] ?? 0);
?>
<div class="cpt-head">
  <div>
    <h1 class="cpt-title">Documents</h1>
    <p class="cpt-lead mb-0">Quotes, contracts, and files your company shared with you.</p>
  </div>
</div>
<div class="cpt-kpis">
  <?php foreach ([...$tabs, ['all', 'All', 'bi-collection', $all, '#ea580c', '#fff7ed']] as $k): ?>
    <div class="cpt-kpi" style="--kpi-accent: <?= e($k[4]) ?>; --kpi-tint: <?= e($k[5]) ?>">
      <span class="cpt-kpi-ico"><i class="bi <?= e($k[2]) ?>"></i></span>
      <span><span class="cpt-kpi-n"><?= (int) $k[3] ?></span><span class="cpt-kpi-l d-block"><?= e($k[1]) ?></span></span>
    </div>
  <?php endforeach; ?>
</div>
<nav class="cpt-tabs" aria-label="Document type">
  <?php foreach ($tabs as $t): ?>
    <a class="cpt-tab <?= $tab === $t[0] ? 'is-on' : '' ?>" href="<?= e(url('documents') . '?tab=' . $t[0]) ?>">
      <i class="bi <?= e($t[2]) ?>"></i>
      <span><?= e($t[1]) ?></span>
      <em><?= (int) $t[3] ?></em>
    </a>
  <?php endforeach; ?>
</nav>
<?php if (empty($rows)): ?>
  <div class="cpt-empty"><i class="bi <?= e($ico) ?>"></i><h2><?= e($emptyCopy[0]) ?></h2><p class="mb-0"><?= e($emptyCopy[1]) ?></p></div>
<?php else: foreach ($rows as $r):
    $signSt = (string) ($r['sign_status'] ?? '');
    $st = $signSt !== '' ? cpt_status_label($signSt) : ['text' => 'File', 'tone' => $tab === 'quotes' ? 'open' : ($tab === 'contracts' ? 'done' : 'open'), 'pill' => 'cpt-pill'];
    if ($signSt === 'signed') {
        $st['tone'] = 'signed';
    }
?>
  <div class="cpt-item cpt-tone-<?= e($st['tone']) ?>">
    <div class="cpt-item-main">
      <div class="cpt-item-ico"><i class="bi <?= e($ico) ?>"></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-calendar3"></i> <?= e(dt_fmt($r['created_at'])) ?></span>
          <?php if (!empty($r['file_name'])): ?><span><i class="bi bi-file-earmark-pdf"></i> PDF</span><?php endif; ?>
        </div>
        <?php if (!empty($r['body'])): ?>
          <div class="cpt-item-need"><?= e((string) $r['body']) ?></div>
        <?php endif; ?>
      </div>
      <?php if ($signSt !== ''): ?>
        <span class="<?= e($st['pill']) ?>"><?= e($st['text']) ?></span>
      <?php endif; ?>
    </div>
    <div class="cpt-item-actions">
      <?php if (!empty($r['file_name'])): ?>
        <a class="btn btn-sm btn-outline-primary" href="<?= e(url('documents/' . $r['id'] . '/file')) ?>" target="_blank" rel="noopener noreferrer"><i class="bi bi-file-earmark-pdf"></i> Open PDF</a>
      <?php endif; ?>
      <?php if ($signSt === 'signed' && !empty($r['signed_file_name'])): ?>
        <a class="btn btn-sm btn-primary" href="<?= e(url('documents/' . $r['id'] . '/signed')) ?>" target="_blank" rel="noopener noreferrer"><i class="bi bi-pen"></i> Signed PDF</a>
      <?php endif; ?>
    </div>
  </div>
<?php endforeach; endif; ?>
