<?php $pageTitle = 'Subscription codes'; require __DIR__ . '/../partials/standalone-head.php'; ?>
<?php require __DIR__ . '/_nav.php'; ?>

<div class="standalone-wrap wide">
  <div class="row g-4">
    <div class="col-lg-5">
      <div class="standalone-card">
        <h2 class="mb-3">Generate a code</h2>

        <?php $errors = \App\Core\Session::getFlash('_errors', []); ?>
        <?php if ($errors): ?>
          <div class="alert alert-danger">
            <ul class="mb-0">
              <?php foreach ($errors as $field => $messages): foreach ((array) $messages as $m): ?>
                <li><?= e($m) ?></li>
              <?php endforeach; endforeach; ?>
            </ul>
          </div>
        <?php endif; ?>

        <form method="POST" action="<?= base_url('platform/codes') ?>">
          <?= csrf_field() ?>

          <div class="mb-3">
            <label class="form-label">Assign to a specific company (optional)</label>
            <select name="business_line_id" class="form-select">
              <option value="">— Any new signup can redeem it —</option>
              <?php foreach ($companies as $c): ?>
                <option value="<?= (int) $c['id'] ?>"><?= e($c['name']) ?> (renewal only)</option>
              <?php endforeach; ?>
            </select>
          </div>

          <div class="mb-3">
            <label class="form-label">Intent</label>
            <select name="intent" class="form-select">
              <option value="either">Either — new signup or renewal</option>
              <option value="activate_new">New signup only</option>
              <option value="renew_existing">Renewal only</option>
            </select>
          </div>

          <div class="row g-2 mb-3">
            <div class="col-6">
              <label class="form-label">Duration (months)</label>
              <input type="number" name="duration_months" class="form-control" value="12" min="1" max="60" required>
            </div>
            <div class="col-6">
              <label class="form-label">Max uses</label>
              <input type="number" name="max_uses" class="form-control" value="1" min="1" max="1000" required>
            </div>
          </div>

          <div class="mb-4">
            <label class="form-label">Code expires on (optional)</label>
            <input type="date" name="expires_at" class="form-control">
          </div>

          <button type="submit" class="btn btn-primary w-100">Generate code</button>
        </form>
      </div>
    </div>

    <div class="col-lg-7">
      <div class="standalone-card p-0">
        <table class="table table-sm mb-0 align-middle">
          <thead class="table-light">
            <tr><th>Code</th><th>Intent</th><th>Company</th><th>Uses</th><th>Expires</th><th></th></tr>
          </thead>
          <tbody>
            <?php foreach ($codes as $c): ?>
            <tr>
              <td><code><?= e($c['code_prefix']) ?>…</code></td>
              <td class="small"><?= e($c['intent']) ?></td>
              <td class="small"><?= e($c['business_line_name'] ?? 'any') ?></td>
              <td class="small"><?= (int) $c['use_count'] ?>/<?= (int) $c['max_uses'] ?></td>
              <td class="small"><?= e($c['expires_at'] ?? '—') ?></td>
              <td>
                <?php if ($c['revoked_at'] === null): ?>
                <form method="POST" action="<?= base_url('platform/codes/' . $c['id'] . '/revoke') ?>" onsubmit="return confirm('Revoke this code?');">
                  <?= csrf_field() ?>
                  <button type="submit" class="btn btn-sm btn-outline-danger">Revoke</button>
                </form>
                <?php else: ?>
                  <span class="badge text-bg-secondary">revoked</span>
                <?php endif; ?>
              </td>
            </tr>
            <?php endforeach; ?>
            <?php if (!$codes): ?>
              <tr><td colspan="6" class="text-center text-muted py-4">No codes yet.</td></tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

</body>
</html>
