<?php
/**
 * Branch Applications List — Gmind Institute ERP
 * Lists all branch applications with status and action buttons
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';

$statusColors = [
    'pending'  => ['bg' => '#fff3e0', 'text' => '#e65100'],
    'approved' => ['bg' => '#e8f5e9', 'text' => '#2e7d32'],
    'rejected' => ['bg' => '#ffebee', 'text' => '#c62828'],
];
?>
<div class="admin-page-header" style="display:flex;justify-content:space-between;align-items:center;margin-bottom:24px;">
    <h1 style="margin:0;font-size:22px;font-weight:700;">Branch Applications</h1>
</div>

<div class="admin-table-wrapper">
    <table class="admin-table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>City</th>
                <th>State</th>
                <th>Plan</th>
                <th>Status</th>
                <th>Applied On</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (!empty($applications)): ?>
                <?php foreach ($applications as $app): ?>
                    <?php
                    $status = $app['status'] ?? 'pending';
                    $sc = $statusColors[$status] ?? $statusColors['pending'];
                    ?>
                <tr>
                    <td><?= htmlspecialchars($app['name'], ENT_QUOTES, 'UTF-8') ?></td>
                    <td><?= htmlspecialchars($app['email'], ENT_QUOTES, 'UTF-8') ?></td>
                    <td><?= htmlspecialchars($app['phone'], ENT_QUOTES, 'UTF-8') ?></td>
                    <td><?= htmlspecialchars($app['city'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                    <td><?= htmlspecialchars($app['state'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                    <td><?= htmlspecialchars($app['plan_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:12px;font-size:12px;font-weight:600;background:<?= $sc['bg'] ?>;color:<?= $sc['text'] ?>;">
                            <?= ucfirst(htmlspecialchars($status, ENT_QUOTES, 'UTF-8')) ?>
                        </span>
                    </td>
                    <td><?= htmlspecialchars($app['created_at'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                    <td>
                        <a href="<?= $bp ?>/admin/branch-applications/<?= (int)$app['id'] ?>" class="admin-btn admin-btn-sm admin-btn-secondary" title="View">View</a>
                        <?php if ($status === 'pending'): ?>
                            <form method="POST" action="<?= $bp ?>/admin/branch-applications/<?= (int)$app['id'] ?>/approve" style="display:inline;">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                                <button type="submit" class="admin-btn admin-btn-sm admin-btn-success" style="background:#2e7d32;color:#fff;border:none;" title="Approve" onclick="return confirm('Approve this application?')">Approve</button>
                            </form>
                            <form method="POST" action="<?= $bp ?>/admin/branch-applications/<?= (int)$app['id'] ?>/reject" style="display:inline;">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
                                <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" title="Reject" onclick="return confirm('Reject this application?')">Reject</button>
                            </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr><td colspan="9" style="text-align:center;color:#999;">No branch applications found.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
