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

<div class="standalone-wrap wide">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h2 class="mb-0">Companies</h2>
    <a href="<?= base_url('platform/companies/onboard') ?>" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i>Onboard a company</a>
  </div>

  <div class="standalone-card p-0">
    <table class="table table-hover mb-0 align-middle">
      <thead class="table-light">
        <tr>
          <th>Company</th>
          <th>Subdomain</th>
          <th>Status</th>
          <th>Expires</th>
          <th>Users</th>
          <th>Source</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($companies as $c): ?>
        <tr>
          <td><?= e($c['name']) ?></td>
          <td><code><?= e($c['slug']) ?>.<?= e($appDomain) ?></code></td>
          <td>
            <?php
              $status = $c['subscription_status'];
              $badge = match ($status) {
                  'active' => 'success',
                  'trialing' => 'info',
                  'past_due' => 'warning',
                  default => 'secondary',
              };
            ?>
            <span class="badge text-bg-<?= $badge ?>"><?= e($status) ?></span>
          </td>
          <td><?= e($c['subscription_expires_at'] ?? $c['trial_ends_at'] ?? '—') ?></td>
          <td><?= (int) $c['user_count'] ?></td>
          <td class="small text-muted"><?= e($c['activation_source'] ?? '—') ?></td>
          <td>
            <?php if ($c['subscription_status'] !== 'canceled'): ?>
            <form method="POST" action="<?= base_url('platform/companies/' . $c['id'] . '/suspend') ?>" onsubmit="return confirm('Suspend this company\'s access?');">
              <?= csrf_field() ?>
              <button type="submit" class="btn btn-sm btn-outline-danger">Suspend</button>
            </form>
            <?php endif; ?>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if (!$companies): ?>
        <tr><td colspan="7" class="text-center text-muted py-4">No companies yet.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

</body>
</html>
