<?php
/**
 * Branch Application View — Gmind Institute ERP
 * Shows single application detail with approve/reject actions
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';

$statusColors = [
    'pending'  => ['bg' => '#fff3e0', 'text' => '#e65100'],
    'approved' => ['bg' => '#e8f5e9', 'text' => '#2e7d32'],
    'rejected' => ['bg' => '#ffebee', 'text' => '#c62828'],
];
$status = $application['status'] ?? 'pending';
$sc = $statusColors[$status] ?? $statusColors['pending'];
?>
<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;">Application Details</h1>
    <a href="<?= $bp ?>/admin/branch-applications" class="admin-btn admin-btn-secondary">← Back to Applications</a>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:24px;">
    <!-- Application Information -->
    <div class="admin-section" style="padding:20px;border:1px solid #e0e0e0;border-radius:8px;">
        <h2 style="margin:0 0 16px;font-size:16px;font-weight:600;">Applicant Information</h2>
        <table class="admin-detail-table" style="width:100%;">
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;width:40%;">Name</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Email</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['email'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Phone</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">City</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['city'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">State</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['state'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Address</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['address'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Pincode</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['pincode'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
        </table>
    </div>

    <!-- Plan & Status Information -->
    <div class="admin-section" style="padding:20px;border:1px solid #e0e0e0;border-radius:8px;">
        <h2 style="margin:0 0 16px;font-size:16px;font-weight:600;">Plan &amp; Status</h2>
        <table class="admin-detail-table" style="width:100%;">
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;width:40%;">Selected Plan</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['plan_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Status</td>
                <td style="padding:8px 0;">
                    <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>
            </tr>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Applied On</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['created_at'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <?php if (!empty($application['reviewed_at'])): ?>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Reviewed On</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['reviewed_at'], ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($application['admin_notes'])): ?>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Admin Notes</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['admin_notes'], ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($application['owner_name'])): ?>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Owner Name</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['owner_name'], ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($application['owner_aadhaar'])): ?>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">Owner Aadhaar</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['owner_aadhaar'], ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($application['gst_number'])): ?>
            <tr style="border-bottom:1px solid #f0f0f0;">
                <td style="padding:8px 0;font-weight:600;color:#555;">GST Number</td>
                <td style="padding:8px 0;"><?= htmlspecialchars($application['gst_number'], ENT_QUOTES, 'UTF-8') ?></td>
            </tr>
            <?php endif; ?>
        </table>
    </div>
</div>

<!-- Action Buttons (only for pending applications) -->
<?php if ($status === 'pending'): ?>
<div style="margin-top:24px;display:grid;grid-template-columns:1fr 1fr;gap:24px;">
    <!-- Approve Form -->
    <div class="admin-section" style="padding:20px;border:1px solid #c8e6c9;border-radius:8px;background:#f1f8e9;">
        <h3 style="margin:0 0 12px;font-size:15px;font-weight:600;color:#2e7d32;">Approve Application</h3>
        <form method="POST" action="<?= $bp ?>/admin/branch-applications/<?= (int)$application['id'] ?>/approve">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
            <p style="margin:0 0 12px;color:#555;">Approving will create a new branch account with the selected franchise plan.</p>
            <button type="submit" class="admin-btn admin-btn-primary" style="background:#2e7d32;color:#fff;" onclick="return confirm('Are you sure you want to approve this application?')">Approve Application</button>
        </form>
    </div>

    <!-- Reject Form -->
    <div class="admin-section" style="padding:20px;border:1px solid #ffcdd2;border-radius:8px;background:#fff5f5;">
        <h3 style="margin:0 0 12px;font-size:15px;font-weight:600;color:#c62828;">Reject Application</h3>
        <form method="POST" action="<?= $bp ?>/admin/branch-applications/<?= (int)$application['id'] ?>/reject">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">
            <div class="admin-form-group" style="margin-bottom:12px;">
                <label for="admin_notes" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Reason for Rejection <span style="color:#c62828;">*</span></label>
                <textarea id="admin_notes" name="admin_notes" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;min-height:80px;" rows="3" required placeholder="Provide a reason for rejecting this application..."></textarea>
            </div>
            <button type="submit" class="admin-btn admin-btn-danger" onclick="return confirm('Are you sure you want to reject this application?')">Reject Application</button>
        </form>
    </div>
</div>
<?php endif; ?>
