<?php // voucher log tab — included by claims.php ?>
<?php if ($tab === 'vouchers'): ?>
<!-- ── VOUCHER LOG TAB ── -->

<div class="sec-hd">
  <div class="sec-hd-title"><i class="bi bi-ticket-perforated-fill"></i>Voucher Log
    <span class="chip chip-muted" style="font-size:11px"><?= number_format($vTotal) ?> vouchers</span>
  </div>
  <a href="?tab=vouchers&export=csv" class="btn-export"><i class="bi bi-download"></i>Export CSV</a>
</div>

<form method="GET" class="log-search">
  <input type="hidden" name="tab" value="vouchers">
  <input type="text" name="vs" class="frm-ctrl" placeholder="Search event name, code or type…" value="<?= htmlspecialchars($vSearch) ?>">
  <button class="btn-accent" style="padding:9px 16px"><i class="bi bi-search"></i></button>
  <?php if($vSearch): ?><a href="?tab=vouchers" class="btn-ghost" style="padding:9px 14px"><i class="bi bi-x"></i></a><?php endif; ?>
</form>

<div class="adm-card" style="padding:0;overflow:hidden">
  <?php if(empty($voucherRows)): ?>
  <div class="log-empty">
    <i class="bi bi-ticket-perforated"></i>
    <p><?= $vSearch ? 'No vouchers match your search.' : 'No vouchers created yet.' ?></p>
  </div>
  <?php else: ?>
  <div class="table-responsive">
  <table class="adm-table">
    <thead>
      <tr>
        <th>Voucher</th>
        <th>Code</th>
        <th>Period</th>
        <th>Visibility</th>
        <th>Claims</th>
        <th>Redeemed</th>
        <th>Users</th>
        <th>Status</th>
        <th>Created</th>
      </tr>
    </thead>
    <tbody>
    <?php foreach($voucherRows as $r):
      $isExp = $r['end_date'] < $today;
      $isUp  = $r['start_date'] > $today;
      $st    = $isExp ? 'expired' : ($isUp ? 'upcoming' : 'active');
      $pct   = $r['total_claims'] > 0 ? round($r['redeemed']/$r['total_claims']*100) : 0;
    ?>
    <tr>
      <td>
        <div style="font-weight:600;font-size:13px"><?= htmlspecialchars($r['event_name']) ?></div>
        <?php if($r['type']): ?><div style="font-size:11px;color:var(--muted)"><?= htmlspecialchars($r['type']) ?></div><?php endif; ?>
      </td>
      <td><span style="font-family:var(--mono);font-size:11px;background:var(--bg);color:var(--accent);padding:3px 8px;border-radius:5px;border:1px solid var(--border)"><?= htmlspecialchars($r['code']) ?></span></td>
      <td style="font-size:11.5px;color:var(--muted);white-space:nowrap">
        <?= $r['start_date'] ? date('d M y', strtotime($r['start_date'])) : '—' ?><br>→ <?= $r['end_date'] ? date('d M y', strtotime($r['end_date'])) : '—' ?>
      </td>
      <td>
        <?php if($r['is_public']): ?>
          <span class="chip chip-green"><i class="bi bi-globe2"></i> Public</span>
        <?php else: ?>
          <span class="chip chip-muted"><i class="bi bi-lock"></i> Private</span>
        <?php endif; ?>
      </td>
      <td style="font-family:var(--mono);font-size:14px;font-weight:700;color:var(--accent);text-align:center"><?= $r['total_claims'] ?></td>
      <td>
        <div style="font-family:var(--mono);font-size:14px;font-weight:700;color:#2dbd87;text-align:center"><?= $r['redeemed'] ?></div>
        <?php if($r['total_claims']>0): ?>
        <div style="font-size:10px;color:var(--muted);text-align:center"><?= $pct ?>%</div>
        <?php endif; ?>
      </td>
      <td style="font-family:var(--mono);font-size:14px;font-weight:700;color:#4b9eff;text-align:center"><?= $r['unique_users'] ?></td>
      <td><span class="vs-pill vs-<?= $st ?>"><?= ucfirst($st) ?></span></td>
      <td style="font-size:11.5px;color:var(--muted);white-space:nowrap"><?= date('d M Y', strtotime($r['created_at'])) ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
  </div>
  <div class="pg-bar">
    <span><?= number_format($vTotal) ?> vouchers · Page <?= $vPage ?> of <?= $vPages ?></span>
    <div class="pg-nums"><?= pgLinks($vPage, $vPages, '?tab=vouchers'.($vSearch?'&vs='.urlencode($vSearch):'').'&vp=') ?></div>
  </div>
  <?php endif; ?>
</div>

<?php endif; ?>

