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

if (!$user || !\App\Core\Auth::hasPermission('rewards.manage', $user)) {
    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>Coupons | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_coupons.css?v=1">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container enterprise-page coupons-admin-container">
  <section class="page-hero admin-hero enterprise-hero">
    <div>
      <p class="eyebrow">Discount system</p>
      <h1>🎟️ Coupons</h1>
      <p class="muted">Create coupon codes for paid walks. Use percentage or fixed value discounts with validity dates and usage limits.</p>
    </div>
    <div class="hero-actions"><button class="btn" type="button" id="newCouponBtn">+ New coupon</button></div>
  </section>

  <section class="grid four compact-stats">
    <div class="stat stat-pro"><span>Total coupons</span><strong id="couponTotal">0</strong></div>
    <div class="stat stat-pro"><span>Active</span><strong id="couponActive">0</strong></div>
    <div class="stat stat-pro"><span>Used</span><strong id="couponUsed">0</strong></div>
    <div class="stat stat-pro"><span>Discount given</span><strong id="couponDiscount">0</strong></div>
  </section>

  <section class="grid coupons-grid">
    <article class="panel enterprise-table-panel">
      <div class="section-head">
        <div>
          <p class="eyebrow">Coupon library</p>
          <h2>Manage coupons</h2>
          <p class="muted small">Coupons apply to paid walks during member registration.</p>
        </div>
        <div class="inline-controls search-controls">
          <input id="couponSearch" placeholder="Search coupon code">
          <select id="couponStatusFilter"><option value="">All status</option><option value="active">Active</option><option value="inactive">Inactive</option></select>
        </div>
      </div>
      <div id="couponGrid" class="coupon-card-grid"></div>
    </article>

    <aside class="panel enterprise-form-panel sticky-panel">
      <div class="section-head"><div><p class="eyebrow">Create / edit</p><h2 id="couponFormTitle">Create coupon</h2></div></div>
      <form id="couponForm" class="stack-form">
        <input type="hidden" name="id" id="couponId">
        <label>Coupon code<input name="code" id="couponCode" required placeholder="SUMMER25"></label>
        <label>Description<textarea name="description" id="couponDescription" placeholder="Internal note or customer-facing reason"></textarea></label>
        <div class="form-grid two">
          <label>Discount type<select name="discount_type" id="couponDiscountType"><option value="percent">Percentage</option><option value="fixed">Fixed value</option></select></label>
          <label>Discount value<input name="discount_value" id="couponDiscountValue" type="number" min="0" step="0.01" required placeholder="25"></label>
        </div>
        <div class="form-grid two">
          <label>Currency<select name="currency" id="couponCurrency"><option value="LBP">LBP</option><option value="USD">USD</option><option value="EUR">EUR</option></select></label>
          <label>Status<select name="status" id="couponStatus"><option value="active">Active</option><option value="inactive">Inactive</option></select></label>
        </div>
        <div class="form-grid two">
          <label>Valid from<input name="valid_from" id="couponValidFrom" type="datetime-local"></label>
          <label>Valid until<input name="valid_until" id="couponValidUntil" type="datetime-local"></label>
        </div>
        <div class="form-grid three">
          <label>Max uses<input name="max_uses" id="couponMaxUses" type="number" min="0" placeholder="Blank = unlimited"></label>
          <label>Per user<input name="max_uses_per_user" id="couponMaxPerUser" type="number" min="1" value="1"></label>
          <label>Minimum walk amount<input name="min_walk_amount" id="couponMinAmount" type="number" min="0" step="0.01" value="0"></label>
        </div>
        <div class="coupon-preview" id="couponPreview">Preview: percentage discount</div>
        <div class="inline-controls"><button class="btn" type="submit">Save coupon</button><button class="btn btn-light" type="button" id="resetCouponForm">Clear</button></div>
        <p id="couponMsg" class="notice"></p>
      </form>
    </aside>
  </section>

  <section class="panel">
    <div class="section-head"><div><p class="eyebrow">Usage ledger</p><h2>Coupon redemptions</h2></div></div>
    <div class="table-wrap"><table><thead><tr><th>Date</th><th>Coupon</th><th>Member</th><th>Walk</th><th>Original</th><th>Discount</th><th>Final</th></tr></thead><tbody id="couponRedemptionsBody"></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_coupons.js?v=1"></script>
</body>
</html>
