<?php
/**
 * Super Admin — Branch Application Detail / Review
 *
 * If status=pending: shows approve form (branch_code + admin_password fields)
 * If status=approved/rejected: shows read-only details + result
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$application = $application ?? [];
$plans       = $plans ?? [];
$nextCode    = $nextCode ?? 'GMBR02';
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <a href="<?= $bp ?>/super-admin/branch-applications" style="color:#6c3fe8;text-decoration:none;font-size:0.85rem;display:inline-flex;align-items:center;gap:4px;">
            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15 18 9 12 15 6"/></svg>
            Back to Applications
        </a>
    </div>

    <!-- Header -->
    <div class="admin-card" style="background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;border-radius:12px;padding:24px;margin-bottom:20px;">
        <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:16px;">
            <div>
                <h1 style="margin:0;font-size:1.4rem;font-family:'Poppins',sans-serif;"><?= htmlspecialchars($application['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></h1>
                <p style="margin:6px 0 0;opacity:0.9;font-size:0.9rem;">
                    <?= htmlspecialchars($application['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($application['state'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                    · Applied: <?= date('M j, Y g:i A', strtotime($application['created_at'] ?? 'now')) ?>
                </p>
            </div>
            <span style="display:inline-block;padding:6px 14px;border-radius:20px;font-size:0.78rem;font-weight:600;text-transform:uppercase;background:rgba(255,255,255,0.25);"><?= htmlspecialchars($application['status'] ?? 'pending', ENT_QUOTES, 'UTF-8') ?></span>
        </div>
    </div>

    <!-- Application Details -->
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 14px;font-size:1rem;color:#2c1e4a;">Applicant Information</h2>
            <table style="width:100%;font-size:0.86rem;">
                <tr><td style="padding:6px 0;color:#666;">Name</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($application['name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Email</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($application['email'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Phone</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($application['phone'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">City</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($application['city'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">State</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($application['state'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Address</td><td style="padding:6px 0;text-align:right;font-weight:600;max-width:240px;"><?= htmlspecialchars($application['address'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
            </table>
        </div>

        <div class="admin-card" style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 14px;font-size:1rem;color:#2c1e4a;">Investment & Plan</h2>
            <table style="width:100%;font-size:0.86rem;">
                <tr><td style="padding:6px 0;color:#666;">Investment Range</td><td style="padding:6px 0;text-align:right;font-weight:600;"><?= htmlspecialchars($application['investment_range'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td></tr>
                <tr><td style="padding:6px 0;color:#666;">Preferred Plan</td>
                    <td style="padding:6px 0;text-align:right;font-weight:600;">
                        <?php
                        $planName = '—';
                        foreach ($plans as $p) {
                            if ($p['id'] == ($application['plan_id'] ?? null)) { $planName = $p['name'] . ' (₹' . number_format($p['price'], 0) . ')'; break; }
                        }
                        echo htmlspecialchars($planName, ENT_QUOTES, 'UTF-8');
                        ?>
                    </td>
                </tr>
                <tr><td style="padding:6px 0;color:#666;vertical-align:top;">Message</td><td style="padding:6px 0;text-align:right;font-weight:600;max-width:240px;"><?= nl2br(htmlspecialchars($application['message'] ?? '—', ENT_QUOTES, 'UTF-8')) ?></td></tr>
            </table>
        </div>
    </div>

    <?php if (!empty($application['admin_notes'])): ?>
    <div class="admin-card" style="background:#fff8e1;border:1px solid #f39c12;border-radius:12px;padding:16px;margin-bottom:20px;">
        <strong style="color:#8a6d3b;">Admin Notes:</strong>
        <p style="margin:6px 0 0;color:#8a6d3b;font-size:0.86rem;"><?= nl2br(htmlspecialchars($application['admin_notes'], ENT_QUOTES, 'UTF-8')) ?></p>
        <?php if (!empty($application['reviewed_at'])): ?>
            <p style="margin:8px 0 0;color:#999;font-size:0.78rem;">Reviewed: <?= date('M j, Y g:i A', strtotime($application['reviewed_at'])) ?></p>
        <?php endif; ?>
    </div>
    <?php endif; ?>

    <?php if (($application['status'] ?? '') === 'pending'): ?>
        <!-- Approve / Reject Form -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:24px;box-shadow:0 2px 8px rgba(0,0,0,0.06);border-left:4px solid #27ae60;">
            <h2 style="margin:0 0 6px;font-size:1.1rem;color:#2c1e4a;">Review & Approve</h2>
            <p style="margin:0 0 20px;font-size:0.85rem;color:#666;">Approving will create a new branch + branch_admin login account. The applicant can then log in at <code><?= $bp ?>/branch/login</code>.</p>

            <form method="POST" action="<?= $bp ?>/super-admin/branch-applications/<?= (int) ($application['id'] ?? 0) ?>/approve" style="margin-bottom:16px;">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:14px;align-items:end;">
                    <div>
                        <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Branch Code</label>
                        <input type="text" name="branch_code" value="<?= htmlspecialchars($nextCode, ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;font-family:monospace;text-transform:uppercase;">
                    </div>
                    <div>
                        <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Initial Password</label>
                        <input type="text" name="admin_password" value="Branch@123" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                    </div>
                    <div>
                        <button type="submit" style="width:100%;padding:10px;background:linear-gradient(135deg,#27ae60,#229954);color:#fff;border:none;border-radius:6px;font-weight:600;font-size:0.88rem;cursor:pointer;font-family:'Poppins',sans-serif;">✓ Approve & Create Branch</button>
                    </div>
                </div>
            </form>

            <form method="POST" action="<?= $bp ?>/super-admin/branch-applications/<?= (int) ($application['id'] ?? 0) ?>/reject" onsubmit="return confirm('Reject this application?');" style="display:flex;gap:10px;align-items:end;">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <div style="flex:1;">
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Rejection Reason</label>
                    <input type="text" name="reason" placeholder="e.g. Investment too low, area already covered, etc." style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <button type="submit" style="padding:10px 20px;background:#e84118;color:#fff;border:none;border-radius:6px;font-weight:600;font-size:0.88rem;cursor:pointer;">✗ Reject</button>
            </form>
        </div>
    <?php elseif (($application['status'] ?? '') === 'approved' && !empty($application['created_branch_code'])): ?>
        <div class="admin-card" style="background:#eafaf1;border:1px solid #27ae60;border-radius:12px;padding:20px;">
            <h3 style="margin:0 0 6px;color:#1e7e34;">✓ Approved</h3>
            <p style="margin:0;color:#1e7e34;font-size:0.86rem;">Branch created with code <code style="background:#27ae60;color:#fff;padding:2px 8px;border-radius:4px;"><?= htmlspecialchars($application['created_branch_code'], ENT_QUOTES, 'UTF-8') ?></code>. Admin login: <?= htmlspecialchars($application['created_admin_email'] ?? $application['email'], ENT_QUOTES, 'UTF-8') ?></p>
            <?php if (!empty($application['created_branch_id'])): ?>
                <a href="<?= $bp ?>/super-admin/branches/<?= (int) $application['created_branch_id'] ?>" style="display:inline-block;margin-top:10px;color:#27ae60;text-decoration:none;font-size:0.85rem;font-weight:600;">View Branch →</a>
            <?php endif; ?>
        </div>
    <?php elseif (($application['status'] ?? '') === 'rejected'): ?>
        <div class="admin-card" style="background:#fff5f5;border:1px solid #e84118;border-radius:12px;padding:20px;">
            <h3 style="margin:0 0 6px;color:#c0392b;">✗ Rejected</h3>
            <p style="margin:0;color:#c0392b;font-size:0.86rem;">This application was rejected on <?= date('M j, Y', strtotime($application['reviewed_at'] ?? 'now')) ?>.</p>
        </div>

        <form method="POST" action="<?= $bp ?>/super-admin/branch-applications/<?= (int) ($application['id'] ?? 0) ?>/delete" onsubmit="return confirm('Permanently delete this rejected application?');" style="margin-top:14px;text-align:right;">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
            <button type="submit" style="padding:10px 20px;background:#e84118;color:#fff;border:none;border-radius:6px;font-weight:600;font-size:0.85rem;cursor:pointer;">Delete Application</button>
        </form>
    <?php endif; ?>
</div>
