<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-shield-haltered me-2 text-danger"></i>SuperAdmin Dashboard</h5>
    <a href="<?= url('/Logout') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-sign-out-alt me-1"></i>Sign Out</a>
</div>

<?php if (!empty($impersonatedId)): ?>
<div class="alert alert-warning d-flex justify-content-between align-items-center py-2 small mb-3">
    <span><i class="fas fa-eye me-1"></i> Viewing as: <strong><?= e($impersonatedName) ?></strong></span>
    <a href="<?= url('/SuperAdmin/endImpersonation') ?>" class="btn btn-sm btn-outline-dark">Return to SuperAdmin</a>
</div>
<?php endif; ?>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>
<?php if (!empty($error)): ?><div class="alert alert-danger py-2 small"><?= e($error) ?></div><?php endif; ?>

<!-- Tenants -->
<div class="rs-card p-3 mb-3">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h6 class="fw-bold mb-0"><i class="fas fa-building me-2 text-primary"></i>Tenants</h6>
        <a href="<?= url('/SuperAdmin/createTenant') ?>" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i>Add Tenant</a>
    </div>
    <table class="table table-sm table-hover mb-0">
        <thead><tr><th>Name</th><th>Subdomain</th><th>Status</th><th>Created</th><th class="text-end">Actions</th></tr></thead>
        <tbody>
            <?php foreach ($tenants as $t): ?>
                <tr>
                    <td class="fw-semibold"><?= e($t['name']) ?></td>
                    <td class="small text-muted"><?= e($t['subdomain'] ?? '—') ?></td>
                    <td><span class="badge bg-<?= $t['status'] === 'active' ? 'success' : ($t['status'] === 'suspended' ? 'warning' : 'secondary') ?>"><?= e($t['status']) ?></span></td>
                    <td class="small text-muted"><?= e($t['created_at'] ?? '') ?></td>
                    <td class="text-end">
                        <a href="<?= url('/SuperAdmin/impersonate/' . (int) $t['tenant_id']) ?>" class="btn btn-sm btn-outline-warning" title="Login as this tenant"><i class="fas fa-sign-in-alt"></i></a>
                        <a href="<?= url('/SuperAdmin/editTenant/' . (int) $t['tenant_id']) ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-edit"></i></a>
                        <a href="<?= url('/SuperAdmin/toggleStatus/' . (int) $t['tenant_id']) ?>" class="btn btn-sm btn-outline-<?= $t['status'] === 'active' ? 'warning' : 'success' ?>" onclick="return confirm('Toggle status?')"><i class="fas fa-<?= $t['status'] === 'active' ? 'pause' : 'play' ?>"></i></a>
                        <a href="<?= url('/SuperAdmin/cloneToTenant/' . (int) $t['tenant_id']) ?>" class="btn btn-sm btn-outline-info" title="Clone default checklists" onclick="return confirm('Clone all default checklists into this tenant?')"><i class="fas fa-clone"></i></a>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($tenants)): ?>
                <tr><td colspan="5" class="text-muted text-center py-3">No tenants yet.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<!-- Default Checklists -->
<div class="rs-card p-3">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h6 class="fw-bold mb-0"><i class="fas fa-clipboard-list me-2 text-success"></i>Default Checklists</h6>
        <a href="<?= url('/SuperAdmin/defaultChecklists') ?>" class="btn btn-sm btn-outline-success"><i class="fas fa-cog me-1"></i>Manage</a>
    </div>
    <?php if (empty($defaultChecklists)): ?>
        <p class="text-muted small mb-0">No default checklists yet. <a href="<?= url('/SuperAdmin/defaultChecklists') ?>">Create one</a>.</p>
    <?php else: ?>
        <table class="table table-sm mb-0"><thead><tr><th>Name</th><th>Interval</th><th>Active</th></tr></thead><tbody>
            <?php foreach ($defaultChecklists as $c): ?>
                <tr><td><?= e($c['name']) ?></td><td class="small"><?= (int) $c['interval_days'] ?> days</td><td><?= $c['is_active'] ? '<span class="badge bg-success">Yes</span>' : '<span class="badge bg-secondary">No</span>' ?></td></tr>
            <?php endforeach; ?>
        </tbody></table>
    <?php endif; ?>
</div>