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

<div class="sec-hd">
  <div class="sec-hd-title"><i class="bi bi-check2-circle"></i>Redeem Log
    <span class="chip chip-green" style="font-size:11px"><?= number_format($rTotal) ?> records</span>
  </div>
  <div style="display:flex;gap:8px;align-items:center">
    <a href="?tab=redeem&export=csv" class="btn-export"><i class="bi bi-download"></i>Export CSV</a>
  </div>
</div>

<form method="GET" class="log-search">
  <input type="hidden" name="tab" value="redeem">
  <input type="text" name="rs" class="frm-ctrl" placeholder="Search customer, phone, voucher or code…" value="<?= htmlspecialchars($rSearch) ?>">
  <button class="btn-accent" style="padding:9px 16px"><i class="bi bi-search"></i></button>
  <?php if($rSearch): ?><a href="?tab=redeem" 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($redeemRows)): ?>
  <div class="log-empty">
    <i class="bi bi-check2-circle"></i>
    <p><?= $rSearch ? 'No redemptions match your search.' : 'No redemptions yet.' ?></p>
  </div>
  <?php else: ?>
  <div class="table-responsive">
  <table class="adm-table">
    <thead>
      <tr>
        <th>#</th>
        <th>Customer</th>
        <th>Voucher</th>
        <th>Code</th>
        <th>Claimed At</th>
        <th>Redeemed At</th>
        <th>Turnaround</th>
      </tr>
    </thead>
    <tbody>
    <?php foreach($redeemRows as $i => $r):
      $turnSec = $r['redeemed_at'] ? strtotime($r['redeemed_at']) - strtotime($r['claimed_at']) : null;
      if($turnSec !== null) {
        if($turnSec < 3600)       $turn = floor($turnSec/60).'m';
        elseif($turnSec < 86400)  $turn = floor($turnSec/3600).'h '.floor(($turnSec%3600)/60).'m';
        else                      $turn = floor($turnSec/86400).'d';
      } else $turn = '—';
    ?>
    <tr>
      <td style="font-family:var(--mono);font-size:11px;color:var(--muted)"><?= $r['id'] ?></td>
      <td>
        <div style="font-weight:600"><?= htmlspecialchars($r['name']) ?></div>
        <div style="font-size:11px;color:var(--muted);font-family:var(--mono)"><?= htmlspecialchars($r['phone']) ?></div>
      </td>
      <td>
        <div style="font-size:13px;font-weight:500"><?= 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:12px;color:var(--muted);white-space:nowrap"><?= fmtDate($r['claimed_at']) ?></td>
      <td style="font-size:12px;white-space:nowrap">
        <span style="color:#2dbd87;font-weight:600"><?= fmtDate($r['redeemed_at']) ?></span>
      </td>
      <td>
        <span class="chip chip-<?= $turnSec!==null&&$turnSec<3600?'green':($turnSec!==null&&$turnSec<86400?'yellow':'muted') ?>">
          <i class="bi bi-clock"></i> <?= $turn ?>
        </span>
      </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
  </div>
  <div class="pg-bar">
    <span><?= number_format($rTotal) ?> redemptions · Page <?= $rPage ?> of <?= $rPages ?></span>
    <div class="pg-nums"><?= pgLinks($rPage, $rPages, '?tab=redeem'.($rSearch?'&rs='.urlencode($rSearch):'').'&rp=') ?></div>
  </div>
  <?php endif; ?>
</div>

<?php endif; ?>
