<?php $this->start('body'); ?>
<?php
$status  = $this->raw('status', 'idle');
$driver  = $this->raw('driver', null);
$cardNo  = $this->raw('card_no', '');
$expAt   = $this->raw('expires_at', '');

$statusClass  = 'status-' . htmlspecialchars($status);
$verdictWord  = [
    'valid'     => 'VALID',
    'expired'   => 'EXPIRED',
    'revoked'   => 'REVOKED',
    'unknown'   => 'NOT FOUND',
    'malformed' => 'INVALID FORMAT',
    'idle'      => '',
][$status] ?? '';
$verdictClass = [
    'valid'   => 'verdict-valid',
    'expired' => 'verdict-expired',
    'revoked' => 'verdict-revoked',
][$status] ?? 'verdict-neutral';
?>

<div class="verify-card <?= $statusClass; ?>">
  <div class="verify-status-bar"></div>

  <?php if ($status === 'idle'): ?>
    <div class="text-uppercase small text-muted mb-2" style="letter-spacing:.08em;">Verify Staff ID</div>
    <p class="small text-muted mb-3">Enter or paste the card number printed on the front of the ID (format <span class="font-monospace">PGC-####-A</span>).</p>
    <form action="<?= PROOT; ?>Verify" method="GET" onsubmit="event.preventDefault(); const v=this.card.value.trim(); if(v) window.location.href='<?= PROOT; ?>Verify/'+encodeURIComponent(v);">
      <input class="search-input mb-2" type="text" name="card" placeholder="PGC-0000-A" autocomplete="off" spellcheck="false">
      <button class="btn-verify w-100" type="submit">Verify</button>
    </form>

  <?php elseif ($status === 'malformed' || $status === 'unknown'): ?>
    <div class="d-flex align-items-center justify-content-between mb-2">
      <div class="verdict <?= $verdictClass; ?>"><?= $verdictWord; ?></div>
      <div class="small text-muted font-monospace"><?= htmlspecialchars($cardNo); ?></div>
    </div>
    <div class="small text-muted mb-3">
      <?php if ($status === 'malformed'): ?>
        The card number is not in the expected format. Confirm the guard scanned the full QR code, or type the number by hand.
      <?php else: ?>
        No staff record matches this card number. If the card looks genuine, ask HR to reissue.
      <?php endif; ?>
    </div>
    <a href="<?= PROOT; ?>Verify" class="btn btn-verify w-100">Try again</a>

  <?php else: ?>
    <div class="d-flex align-items-start gap-3">
      <?php if (!empty($driver['photo_url'])): ?>
        <img class="portrait" src="<?= htmlspecialchars($driver['photo_url']); ?>" alt="">
      <?php else: ?>
        <div class="portrait d-flex align-items-center justify-content-center small text-muted">NO PHOTO</div>
      <?php endif; ?>
      <div class="flex-grow-1">
        <div class="verdict <?= $verdictClass; ?>"><?= $verdictWord; ?></div>
        <div class="mt-1" style="font-size:18px; font-weight:700; letter-spacing:.02em;"><?= htmlspecialchars($driver['full_name']); ?></div>
        <div class="small text-muted">Driver &middot; <?= htmlspecialchars($driver['employee_no'] ?? '—'); ?></div>
        <div class="small text-muted mt-1 font-monospace"><?= htmlspecialchars($cardNo); ?></div>
      </div>
    </div>

    <dl class="kv">
      <dt>Licence</dt><dd><?= htmlspecialchars($driver['licence_no'] ?: '—'); ?> <span class="text-muted small ms-1"><?= htmlspecialchars($driver['licence_class'] ?: ''); ?></span></dd>
      <dt>Valid to</dt><dd><?= htmlspecialchars($expAt); ?></dd>
      <?php if (!empty($driver['blood_group'])): ?>
        <dt>Blood</dt><dd><?= htmlspecialchars($driver['blood_group']); ?></dd>
      <?php endif; ?>
      <?php if (!empty($driver['hire_date'])): ?>
        <dt>Since</dt><dd><?= date('d M Y', strtotime($driver['hire_date'])); ?></dd>
      <?php endif; ?>
    </dl>

    <?php if ($status === 'valid'): ?>
      <div class="mt-4 small text-muted">
        <i class="fas fa-check-circle me-1" style="color:#16A34A;"></i>
        This ID is current. If the photo does not match the person presenting the card, treat it as suspicious.
      </div>
    <?php elseif ($status === 'expired'): ?>
      <div class="mt-4 small" style="color:#B45309;">
        <i class="fas fa-clock me-1"></i>
        This card is past its expiry date. Ask HR to reissue.
      </div>
    <?php elseif ($status === 'revoked'): ?>
      <div class="mt-4 small" style="color:#B91C1C;">
        <i class="fas fa-ban me-1"></i>
        This card has been revoked. Do not admit the holder without HR clearance.
      </div>
    <?php endif; ?>

    <a href="<?= PROOT; ?>Verify" class="d-block text-center mt-3 small">Verify another card</a>
  <?php endif; ?>
</div>
<?php $this->end(); ?>
