<?php
/**
 * Super Admin — Branch Admins List View
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$admins = $admins ?? [];
$search = $search ?? '';
$statusFilter = $statusFilter ?? '';
?>
<div class="admin-section">

    <!-- Filters -->
    <form method="GET" action="<?= $bp ?>/super-admin/branch-admins" style="background:#fff;padding:16px;border-radius:10px;margin-bottom:20px;display:flex;gap:12px;flex-wrap:wrap;align-items:end;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <div style="flex:1;min-width:200px;">
            <label style="display:block;font-size:0.78rem;font-weight:600;color:#666;margin-bottom:4px;">Search</label>
            <input type="text" name="search" value="<?= htmlspecialchars($search, ENT_QUOTES, 'UTF-8') ?>" placeholder="Name, email, phone, branch" style="width:100%;padding:8px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.85rem;">
        </div>
        <div style="min-width:140px;">
            <label style="display:block;font-size:0.78rem;font-weight:600;color:#666;margin-bottom:4px;">Status</label>
            <select name="status" style="width:100%;padding:8px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.85rem;">
                <option value="">All</option>
                <option value="active" <?= $statusFilter === 'active' ? 'selected' : '' ?>>Active</option>
                <option value="inactive" <?= $statusFilter === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                <option value="suspended" <?= $statusFilter === 'suspended' ? 'selected' : '' ?>>Suspended</option>
            </select>
        </div>
        <button type="submit" class="admin-btn admin-btn-primary" style="padding:8px 18px;border:none;border-radius:6px;font-size:0.85rem;cursor:pointer;">Filter</button>
        <a href="<?= $bp ?>/super-admin/branch-admins" class="admin-btn" style="background:#eee;color:#666;text-decoration:none;padding:8px 18px;border-radius:6px;font-size:0.85rem;">Clear</a>
        <a href="<?= $bp ?>/super-admin/branch-admins/create" class="admin-btn" style="background:#27ae60;color:#fff;text-decoration:none;padding:8px 18px;border-radius:6px;font-size:0.85rem;font-weight:600;">+ Add Branch Admin</a>
    </form>

    <div class="admin-card" style="background:#fff;border-radius:10px;padding:0;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <?php if (empty($admins)): ?>
            <div style="text-align:center;padding:60px 20px;color:#999;">
                <p style="font-size:16px;margin:0 0 6px;">No branch admins found</p>
                <p style="font-size:13px;margin:0;">Create one to assign a login to a branch.</p>
            </div>
        <?php else: ?>
            <div style="overflow-x:auto;">
                <table class="admin-table" style="width:100%;border-collapse:collapse;font-size:0.86rem;">
                    <thead>
                        <tr style="background:#f8f9fa;border-bottom:2px solid #dee2e6;">
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Admin Name</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Branch</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Phone</th>
                            <th style="padding:12px;text-align:center;font-weight:600;color:#495057;">Students</th>
                            <th style="padding:12px;text-align:center;font-weight:600;color:#495057;">Status</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Created</th>
                            <th style="padding:12px;text-align:right;font-weight:600;color:#495057;">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($admins as $a): ?>
                            <tr style="border-bottom:1px solid #f0f0f0;" onmouseover="this.style.background='#fafafa'" onmouseout="this.style.background=''">
                                <td style="padding:12px;">
                                    <div style="font-weight:600;color:#2c1e4a;"><?= htmlspecialchars($a['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                    <div style="font-size:0.76rem;color:#999;margin-top:2px;"><?= htmlspecialchars($a['email'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                </td>
                                <td style="padding:12px;">
                                    <?php if (!empty($a['branch_code'])): ?>
                                        <code style="background:#6c3fe8;color:#fff;padding:2px 6px;border-radius:3px;font-size:0.74rem;"><?= htmlspecialchars($a['branch_code'], ENT_QUOTES, 'UTF-8') ?></code>
                                        <div style="font-size:0.76rem;color:#999;margin-top:2px;"><?= htmlspecialchars($a['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                    <?php else: ?>
                                        <span style="color:#e84118;font-size:0.8rem;">No branch</span>
                                    <?php endif; ?>
                                </td>
                                <td style="padding:12px;color:#666;"><?= htmlspecialchars($a['phone'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                                <td style="padding:12px;text-align:center;font-weight:600;color:#27ae60;"><?= (int) ($a['student_count'] ?? 0) ?></td>
                                <td style="padding:12px;text-align:center;">
                                    <?php
                                    $sc = ['active' => '#27ae60', 'inactive' => '#95a5a6', 'suspended' => '#e84118'][$a['status'] ?? 'inactive'] ?? '#999';
                                    ?>
                                    <span style="display:inline-block;padding:3px 10px;border-radius:20px;font-size:0.72rem;font-weight:600;color:#fff;background:<?= $sc ?>;text-transform:uppercase;"><?= htmlspecialchars($a['status'] ?? 'inactive', ENT_QUOTES, 'UTF-8') ?></span>
                                </td>
                                <td style="padding:12px;color:#666;font-size:0.8rem;"><?= date('M j, Y', strtotime($a['created_at'] ?? 'now')) ?></td>
                                <td style="padding:12px;text-align:right;">
                                    <a href="<?= $bp ?>/super-admin/branch-admins/<?= (int) $a['id'] ?>/edit" class="admin-btn" style="background:#f39c12;color:#fff;text-decoration:none;padding:5px 10px;border-radius:4px;font-size:0.74rem;">Edit</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <div style="padding:12px;background:#f8f9fa;font-size:0.82rem;color:#666;text-align:center;border-top:1px solid #f0f0f0;">
                Showing <?= count($admins) ?> branch admin(s)
            </div>
        <?php endif; ?>
    </div>
</div>
