<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Super Admin Dashboard</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
  <nav class="navbar navbar-dark bg-dark px-3">
    <span class="navbar-brand">🏥 Munshiji Clinics</span>
    <div class="ms-auto text-white d-flex align-items-center gap-3">
      <span class="small"><?= htmlspecialchars($saas_name) ?></span>
      <a class="btn btn-sm btn-outline-light" href="<?= base_url('saas/logout') ?>">Logout</a>
    </div>
  </nav>

  <div class="container py-4">
    <h5 class="mb-3">Clients</h5>

    <?php if (empty($clinics)): ?>
      <div class="alert alert-warning">
        Master DB mein clinics ki koi entry nahi. `sql/003_saas_master.sql` import karo.
      </div>
    <?php endif; ?>

    <div class="row g-3">
      <?php foreach ($clinics as $c): ?>
        <?php
          $ok    = (strtolower($c->status) === 'active');
          $stats = isset($c->stats) ? $c->stats : array('status' => 'ok', 'counts' => array());
          $badge = $ok ? 'bg-success' : 'bg-warning text-dark';
        ?>
        <div class="col-md-6">
          <div class="card h-100 shadow-sm">
            <div class="card-body">
              <div class="d-flex justify-content-between align-items-start">
                <h5 class="mb-1"><?= htmlspecialchars($c->name) ?></h5>
                <span class="badge <?= $badge ?>"><?= htmlspecialchars($c->status) ?></span>
              </div>
              <div class="text-muted small mb-2">
                <code>/<?= htmlspecialchars($c->slug) ?></code> ·
                db: <code><?= htmlspecialchars($c->db_name) ?></code>
              </div>

              <?php if ($stats['status'] === 'ok'): ?>
                <div class="row text-center mt-2">
                  <?php foreach ($stats['counts'] as $label => $n): ?>
                    <div class="col">
                      <div class="fw-bold"><?= number_format((int)$n) ?></div>
                      <div class="small text-muted"><?= $label ?></div>
                    </div>
                  <?php endforeach; ?>
                </div>
              <?php else: ?>
                <div class="small text-warning">⚠ Database provisioned nahi (ya connect nahi hua)</div>
              <?php endif; ?>

              <div class="mt-3">
                <a class="btn btn-sm btn-outline-primary" href="<?= site_url($c->slug . '/admin') ?>">Open Clinic →</a>
              </div>
            </div>
          </div>
        </div>
      <?php endforeach; ?>
    </div>
  </div>
</body>
</html>