<?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>Prizes & Claims | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_prize_claim_print.css?v=tiers-sponsors-v1">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container enterprise-page prizes-admin-container rewards-suite-page">
  <section class="page-hero admin-hero enterprise-hero rewards-suite-hero">
    <div>
      <p class="eyebrow">Rewards marketplace</p>
      <h1>🎁 Prizes & Claims</h1>
      <p class="muted">Create prizes that members can claim with loyalty points, manage stock, and fulfil claims.</p>
    </div>
    <div class="hero-actions">
      <a class="btn btn-light" href="loyalty.php">Loyalty points</a>
      <a class="btn btn-light" href="tiers.php">Tiers & sponsors</a>
      <a class="btn btn-light" href="badges.php">Badges</a>
      <?php if ($canManage): ?><button class="btn" type="button" id="newPrizeBtn">+ New prize</button><?php endif; ?>
    </div>
  </section>

  <section class="grid four compact-stats">
    <div class="stat stat-pro"><span>Total prizes</span><strong id="rewardTotal">0</strong></div>
    <div class="stat stat-pro"><span>Active prizes</span><strong id="rewardActive">0</strong></div>
    <div class="stat stat-pro"><span>Open claims</span><strong id="claimOpen">0</strong></div>
    <div class="stat stat-pro"><span>Fulfilled</span><strong id="claimFulfilled">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">Prize catalog</p>
          <h2>Claimable prizes</h2>
          <p class="muted small">Members redeem these from the client dashboard using loyalty points.</p>
        </div>
        <div class="inline-controls search-controls">
          <input id="rewardSearch" placeholder="Search prizes">
          <select id="rewardStatusFilter"><option value="">All status</option><option value="active">Active</option><option value="inactive">Inactive</option></select>
        </div>
      </div>
      <div id="rewardGrid" class="reward-management-grid pro-reward-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="rewardFormTitle">Create prize</h2></div></div>
      <form id="rewardForm" class="stack-form">
        <input type="hidden" name="id" id="rewardId">
        <label>Prize name<input name="reward_name" id="rewardName" required placeholder="Free coffee voucher"></label>
        <div class="form-grid two">
          <label>Type<select name="reward_type" id="rewardType"><option value="gift">Gift</option><option value="manual">Manual</option><option value="badge">Badge</option><option value="points">Points</option></select></label>
          <label>Status<select name="status" id="rewardStatus"><option value="active">Active</option><option value="inactive">Inactive</option></select></label>
        </div>
        <div class="form-grid two">
          <label>Points cost<input name="points_cost" id="rewardPointsCost" type="number" min="0" required placeholder="Example: 500"></label>
          <label>Stock quantity<input name="stock_quantity" id="rewardStock" type="number" min="0" placeholder="Leave empty for unlimited"></label>
        </div>
        <div class="form-grid two">
          <label>External sponsor<select name="sponsor_id" id="rewardSponsor"><option value="">No sponsor</option></select></label>
          <label>Open to tiers<select name="tier_ids[]" id="rewardTiers" multiple size="4"></select><small class="muted">Leave empty = all tiers can claim.</small></label>
        </div>
        <div class="form-grid two">
          <label>Available from<input name="available_from" id="rewardAvailableFrom" type="date"></label>
          <label>Available until<input name="available_until" id="rewardAvailableUntil" type="date"></label>
        </div>
        <label>Description<textarea name="description" id="rewardDescription" placeholder="Describe what the member can claim"></textarea></label>
        <label>Fulfilment instructions<textarea name="redemption_instructions" id="rewardInstructions" placeholder="Internal instructions: where to collect, voucher code, sponsor contact, etc."></textarea></label>
        <div class="inline-controls"><button class="btn" type="submit">Save prize</button><button class="btn btn-light" type="button" id="resetRewardForm">Clear</button></div>
        <p id="rewardMsg" class="notice"></p>
      </form>
      <?php else: ?>
      <p class="muted">Walk leaders can view prizes and claims. Super Admin and Event Manager can create or edit prizes.</p>
      <?php endif; ?>
    </aside>
  </section>

  <section class="panel enterprise-table-panel crm-wide-panel">
    <div class="section-head crm-section-head">
      <div><p class="eyebrow">Fulfilment desk</p><h2>Prize claims</h2><p class="muted small">Approve, fulfil, reject, or refund member prize claims.</p></div>
      <div class="inline-controls search-controls">
        <input id="claimSearch" placeholder="Search member or prize">
        <select id="claimStatusFilter"><option value="">All claims</option><option value="requested">Requested</option><option value="approved">Approved</option><option value="fulfilled">Fulfilled</option><option value="rejected">Rejected</option><option value="cancelled">Cancelled</option></select>
      </div>
    </div>
    <div class="table-wrap pro-table-wrap rewards-claims-wrap">
      <table class="pro-table reward-claims-table">
        <thead><tr><th>Member</th><th>Prize</th><th>Status</th><th>Points</th><th>Requested</th><th>Actions</th></tr></thead>
        <tbody id="rewardClaimsBody"></tbody>
      </table>
    </div>
  </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_prizes.js?v=tiers-sponsors-v1"></script>
</body>
</html>
