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

    <!-- Filters -->
    <form method="GET" action="<?= $bp ?>/super-admin/branches" 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:160px;">
            <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, code, owner, email, phone" 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 Statuses</option>
                <option value="active" <?= $statusFilter === 'active' ? 'selected' : '' ?>>Active</option>
                <option value="pending" <?= $statusFilter === 'pending' ? 'selected' : '' ?>>Pending</option>
                <option value="inactive" <?= $statusFilter === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                <option value="suspended" <?= $statusFilter === 'suspended' ? 'selected' : '' ?>>Suspended</option>
            </select>
        </div>
        <div style="min-width:140px;">
            <label style="display:block;font-size:0.78rem;font-weight:600;color:#666;margin-bottom:4px;">City</label>
            <input type="text" name="city" value="<?= htmlspecialchars($cityFilter, ENT_QUOTES, 'UTF-8') ?>" placeholder="City" style="width:100%;padding:8px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.85rem;">
        </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/branches" 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/branches/create" class="admin-btn admin-btn-primary" style="background:#27ae60;text-decoration:none;padding:8px 18px;border-radius:6px;font-size:0.85rem;color:#fff;font-weight:600;">+ Add Branch</a>
    </form>

    <!-- Branches Table -->
    <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($branches)): ?>
            <div style="text-align:center;padding:60px 20px;color:#999;">
                <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#ccc" stroke-width="1.5" style="margin-bottom:12px;"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z"/><circle cx="12" cy="10" r="3"/></svg>
                <p style="font-size:16px;margin:0 0 6px;">No branches found</p>
                <p style="font-size:13px;margin:0;">Create your first branch to get started.</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;">Branch</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Code</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Location</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Owner</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Admin User</th>
                            <th style="padding:12px;text-align:center;font-weight:600;color:#495057;">Students</th>
                            <th style="padding:12px;text-align:right;font-weight:600;color:#495057;">Wallet</th>
                            <th style="padding:12px;text-align:center;font-weight:600;color:#495057;">Status</th>
                            <th style="padding:12px;text-align:right;font-weight:600;color:#495057;">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($branches as $b): ?>
                            <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;">
                                        <?php if (!empty($b['is_trusted'])): ?><span style="background:#f39c12;color:#fff;padding:1px 6px;border-radius:3px;font-size:0.66rem;margin-right:4px;">⭐ TRUSTED</span><?php endif; ?>
                                        <?= htmlspecialchars($b['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                                    </div>
                                    <div style="font-size:0.74rem;color:#999;margin-top:2px;"><?= htmlspecialchars($b['owner_phone'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                </td>
                                <td style="padding:12px;">
                                    <span style="background:#6c3fe8;color:#fff;padding:3px 8px;border-radius:4px;font-size:0.76rem;font-weight:600;font-family:monospace;"><?= htmlspecialchars($b['branch_code'] ?? '-', ENT_QUOTES, 'UTF-8') ?></span>
                                </td>
                                <td style="padding:12px;color:#666;">
                                    <div><?= htmlspecialchars($b['city'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                    <div style="font-size:0.74rem;color:#999;"><?= htmlspecialchars($b['state'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                </td>
                                <td style="padding:12px;color:#666;">
                                    <div><?= htmlspecialchars($b['owner_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></div>
                                    <div style="font-size:0.74rem;color:#999;"><?= htmlspecialchars($b['owner_email'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                </td>
                                <td style="padding:12px;color:#666;">
                                    <?php if (!empty($b['admin_email'])): ?>
                                        <div><?= htmlspecialchars($b['admin_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                        <div style="font-size:0.74rem;color:#999;"><?= htmlspecialchars($b['admin_email'], ENT_QUOTES, 'UTF-8') ?></div>
                                    <?php else: ?>
                                        <span style="color:#e84118;font-size:0.8rem;">Not assigned</span>
                                    <?php endif; ?>
                                </td>
                                <td style="padding:12px;text-align:center;font-weight:600;color:#27ae60;"><?= (int) ($b['student_count'] ?? 0) ?></td>
                                <td style="padding:12px;text-align:right;font-weight:600;color:#2c1e4a;">&#8377;<?= number_format((float) ($b['wallet_balance'] ?? 0), 0) ?></td>
                                <td style="padding:12px;text-align:center;">
                                    <?php
                                    $statusColors = ['active' => '#27ae60', 'pending' => '#f39c12', 'inactive' => '#95a5a6', 'suspended' => '#e84118'];
                                    $sc = $statusColors[$b['status']] ?? '#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($b['status'] ?? 'pending', ENT_QUOTES, 'UTF-8') ?></span>
                                    <?php if (!empty($b['is_verified'])): ?>
                                        <span style="display:inline-block;margin-left:4px;color:#27ae60;font-size:0.7rem;" title="Verified">&#10003;</span>
                                    <?php endif; ?>
                                </td>
                                <td style="padding:12px;text-align:right;">
                                    <div style="display:flex;gap:6px;justify-content:flex-end;flex-wrap:wrap;">
                                        <a href="<?= $bp ?>/super-admin/branches/<?= (int) $b['id'] ?>" class="admin-btn" style="background:#3498db;color:#fff;text-decoration:none;padding:5px 10px;border-radius:4px;font-size:0.74rem;">View</a>
                                        <a href="<?= $bp ?>/super-admin/branches/<?= (int) $b['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>
                                        <form method="POST" action="<?= $bp ?>/super-admin/branches/<?= (int) $b['id'] ?>/suspend" style="display:inline;" onsubmit="return confirm('Suspend this branch? Data preserved — can be restored.');">
                                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                            <button type="submit" style="background:#95a5a6;color:#fff;border:none;padding:5px 10px;border-radius:4px;font-size:0.74rem;cursor:pointer;">Suspend</button>
                                        </form>
                                        <form method="POST" action="<?= $bp ?>/super-admin/branches/<?= (int) $b['id'] ?>/hard-delete" style="display:inline;" onsubmit="return confirm('⚠️ PERMANENTLY DELETE this branch?\n\nThis will remove:\n• Branch record\n• All students & user accounts\n• All certificates & marksheets\n• All fee payments\n• All batches\n• All wallet transactions\n\nThis CANNOT be undone! Are you absolutely sure?');">
                                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                            <button type="submit" style="background:#e84118;color:#fff;border:none;padding:5px 10px;border-radius:4px;font-size:0.74rem;cursor:pointer;font-weight:600;">🗑 Delete</button>
                                        </form>
                                    </div>
                                </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($branches) ?> branch(es)
            </div>
        <?php endif; ?>
    </div>
</div>
