<?php
$rows = $rows ?? [];
$waiting = count($rows);
$withPdf = 0;
foreach ($rows as $r) {
    if (!empty($r['file_name'])) {
        $withPdf++;
    }
}
?>
<div class="cpt-head">
  <div>
    <h1 class="cpt-title">Sign a paper</h1>
    <p class="cpt-lead mb-0">Open the PDF, sign it, then upload the signed file. Your name and the signed PDF are required.</p>
  </div>
</div>
<div class="cpt-kpis is-3">
  <div class="cpt-kpi" style="--kpi-accent:#ea580c;--kpi-tint:#fff7ed">
    <span class="cpt-kpi-ico"><i class="bi bi-pen-fill"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $waiting ?></span><span class="cpt-kpi-l d-block">Waiting</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#2563eb;--kpi-tint:#eff6ff">
    <span class="cpt-kpi-ico"><i class="bi bi-file-earmark-pdf"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $withPdf ?></span><span class="cpt-kpi-l d-block">PDFs to open</span></span>
  </div>
  <div class="cpt-kpi" style="--kpi-accent:#0f766e;--kpi-tint:#f0fdfa">
    <span class="cpt-kpi-ico"><i class="bi bi-upload"></i></span>
    <span><span class="cpt-kpi-n"><?= (int) $waiting ?></span><span class="cpt-kpi-l d-block">To upload</span></span>
  </div>
</div>
<?php if (empty($rows)): ?>
  <div class="cpt-empty"><i class="bi bi-pen"></i><h2>Nothing waiting</h2><p class="mb-0">When someone sends a paper to sign, it shows here.</p></div>
<?php else: foreach ($rows as $r): ?>
  <form class="cpt-compose mb-3" method="post" action="<?= e(url('sign/' . $r['id'])) ?>" enctype="multipart/form-data">
    <?= csrf_field() ?>
    <div class="cpt-compose-head">
      <span class="cpt-compose-ico"><i class="bi bi-pen"></i></span>
      <div>
        <div class="cpt-compose-kicker">Signature needed</div>
        <h2><?= e($r['title']) ?></h2>
        <p>Read the paper, upload your signed PDF, type your full name, and confirm.</p>
      </div>
    </div>
    <div class="cpt-compose-body">
      <?php if (!empty($r['body'])): ?><p><?= nl2br(e((string) $r['body'])) ?></p><?php endif; ?>
      <?php if (!empty($r['file_name'])): ?>
        <p>
          <a class="btn btn-sm btn-outline-primary" href="<?= e(url('documents/' . $r['document_id'] . '/file')) ?>" target="_blank" rel="noopener noreferrer"><i class="bi bi-box-arrow-up-right"></i> Open PDF</a>
        </p>
      <?php endif; ?>
      <label class="form-label">Signed PDF *</label>
      <input class="form-control mb-2" type="file" name="signed_pdf" accept="application/pdf,.pdf" required>
      <div class="muted mb-3">PDF only, up to 8 MB. Your company will open this file as the signed copy.</div>
      <label class="form-label">Your full name *</label>
      <input class="form-control mb-2" name="signer_name" required placeholder="As you would sign it">
      <label class="form-check mb-3"><input class="form-check-input" type="checkbox" name="agree" value="1" required> I agree to sign this paper for my company.</label>
      <div class="cpt-compose-foot">
        <div></div>
        <button class="btn btn-primary" type="submit"><i class="bi bi-check2"></i> Sign and upload</button>
      </div>
    </div>
  </form>
<?php endforeach; endif; ?>
