<?php
require __DIR__ . '/../_boot.php';
require __DIR__ . '/../partials/nav.php';
if (!$user || !in_array($user['role_name'], ['super_admin','event_manager','walk_leader'], true)) { header('Location: ../login-dev.php'); exit; }
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_wide_pages.css">
  <link rel="stylesheet" href="../assets/css/admin_dashboard_active_walks.css?v=1">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container admin-wide-page dashboard-admin-page">
  <section class="page-hero admin-hero dashboard-hero">
    <div>
      <p class="eyebrow">Pyrk Interface</p>
      <h1>Welcome back, <?= htmlspecialchars(explode(' ', trim($user['full_name']))[0] ?? 'Admin') ?></h1>
      <p class="muted">Your operation center for routes, walks, attendance, members, waivers, photos, votes, and rewards.</p>
    </div>
    <div class="hero-actions">
      <a class="btn" href="walks.php">Schedule walk</a>
      <?php if ($user['role_name'] === 'super_admin'): ?><a class="btn btn-light" href="users.php?create=1">Create user</a><?php endif; ?>
      <?php if (in_array($user['role_name'], ['super_admin','event_manager'], true)): ?><a class="btn btn-light" href="members.php?create=1">Create member</a><?php endif; ?>
    </div>
  </section>

  <div id="dashboardLoadMsg"></div>
  <section class="grid four compact-stats" id="reportStats">
    <div class="stat skeleton"></div><div class="stat skeleton"></div><div class="stat skeleton"></div><div class="stat skeleton"></div>
  </section>

  <section class="grid admin-dashboard-grid">
    <div class="panel dashboard-card-large active-walks-dashboard-panel">
      <div class="section-head active-walks-dashboard-head">
        <div>
          <p class="eyebrow">Live registrations</p>
          <h2>Active walk registrations</h2>
          <p class="muted small">Current and upcoming walks only. Completed and archived registrations are excluded from these numbers.</p>
        </div>
        <a class="btn btn-small" href="walks.php">Manage walks</a>
      </div>
      <div id="activeWalkSummary" class="active-walk-summary">
        <div class="active-summary-card skeleton"></div><div class="active-summary-card skeleton"></div><div class="active-summary-card skeleton"></div><div class="active-summary-card skeleton"></div>
      </div>
      <div id="upcomingWalks" class="active-walk-registration-list"></div>
    </div>

    <div class="panel quick-actions-panel">
      <div class="section-head"><div><p class="eyebrow">Fast actions</p><h2>Shortcuts</h2></div></div>
      <div class="quick-actions-grid">
        <?php if ($user['role_name'] === 'super_admin'): ?><a href="users.php?create=1"><strong>＋ Create system user</strong><span>Add admin, manager or leader</span></a><?php endif; ?>
        <a href="members.php?create=1"><strong>👤 Create member</strong><span>Add client profile and emergency data</span></a>
        <a href="routes.php"><strong>🗺️ Create route</strong><span>Add checkpoints and stations</span></a>
        <a href="attendance.php"><strong>▣ QR check-in</strong><span>Scan attendees on arrival</span></a>
        <a href="photos.php"><strong>📸 Photo contest</strong><span>Approve photos and winners</span></a>
        <a href="loyalty.php"><strong>⭐ Loyalty & badges</strong><span>Manage points and achievements</span></a>
      </div>
    </div>
  </section>

  <section class="grid two">
    <div class="panel">
      <div class="section-head"><div><p class="eyebrow">Community</p><h2>Top walkers</h2></div></div>
      <div class="table-wrap"><table><thead><tr><th>Name</th><th>Walks</th><th>KM</th><th>Points</th></tr></thead><tbody id="topWalkers"></tbody></table></div>
    </div>

    <div class="panel">
      <div class="section-head"><div><p class="eyebrow">Moderation</p><h2>Pending photos</h2></div><a class="btn btn-small" href="photos.php">Review</a></div>
      <div id="pendingPhotos" class="mini-list"></div>
    </div>
  </section>

  <section class="panel">
    <div class="section-head"><div><p class="eyebrow">Newest community members</p><h2>Recent members</h2></div><a class="btn btn-small" href="members.php">Open members</a></div>
    <div id="recentMembers" class="member-row-list"></div>
  </section>
</main>
<script>window.PYRK_API_BASE='../../api'; window.PYRK_CSRF='<?= htmlspecialchars($csrf) ?>';</script>
<script src="../assets/js/api.js"></script>
<script src="../assets/js/admin_dashboard.js?v=3-active-walk-registrations"></script>
</body>
</html>
