<?php
require __DIR__ . '/../_boot.php';
require __DIR__ . '/../partials/nav.php';

if (!$user || !(\App\Core\Auth::hasPermission('tiers.manage', $user) || \App\Core\Auth::hasPermission('loyalty.manage', $user) || in_array($user['role_name'] ?? '', ['super_admin','event_manager'], true))) {
    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>Loyalty Tiers & Sponsors | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_tiers_sponsors.css?v=1">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container enterprise-page tiers-admin-page">
  <section class="page-hero admin-hero enterprise-hero tier-hero">
    <div>
      <p class="eyebrow">Enterprise loyalty engine</p>
      <h1>🏆 Tiers, Sponsors & Prize Access</h1>
      <p class="muted">Automatically assign members to levels based on points and badges, set expiry dates, and restrict sponsored prizes by tier.</p>
    </div>
    <div class="hero-actions">
      <button class="btn btn-light" type="button" id="expireTiersBtn">Expire old levels</button>
      <button class="btn" type="button" id="recalcAllTiersBtn">Recalculate all members</button>
    </div>
  </section>

  <section class="grid four compact-stats tier-stats">
    <div class="stat stat-pro"><span>Active tiers</span><strong id="tierCount">0</strong></div>
    <div class="stat stat-pro"><span>Sponsors</span><strong id="sponsorCount">0</strong></div>
    <div class="stat stat-pro"><span>Members tiered</span><strong id="tieredMemberCount">0</strong></div>
    <div class="stat stat-pro"><span>Expiring 30 days</span><strong id="tierExpiringCount">0</strong></div>
  </section>

  <section class="grid tiers-main-grid">
    <article class="panel tier-list-panel">
      <div class="section-head"><div><p class="eyebrow">Level rules</p><h2>Tiers</h2><p class="muted small">The highest qualifying active tier is assigned automatically.</p></div><button class="btn btn-light btn-small" type="button" id="newTierBtn">New tier</button></div>
      <div id="tierCards" class="tier-card-grid"></div>
    </article>

    <aside class="panel tier-form-panel sticky-panel">
      <div class="section-head"><div><p class="eyebrow">Tier setup</p><h2 id="tierFormTitle">Create tier</h2></div></div>
      <form id="tierForm" class="stack-form">
        <input type="hidden" id="tierId" name="id">
        <div class="form-grid two">
          <label>Tier name<input id="tierName" name="tier_name" required placeholder="Gold"></label>
          <label>Code<input id="tierCode" name="tier_code" placeholder="gold"></label>
        </div>
        <div class="form-grid three">
          <label>Icon<input id="tierIcon" name="icon" placeholder="🥇"></label>
          <label>Color<input id="tierColor" name="color" type="color" value="#1c7c54"></label>
          <label>Status<select id="tierStatus" name="status"><option value="active">Active</option><option value="inactive">Inactive</option></select></label>
        </div>
        <div class="form-grid two">
          <label>Minimum points<input id="tierMinPoints" name="min_points" type="number" min="0" value="0"></label>
          <label>Minimum badge count<input id="tierRequiredBadgeCount" name="required_badge_count" type="number" min="0" value="0"></label>
        </div>
        <div class="form-grid two">
          <label>Level validity days<input id="tierValidDays" name="level_valid_days" type="number" min="0" value="365"><small class="muted">0 = no expiry date.</small></label>
          <label>Sort order<input id="tierSort" name="sort_order" type="number" value="10"><small class="muted">Higher level should have higher order.</small></label>
        </div>
        <label>Required specific badges<select id="tierRequiredBadges" name="required_badge_ids[]" multiple size="5"></select><small class="muted">Optional. Member must have all selected badges.</small></label>
        <label>Description<textarea id="tierDescription" name="description" placeholder="Describe benefits and eligibility"></textarea></label>
        <div class="inline-controls"><button class="btn" type="submit">Save tier</button><button class="btn btn-light" type="button" id="resetTierForm">Clear</button></div>
        <p id="tierMsg" class="notice"></p>
      </form>
    </aside>
  </section>

  <section class="grid tiers-main-grid sponsors-grid">
    <article class="panel">
      <div class="section-head"><div><p class="eyebrow">External partners</p><h2>Sponsors</h2><p class="muted small">Use sponsors on prizes, vouchers, giveaways, and campaigns.</p></div><button class="btn btn-light btn-small" type="button" id="newSponsorBtn">New sponsor</button></div>
      <div id="sponsorCards" class="sponsor-card-grid"></div>
    </article>
    <aside class="panel sticky-panel">
      <div class="section-head"><div><p class="eyebrow">Sponsor profile</p><h2 id="sponsorFormTitle">Create sponsor</h2></div></div>
      <form id="sponsorForm" class="stack-form">
        <input type="hidden" id="sponsorId" name="id">
        <label>Sponsor name<input id="sponsorName" name="sponsor_name" required placeholder="Partner Coffee House"></label>
        <div class="form-grid two"><label>Contact name<input id="sponsorContact" name="contact_name"></label><label>Status<select id="sponsorStatus" name="status"><option value="active">Active</option><option value="inactive">Inactive</option></select></label></div>
        <div class="form-grid two"><label>Email<input id="sponsorEmail" name="email" type="email"></label><label>Phone<input id="sponsorPhone" name="phone"></label></div>
        <label>Website<input id="sponsorWebsite" name="website" placeholder="https://"></label>
        <label>Logo path<input id="sponsorLogo" name="logo_path" placeholder="uploads/sponsors/logo.png"></label>
        <label>Notes<textarea id="sponsorNotes" name="notes" placeholder="Contract notes, prize commitment, contact terms..."></textarea></label>
        <div class="inline-controls"><button class="btn" type="submit">Save sponsor</button><button class="btn btn-light" type="button" id="resetSponsorForm">Clear</button></div>
        <p id="sponsorMsg" class="notice"></p>
      </form>
    </aside>
  </section>

  <section class="panel">
    <div class="section-head"><div><p class="eyebrow">Prize eligibility</p><h2>Restrict prizes by tier</h2><p class="muted small">Leave tiers empty to make a prize open to all tiers.</p></div></div>
    <div class="table-wrap"><table class="pro-table tier-prize-table"><thead><tr><th>Prize</th><th>Sponsor</th><th>Open to tiers</th><th>Availability</th><th>Action</th></tr></thead><tbody id="tierPrizeBody"></tbody></table></div>
  </section>

  <section class="panel">
    <div class="section-head"><div><p class="eyebrow">Member levels</p><h2>Current tier assignments</h2><p class="muted small">Shows current level, points, badges and expiry date.</p></div><input id="memberTierSearch" placeholder="Search member"></div>
    <div class="table-wrap"><table class="pro-table member-tier-table"><thead><tr><th>Member</th><th>Tier</th><th>Points</th><th>Badges</th><th>Assigned</th><th>Expires</th><th>Action</th></tr></thead><tbody id="memberTierBody"></tbody></table></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_tiers_sponsors.js?v=1"></script>
</body>
</html>
