<?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: dashboard.php'); exit; }
$canManage = in_array($user['role_name'], ['super_admin','event_manager'], true);
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Badges | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_badge_cancel_pro.css?v=1">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container enterprise-page badges-admin-container rewards-suite-page">
  <section class="page-hero admin-hero enterprise-hero rewards-suite-hero">
    <div>
      <p class="eyebrow">Achievement engine</p>
      <h1>🏅 Badges</h1>
      <p class="muted">Create badge rules, edit achievement levels, and award badges manually to members.</p>
    </div>
    <div class="hero-actions">
      <a class="btn btn-light" href="loyalty.php">Loyalty points</a>
      <a class="btn btn-light" href="prizes.php">Prizes</a>
      <?php if ($canManage): ?><button class="btn" type="button" id="newBadgeBtn">+ New badge</button><?php endif; ?>
    </div>
  </section>

  <section class="grid four compact-stats">
    <div class="stat stat-pro"><span>Total badges</span><strong id="badgeTotal">0</strong></div>
    <div class="stat stat-pro"><span>Active</span><strong id="badgeActive">0</strong></div>
    <div class="stat stat-pro"><span>Manual</span><strong id="badgeManual">0</strong></div>
    <div class="stat stat-pro"><span>Auto rules</span><strong id="badgeAuto">0</strong></div>
  </section>

  <section class="grid rewards-suite-grid">
    <article class="panel enterprise-table-panel rewards-library-panel">
      <div class="section-head crm-section-head">
        <div>
          <p class="eyebrow">Badge library</p>
          <h2>All badge rules</h2>
          <p class="muted small">Click edit to update a badge. Automatic badges are evaluated from kilometers and walk count.</p>
        </div>
        <div class="inline-controls search-controls">
          <input id="badgeSearch" placeholder="Search badges">
          <select id="badgeStatusFilter"><option value="">All status</option><option value="active">Active</option><option value="inactive">Inactive</option></select>
        </div>
      </div>
      <div id="badgeGrid" class="badge-management-grid pro-badge-grid"></div>
    </article>

    <aside class="panel enterprise-form-panel sticky-panel rewards-side-panel">
      <?php if ($canManage): ?>
      <div class="section-head">
        <div><p class="eyebrow">Create / edit</p><h2 id="badgeFormTitle">Create badge</h2></div>
      </div>
      <form id="badgeForm" class="stack-form">
        <input type="hidden" name="id" id="badgeId">
        <label>Badge name<input name="badge_name" id="badgeName" required placeholder="50 KM Club"></label>
        <div class="form-grid two">
          <label>Icon / emoji<input name="badge_icon" id="badgeIcon" placeholder="🏅"></label>
          <label>Status<select name="status" id="badgeStatus"><option value="active">Active</option><option value="inactive">Inactive</option></select></label>
          <section class="badge-auto-group-box">
            <label class="pro-switch auto-group-switch">
              <input type="checkbox" name="auto_group_enabled" id="badgeAutoGroupEnabled" value="1">
              <span><b></b></span>
              <strong>Auto-add members to a group when this badge is earned</strong>
            </label>
            <label>Target group
              <select name="auto_group_id" id="badgeAutoGroupId">
                <option value="">No group selected</option>
              </select>
            </label>
            <small class="muted">When the badge is earned manually or automatically, the member is added to this group. Existing badge earners are synced after saving.</small>
          </section>
        </div>
        <label>Rule type<select name="rule_type" id="badgeRuleType"><option value="total_km">Total kilometers</option><option value="total_walks">Total walks</option><option value="manual">Manual only</option><option value="photo_winner">Photo winner</option></select></label>
        <div class="form-grid two">
          <label>Rule value<input name="rule_value" id="badgeRuleValue" type="number" step="0.01" placeholder="Example: 50"></label>
          <label>Points reward<input name="points_reward" id="badgePointsReward" type="number" placeholder="Example: 100"></label>
        </div>
        <label>Description<textarea name="description" id="badgeDescription" placeholder="Describe how members earn this badge"></textarea></label>
        <div class="inline-controls"><button class="btn" type="submit">Save badge</button><button class="btn btn-light" type="button" id="resetBadgeForm">Clear</button></div>
        <p id="badgeMsg" class="notice"></p>
      </form>
      <hr>
      <div class="section-head"><div><p class="eyebrow">Manual award</p><h2>Award badge</h2></div></div>
      <form id="awardBadgeForm" class="stack-form">
        <label>Member<select name="member_id" id="badgeMember" required></select></label>
        <label>Badge<select name="badge_id" id="badgeSelect" required></select></label>
        <button class="btn btn-light" type="submit">Award selected badge</button>
        <p id="awardBadgeMsg" class="notice"></p>
      </form>

      <hr>
      <div class="section-head"><div><p class="eyebrow">Cancel awarded badge</p><h2>Remove badge + points</h2><p class="muted small">Select a member and remove a badge that was already awarded. The badge reward points are deducted automatically.</p></div></div>
      <form id="cancelBadgeForm" class="stack-form badge-cancel-form">
        <label>Member<select id="cancelBadgeMember" name="member_id"></select></label>
        <label>Awarded badge<select id="cancelBadgeAward" name="award_id" required></select></label>
        <div id="cancelBadgePreview" class="cancel-badge-preview"></div>
        <button class="btn btn-danger" type="submit">Cancel badge and remove points</button>
        <p id="cancelBadgeMsg" class="notice"></p>
      </form>

      <hr>
      <button class="btn btn-secondary" type="button" id="evaluateAllBadges">Evaluate automatic badges</button>
      <?php else: ?>
      <p class="muted">Walk leaders can view badge rules. Super Admin and Event Manager can create or edit badges.</p>
      <?php endif; ?>
    </aside>
  </section>
</main>
<script>
window.PYRK_API_BASE='../../api';
window.PYRK_CSRF='<?= htmlspecialchars($csrf) ?>';
window.PYRK_CAN_MANAGE_LOYALTY=<?= $canManage ? 'true' : 'false' ?>;
window.PYRK_PUBLIC_BASE='../';
</script>
<script src="../assets/js/api.js"></script>
<script src="../assets/js/admin_badges.js?v=badge-display-fix-v1"></script>
</body>
</html>
