<?php
/**
 * Super Admin Dashboard View
 *
 * Overview: total stats, recent applications, top branches, recent activity.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$stats = $stats ?? [];
$recentApplications = $recentApplications ?? [];
$topBranches = $topBranches ?? [];
$recentActivity = $recentActivity ?? [];
$recentAnnouncements = $recentAnnouncements ?? [];
?>
<div class="admin-section">

    <!-- Stats Row -->
    <div class="admin-stats-row" style="display:grid; grid-template-columns:repeat(auto-fill, minmax(180px, 1fr)); gap:16px; margin-bottom:24px;">

        <div class="admin-stat-card" style="background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;border-radius:12px;padding:20px;">
            <div style="font-size:12px;opacity:0.85;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">Total Branches</div>
            <div style="font-size:28px;font-weight:700;font-family:'Poppins',sans-serif;"><?= (int) ($stats['total_branches'] ?? 0) ?></div>
            <div style="font-size:11px;opacity:0.85;margin-top:6px;"><?= (int) ($stats['active_branches'] ?? 0) ?> active · <?= (int) ($stats['pending_branches'] ?? 0) ?> pending</div>
        </div>

        <div class="admin-stat-card" style="background:linear-gradient(135deg,#27ae60,#229954);color:#fff;border-radius:12px;padding:20px;">
            <div style="font-size:12px;opacity:0.85;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">Branch Admins</div>
            <div style="font-size:28px;font-weight:700;font-family:'Poppins',sans-serif;"><?= (int) ($stats['total_branch_admins'] ?? 0) ?></div>
            <div style="font-size:11px;opacity:0.85;margin-top:6px;">Active accounts</div>
        </div>

        <div class="admin-stat-card" style="background:linear-gradient(135deg,#f39c12,#d68910);color:#fff;border-radius:12px;padding:20px;">
            <div style="font-size:12px;opacity:0.85;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">Total Students</div>
            <div style="font-size:28px;font-weight:700;font-family:'Poppins',sans-serif;"><?= number_format((int) ($stats['total_students'] ?? 0)) ?></div>
            <div style="font-size:11px;opacity:0.85;margin-top:6px;">Across all branches</div>
        </div>

        <div class="admin-stat-card" style="background:linear-gradient(135deg,#3498db,#2874a6);color:#fff;border-radius:12px;padding:20px;">
            <div style="font-size:12px;opacity:0.85;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">Certificates</div>
            <div style="font-size:28px;font-weight:700;font-family:'Poppins',sans-serif;"><?= number_format((int) ($stats['total_certificates'] ?? 0)) ?></div>
            <div style="font-size:11px;opacity:0.85;margin-top:6px;">Issued so far</div>
        </div>

        <div class="admin-stat-card" style="background:linear-gradient(135deg,#e84118,#c0392b);color:#fff;border-radius:12px;padding:20px;">
            <div style="font-size:12px;opacity:0.85;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">Pending Applications</div>
            <div style="font-size:28px;font-weight:700;font-family:'Poppins',sans-serif;"><?= (int) ($stats['pending_applications'] ?? 0) ?></div>
            <div style="font-size:11px;opacity:0.85;margin-top:6px;">Franchise leads</div>
        </div>

        <div class="admin-stat-card" style="background:linear-gradient(135deg,#16a085,#117a65);color:#fff;border-radius:12px;padding:20px;">
            <div style="font-size:12px;opacity:0.85;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">Total Fee Collected</div>
            <div style="font-size:24px;font-weight:700;font-family:'Poppins',sans-serif;">&#8377;<?= number_format((float) ($stats['total_fee_collected'] ?? 0), 0) ?></div>
            <div style="font-size:11px;opacity:0.85;margin-top:6px;">All branches</div>
        </div>

        <div class="admin-stat-card" style="background:linear-gradient(135deg,#9b59b6,#7d3c98);color:#fff;border-radius:12px;padding:20px;">
            <div style="font-size:12px;opacity:0.85;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">Referral Earnings</div>
            <div style="font-size:24px;font-weight:700;font-family:'Poppins',sans-serif;">&#8377;<?= number_format((float) ($stats['total_referral_earnings'] ?? 0), 0) ?></div>
            <div style="font-size:11px;opacity:0.85;margin-top:6px;">Total network</div>
        </div>

        <div class="admin-stat-card" style="background:linear-gradient(135deg,#34495e,#283747);color:#fff;border-radius:12px;padding:20px;">
            <div style="font-size:12px;opacity:0.85;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">Courses & Exams</div>
            <div style="font-size:24px;font-weight:700;font-family:'Poppins',sans-serif;"><?= (int) ($stats['total_courses'] ?? 0) ?> / <?= (int) ($stats['total_exams'] ?? 0) ?></div>
            <div style="font-size:11px;opacity:0.85;margin-top:6px;">Courses / Exams</div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div style="margin-bottom:24px; display:flex; gap:10px; flex-wrap:wrap;">
        <a href="<?= $bp ?>/super-admin/branches/create" class="admin-btn admin-btn-primary" style="text-decoration:none;padding:10px 20px;border-radius:8px;font-size:0.9rem;font-weight:600;color:#fff;">+ Add New Branch</a>
        <a href="<?= $bp ?>/super-admin/branch-admins/create" class="admin-btn" style="background:#27ae60;color:#fff;text-decoration:none;padding:10px 20px;border-radius:8px;font-size:0.9rem;font-weight:600;">+ Add Branch Admin</a>
        <a href="<?= $bp ?>/super-admin/branch-applications" class="admin-btn" style="background:#f39c12;color:#fff;text-decoration:none;padding:10px 20px;border-radius:8px;font-size:0.9rem;font-weight:600;">Review Applications (<?= (int) ($stats['pending_applications'] ?? 0) ?>)</a>
    </div>

    <!-- Two-column section: Recent Applications + Top Branches -->
    <div style="display:grid; grid-template-columns:1fr 1fr; gap:20px; margin-bottom:24px;">
        <!-- Recent Applications -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:14px;">
                <h2 style="margin:0;font-size:1.1rem;color:#2c1e4a;">Recent Franchise Applications</h2>
                <a href="<?= $bp ?>/super-admin/branch-applications" style="font-size:0.82rem;color:#6c3fe8;text-decoration:none;font-weight:600;">View All &rarr;</a>
            </div>
            <?php if (empty($recentApplications)): ?>
                <p style="text-align:center;color:#999;padding:20px 0;margin:0;">No applications yet.</p>
            <?php else: ?>
                <div style="display:flex;flex-direction:column;gap:10px;">
                    <?php foreach ($recentApplications as $app): ?>
                        <a href="<?= $bp ?>/super-admin/branch-applications/<?= (int) $app['id'] ?>" style="display:flex;justify-content:space-between;align-items:center;padding:10px 12px;background:#fafafa;border-radius:8px;text-decoration:none;color:inherit;border-left:3px solid <?= $app['status'] === 'pending' ? '#f39c12' : ($app['status'] === 'approved' ? '#27ae60' : '#e84118') ?>">
                            <div>
                                <div style="font-weight:600;color:#2c1e4a;font-size:0.88rem;"><?= htmlspecialchars($app['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                <div style="font-size:0.76rem;color:#999;margin-top:2px;"><?= htmlspecialchars($app['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($app['state'] ?? '', ENT_QUOTES, 'UTF-8') ?> · <?= htmlspecialchars($app['email'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                            </div>
                            <div style="text-align:right;">
                                <span style="font-size:0.72rem;font-weight:600;text-transform:uppercase;padding:2px 8px;border-radius:10px;background:<?= $app['status'] === 'pending' ? '#fff3e0' : ($app['status'] === 'approved' ? '#eafaf1' : '#fff5f5') ?>;color:<?= $app['status'] === 'pending' ? '#f39c12' : ($app['status'] === 'approved' ? '#27ae60' : '#e84118') ?>;">
                                    <?= htmlspecialchars($app['status'] ?? 'pending', ENT_QUOTES, 'UTF-8') ?>
                                </span>
                                <div style="font-size:0.7rem;color:#bbb;margin-top:4px;"><?= date('M j, Y', strtotime($app['created_at'] ?? 'now')) ?></div>
                            </div>
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

        <!-- Top Branches -->
        <div class="admin-card" style="background:#fff;border-radius:12px;padding:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:14px;">
                <h2 style="margin:0;font-size:1.1rem;color:#2c1e4a;">Top Performing Branches</h2>
                <a href="<?= $bp ?>/super-admin/branches" style="font-size:0.82rem;color:#6c3fe8;text-decoration:none;font-weight:600;">View All &rarr;</a>
            </div>
            <?php if (empty($topBranches)): ?>
                <p style="text-align:center;color:#999;padding:20px 0;margin:0;">No branches yet.</p>
            <?php else: ?>
                <div style="display:flex;flex-direction:column;gap:10px;">
                    <?php foreach ($topBranches as $i => $b): ?>
                        <a href="<?= $bp ?>/super-admin/branches/<?= (int) $b['id'] ?>" style="display:flex;justify-content:space-between;align-items:center;padding:10px 12px;background:#fafafa;border-radius:8px;text-decoration:none;color:inherit;">
                            <div style="display:flex;align-items:center;gap:10px;">
                                <div style="width:28px;height:28px;border-radius:50%;background:<?= $i === 0 ? '#f1c40f' : ($i === 1 ? '#bdc3c7' : ($i === 2 ? '#cd7f32' : '#bbb')) ?>;color:#fff;display:flex;align-items:center;justify-content:center;font-weight:700;font-size:0.85rem;"><?= $i + 1 ?></div>
                                <div>
                                    <div style="font-weight:600;color:#2c1e4a;font-size:0.88rem;"><?= htmlspecialchars($b['name'] ?? '', ENT_QUOTES, 'UTF-8') ?> <span style="color:#999;font-weight:400;font-size:0.78rem;"><?= htmlspecialchars($b['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?></span></div>
                                    <div style="font-size:0.76rem;color:#999;margin-top:2px;"><?= htmlspecialchars($b['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($b['state'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                </div>
                            </div>
                            <div style="text-align:right;font-size:0.78rem;">
                                <div><strong style="color:#27ae60;"><?= (int) ($b['total_students'] ?? 0) ?></strong> students</div>
                                <div style="color:#999;margin-top:2px;"><strong>&#8377;<?= number_format((float) ($b['total_revenue'] ?? 0), 0) ?></strong> revenue</div>
                            </div>
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Recent Activity Log -->
    <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:1.1rem;color:#2c1e4a;">Recent Activity Log</h2>
        <?php if (empty($recentActivity)): ?>
            <p style="text-align:center;color:#999;padding:20px 0;margin:0;">No recent activity.</p>
        <?php else: ?>
            <div style="display:flex;flex-direction:column;gap:8px;max-height:300px;overflow-y:auto;">
                <?php foreach ($recentActivity as $log): ?>
                    <div style="display:flex;justify-content:space-between;align-items:center;padding:8px 10px;border-bottom:1px solid #f0f0f0;font-size:0.84rem;">
                        <div>
                            <strong style="color:#2c1e4a;"><?= htmlspecialchars($log['action'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
                            <span style="color:#999;margin-left:6px;"><?= htmlspecialchars($log['entity_type'] ?? '', ENT_QUOTES, 'UTF-8') ?><?= !empty($log['entity_id']) ? ' #' . (int) $log['entity_id'] : '' ?></span>
                            <div style="font-size:0.76rem;color:#bbb;margin-top:2px;"><?= htmlspecialchars($log['user_name'] ?? 'System', ENT_QUOTES, 'UTF-8') ?> · <?= date('M j, Y g:i A', strtotime($log['created_at'] ?? 'now')) ?></div>
                        </div>
                        <?php if (!empty($log['ip_address'])): ?>
                            <span style="font-size:0.7rem;color:#bbb;font-family:monospace;"><?= htmlspecialchars($log['ip_address'], ENT_QUOTES, 'UTF-8') ?></span>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>
