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

    <!-- Filters -->
    <form method="GET" action="<?= $bp ?>/super-admin/branch-applications" 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, city" 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="pending" <?= $statusFilter === 'pending' ? 'selected' : '' ?>>Pending</option>
                <option value="approved" <?= $statusFilter === 'approved' ? 'selected' : '' ?>>Approved</option>
                <option value="rejected" <?= $statusFilter === 'rejected' ? 'selected' : '' ?>>Rejected</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-applications" class="admin-btn" style="background:#eee;color:#666;text-decoration:none;padding:8px 18px;border-radius:6px;font-size:0.85rem;">Clear</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($applications)): ?>
            <div style="text-align:center;padding:60px 20px;color:#999;">
                <p style="font-size:16px;margin:0 0 6px;">No applications found</p>
                <p style="font-size:13px;margin:0;">Public applications will appear here when submitted via /branch/register.</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;">Applicant</th>
                            <th style="padding:12px;text-align:left;font-weight:600;color:#495057;">Contact</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;">Investment</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;">Applied On</th>
                            <th style="padding:12px;text-align:right;font-weight:600;color:#495057;">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($applications as $app): ?>
                            <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($app['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                </td>
                                <td style="padding:12px;color:#666;font-size:0.82rem;">
                                    <div><?= htmlspecialchars($app['email'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                    <div style="color:#999;"><?= htmlspecialchars($app['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                </td>
                                <td style="padding:12px;color:#666;"><?= htmlspecialchars($app['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($app['state'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                                <td style="padding:12px;color:#666;font-size:0.82rem;"><?= htmlspecialchars($app['investment_range'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                                <td style="padding:12px;text-align:center;">
                                    <?php
                                    $sc = ['pending' => '#f39c12', 'approved' => '#27ae60', 'rejected' => '#e84118'][$app['status'] ?? 'pending'] ?? '#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($app['status'] ?? 'pending', ENT_QUOTES, 'UTF-8') ?></span>
                                </td>
                                <td style="padding:12px;color:#666;font-size:0.8rem;"><?= date('M j, Y', strtotime($app['created_at'] ?? 'now')) ?></td>
                                <td style="padding:12px;text-align:right;">
                                    <a href="<?= $bp ?>/super-admin/branch-applications/<?= (int) $app['id'] ?>" class="admin-btn" style="background:<?= $app['status'] === 'pending' ? '#f39c12' : '#3498db' ?>;color:#fff;text-decoration:none;padding:5px 12px;border-radius:4px;font-size:0.74rem;font-weight:600;">
                                        <?= $app['status'] === 'pending' ? 'Review' : 'View' ?>
                                    </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($applications) ?> application(s)
            </div>
        <?php endif; ?>
    </div>
</div>
