<?php
// admin/report_print.php — print-friendly / PDF-ready attendance report
require_once __DIR__ . '/../config.php';
adminGuard();

$db      = db();
$eventId = (int)($_GET['event_id'] ?? 0);

$ev = $db->prepare('SELECT * FROM events WHERE id=? AND admin_id=?');
$ev->execute([$eventId, $_SESSION['admin_id']]);
$event = $ev->fetch();
if (!$event) { header('Location: ' . BASE_URL . '/admin/events.php'); exit; }

$q = function (string $sql) use ($db, $eventId) { $s = $db->prepare($sql); $s->execute([$eventId]); return $s; };

$total   = (int)$q('SELECT COUNT(*) FROM guests WHERE event_id=?')->fetchColumn();
$checked = (int)$q('SELECT COUNT(*) FROM guests WHERE event_id=? AND checked_in=1')->fetchColumn();
$rate    = $total ? round($checked / $total * 100, 1) : 0;

$byCat = $q('SELECT COALESCE(NULLIF(category,""),"Uncategorized") cat, COUNT(*) total, SUM(checked_in) chk
             FROM guests WHERE event_id=? GROUP BY cat ORDER BY total DESC')->fetchAll();

$byChecker = $db->prepare('SELECT c.name, COUNT(*) c FROM guests g JOIN checkers c ON c.id=g.checked_by
                           WHERE g.event_id=? AND g.checked_in=1 GROUP BY c.id ORDER BY c DESC');
$byChecker->execute([$eventId]);
$byChecker = $byChecker->fetchAll();

$guests = $q('SELECT g.*, c.name checker FROM guests g LEFT JOIN checkers c ON c.id=g.checked_by
              WHERE g.event_id=? ORDER BY g.checked_in DESC, g.name ASC')->fetchAll();

$org = $_SESSION['admin_name'] ?? 'RSVP System';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attendance Report — <?= htmlspecialchars($event['title']) ?></title>
<style>
  * { box-sizing: border-box; }
  body { font-family: Georgia, 'Times New Roman', serif; color: #16161c; margin: 32px; font-size: 13px; }
  h1 { font-size: 22px; margin: 0 0 4px; }
  h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .08em; color: #6a6a78; margin: 26px 0 8px; border-bottom: 1px solid #ddd; padding-bottom: 4px; }
  .sub { color: #6a6a78; margin-bottom: 20px; }
  .cards { display: flex; gap: 14px; margin: 16px 0; }
  .card { flex: 1; border: 1px solid #ddd; border-radius: 8px; padding: 12px 16px; }
  .card .v { font-size: 26px; font-weight: bold; }
  .card .l { color: #6a6a78; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
  table { width: 100%; border-collapse: collapse; margin-top: 6px; }
  th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid #eee; font-size: 12px; }
  th { background: #f5f5f2; font-family: Arial, sans-serif; text-transform: uppercase; font-size: 10px; letter-spacing: .05em; color: #555; }
  .badge { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 10px; font-family: Arial, sans-serif; }
  .in { background: #e3f6ec; color: #1c7a4a; }
  .out { background: #f1f1f1; color: #888; }
  .toolbar { position: fixed; top: 12px; right: 12px; }
  .btn { font-family: Arial, sans-serif; padding: 8px 16px; border: 1px solid #c59e59; background: #c59e59; color: #fff; border-radius: 6px; cursor: pointer; text-decoration: none; font-size: 12px; }
  @media print { .toolbar { display: none; } body { margin: 0; } }
</style>
</head>
<body>
  <div class="toolbar"><a class="btn" href="javascript:window.print()">🖨 Print / Save PDF</a></div>

  <h1><?= htmlspecialchars($event['title']) ?></h1>
  <div class="sub">
    Attendance Report ·
    <?= $event['event_date'] ? htmlspecialchars(fmtEventTime($event['event_date'], $event, 'l, F j, Y · g:i A')) : 'No date set' ?>
    <?= $event['venue'] ? ' · ' . htmlspecialchars($event['venue']) : '' ?><br>
    Generated <?= htmlspecialchars(fmtEventTime(date('Y-m-d H:i:s'), $event, 'M j, Y · g:i A')) ?>
    (<?= htmlspecialchars(eventTz($event)) ?>) by <?= htmlspecialchars($org) ?>
  </div>

  <div class="cards">
    <div class="card"><div class="v"><?= $total ?></div><div class="l">Total Guests</div></div>
    <div class="card"><div class="v"><?= $checked ?></div><div class="l">Checked In</div></div>
    <div class="card"><div class="v"><?= $total - $checked ?></div><div class="l">Pending</div></div>
    <div class="card"><div class="v"><?= $rate ?>%</div><div class="l">Attendance</div></div>
  </div>

  <h2>By Category</h2>
  <table>
    <thead><tr><th>Category</th><th>Total</th><th>Checked In</th><th>Rate</th></tr></thead>
    <tbody>
      <?php foreach ($byCat as $r): $rt = $r['total'] ? round($r['chk'] / $r['total'] * 100) : 0; ?>
        <tr><td><?= htmlspecialchars($r['cat']) ?></td><td><?= $r['total'] ?></td><td><?= (int)$r['chk'] ?></td><td><?= $rt ?>%</td></tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <?php if ($byChecker): ?>
  <h2>By Checker</h2>
  <table>
    <thead><tr><th>Checker</th><th>Check-ins</th></tr></thead>
    <tbody>
      <?php foreach ($byChecker as $r): ?><tr><td><?= htmlspecialchars($r['name']) ?></td><td><?= $r['c'] ?></td></tr><?php endforeach; ?>
    </tbody>
  </table>
  <?php endif; ?>

  <h2>Full Guest Log (<?= $total ?>)</h2>
  <table>
    <thead><tr><th>#</th><th>Name</th><th>Category</th><th>Table</th><th>Status</th><th>Time</th><th>Method</th><th>By</th></tr></thead>
    <tbody>
      <?php foreach ($guests as $i => $g): ?>
      <tr>
        <td><?= $i + 1 ?></td>
        <td><?= htmlspecialchars($g['name']) ?></td>
        <td><?= htmlspecialchars($g['category'] ?? '') ?></td>
        <td><?= htmlspecialchars($g['table_number'] ?? '') ?></td>
        <td><?php if ($g['checked_in']): ?><span class="badge in">Checked In</span><?php else: ?><span class="badge out">Pending</span><?php endif; ?></td>
        <td><?= $g['check_in_time'] ? htmlspecialchars(fmtEventTime($g['check_in_time'], $event, 'M j · g:i A')) : '—' ?></td>
        <td><?= htmlspecialchars(strtoupper($g['check_in_method'] ?? '')) ?></td>
        <td><?= htmlspecialchars($g['checker'] ?? '') ?></td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</body>
</html>
