<?php
require __DIR__ . '/../_boot.php';
require __DIR__ . '/../partials/nav.php';
$canViewWaivers = $user && (
    \App\Core\Auth::hasPermission('members.view', $user)
    || \App\Core\Auth::hasPermission('attendance.manage', $user)
    || \App\Core\Auth::hasPermission('walks.view_all', $user)
    || (($user['role_name'] ?? '') === 'super_admin')
);
if (!$user || !$canViewWaivers) { header('Location: dashboard.php'); exit; }
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Signed Waivers | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_waivers_pro.css?v=1">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container enterprise-page waivers-admin-page">
  <section class="page-hero admin-hero dashboard-hero enterprise-hero waiver-hero-pro">
    <div>
      <p class="eyebrow">Legal records</p>
      <h1>Signed waivers</h1>
      <p class="muted">View every signed member waiver by walk, member, signature, timestamp, IP address and device. This is read-only for audit safety.</p>
    </div>
    <div class="hero-actions">
      <a class="btn btn-light" href="members.php">Members CRM</a>
      <a class="btn btn-light" href="walks.php">Walks</a>
    </div>
  </section>

  <section class="grid four compact-stats waiver-stats-grid" id="waiverStatsGrid">
    <div class="stat stat-pro"><span>Total signed</span><strong id="waiverTotal">0</strong></div>
    <div class="stat stat-pro"><span>Signed today</span><strong id="waiverToday">0</strong></div>
    <div class="stat stat-pro"><span>Walks covered</span><strong id="waiverWalks">0</strong></div>
    <div class="stat stat-pro"><span>Members signed</span><strong id="waiverMembers">0</strong></div>
  </section>

  <section class="panel enterprise-table-panel waivers-command-panel">
    <div class="section-head waiver-section-head">
      <div>
        <p class="eyebrow">Archive</p>
        <h2>Waiver signatures</h2>
        <p class="muted small">Search by member, email, mobile, walk, route or signed name.</p>
      </div>
      <div class="inline-controls search-controls waiver-filters">
        <input id="waiverSearch" placeholder="Search signed waivers...">
        <select id="waiverWalkFilter"><option value="">All walks</option></select>
        <button class="btn btn-small" type="button" id="waiverRefreshBtn">Refresh</button>
      </div>
    </div>

    <div class="waiver-help-card">
      <strong>Where to check a member waiver?</strong>
      <span>Open this page, or open <b>Members CRM → Profile → Signed waivers</b>. Each row has View / Print.</span>
    </div>

    <div class="table-wrap pro-table-wrap waivers-table-wrap">
      <table class="pro-table waivers-pro-table">
        <thead>
          <tr>
            <th>Member</th>
            <th>Walk</th>
            <th>Signed as</th>
            <th>Signed at</th>
            <th>Device / IP</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody id="waiversTableBody"><tr><td colspan="6" class="muted">Loading signed waivers...</td></tr></tbody>
      </table>
    </div>
  </section>
</main>

<div class="modal-backdrop" id="waiverDetailModal" hidden>
  <div class="modal-card modal-large waiver-detail-modal" role="dialog" aria-modal="true" aria-labelledby="waiverDetailTitle">
    <div class="modal-head">
      <div>
        <p class="eyebrow">Signed waiver</p>
        <h2 id="waiverDetailTitle">Waiver details</h2>
      </div>
      <button class="icon-btn" type="button" data-close-modal="waiverDetailModal">×</button>
    </div>
    <div id="waiverDetailContent" class="waiver-detail-content">
      <p class="muted">Loading waiver...</p>
    </div>
  </div>
</div>

<script>
window.PYRK_API_BASE='../../api';
window.PYRK_CSRF='<?= htmlspecialchars($csrf) ?>';
window.PYRK_WAIVER_OPEN_ID=<?= (int)($_GET['waiver_id'] ?? 0) ?>;
window.PYRK_WAIVER_MEMBER_ID=<?= (int)($_GET['member_id'] ?? 0) ?>;
window.PYRK_WAIVER_WALK_ID=<?= (int)($_GET['walk_id'] ?? 0) ?>;
</script>
<script src="../assets/js/api.js?v=4"></script>
<script src="../assets/js/admin_waivers.js?v=1"></script>
</body>
</html>
