<%- include('_head') %>
  <h1 style="font-size:24px; margin-bottom:18px;">Discount codes</h1>

  <div class="panel">
    <h3>Create code</h3>
    <form method="post" action="/admin/coupons" style="display:flex; gap:10px; flex-wrap:wrap; align-items:end;">
      <div class="form-field"><label for="code">Code</label><input id="code" name="code" placeholder="e.g. FRESH20" required></div>
      <div class="form-field"><label for="type">Type</label>
        <select id="type" name="type" style="width:auto;"><option value="percent">% off</option><option value="fixed">Fixed R</option></select>
      </div>
      <div class="form-field"><label for="value">Value</label><input id="value" name="value" type="number" step="0.01" required style="width:110px;"></div>
      <div class="form-field"><label for="min_subtotal">Min subtotal</label><input id="min_subtotal" name="min_subtotal" type="number" step="0.01" value="0" style="width:110px;"></div>
      <div class="form-field"><label for="usage_limit">Usage limit (0 = unlimited)</label><input id="usage_limit" name="usage_limit" type="number" value="0" style="width:110px;"></div>
      <div class="form-field"><label for="expires_at">Expires</label><input id="expires_at" name="expires_at" type="date" style="width:150px;"></div>
      <button class="btn btn-gold btn-sm" type="submit"><i class="ic" data-ic="plus"></i> Create</button>
    </form>
  </div>

  <div class="panel">
    <div style="overflow-x:auto;">
      <table class="table">
        <thead><tr><th>Code</th><th>Type</th><th>Value</th><th>Min subtotal</th><th>Used</th><th>Limit</th><th>Expires</th><th>Active</th><th></th></tr></thead>
        <tbody>
          <% if (coupons.length) { coupons.forEach(function(c){ %>
            <tr>
              <td><b><%= c.code %></b></td>
              <td><%= c.type %></td>
              <td><%= c.type === 'percent' ? c.value + '%' : money(c.value) %></td>
              <td><%= money(c.min_subtotal) %></td>
              <td><%= c.used_count %></td>
              <td><%= c.usage_limit === 0 ? 'Unlimited' : c.usage_limit %></td>
              <td><%= c.expires_at ? new Date(c.expires_at).toLocaleDateString() : 'Never' %></td>
              <td><span class="status-tag <%= c.active ? 'st-paid' : 'st-draft' %>"><%= c.active ? 'active' : 'off' %></span></td>
              <td>
                <div class="admin-actions">
                  <form method="post" action="/admin/coupons/<%= c.id %>/toggle"><button class="btn btn-dark btn-sm" type="submit"><%= c.active ? 'Deactivate' : 'Activate' %></button></form>
                  <form method="post" action="/admin/coupons/<%= c.id %>/delete" onsubmit="return confirm('Delete this code?');"><button class="btn btn-dark btn-sm" type="submit"><i class="ic" data-ic="trash"></i></button></form>
                </div>
              </td>
            </tr>
          <% }); } else { %>
            <tr><td colspan="9" style="color:var(--muted); text-align:center; padding:30px;">No discount codes yet.</td></tr>
          <% } %>
        </tbody>
      </table>
    </div>
  </div>
<%- include('_foot') %>
