<?php
/**
 * Branch Admin Dashboard View
 *
 * Branch-specific overview: stats, recent announcements, recent students,
 * recent fee payments, branch ranking.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$branch    = $branch    ?? [];
$stats     = $stats     ?? [];
$recentAnnouncements = $recentAnnouncements ?? [];
$recentStudents = $recentStudents ?? [];
$recentFees  = $recentFees ?? [];
$topBranches = $topBranches ?? [];
$myRank      = $myRank ?? null;
?>
<div class="admin-section">

    <!-- Welcome Banner -->
    <div style="background:linear-gradient(135deg,#16a085,#117a65);color:#fff;border-radius:12px;padding:24px;margin-bottom:20px;">
        <h2 style="margin:0 0 6px;font-size:1.3rem;font-family:'Poppins',sans-serif;">Welcome, <?= htmlspecialchars($currentUser['name'] ?? 'Branch Admin', ENT_QUOTES, 'UTF-8') ?>! 👋</h2>
        <p style="margin:0;opacity:0.9;font-size:0.9rem;">
            <strong><?= htmlspecialchars($branch['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
            · Code: <code style="background:rgba(255,255,255,0.25);padding:2px 10px;border-radius:4px;"><?= htmlspecialchars($branch['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?></code>
            · <?= htmlspecialchars($branch['city'] ?? '', ENT_QUOTES, 'UTF-8') ?>, <?= htmlspecialchars($branch['state'] ?? '', ENT_QUOTES, 'UTF-8') ?>
            <?php if (!empty($myRank)): ?>
                · <span style="background:rgba(255,255,255,0.2);padding:2px 10px;border-radius:10px;font-size:0.78rem;">🏆 Rank #<?= (int) $myRank ?></span>
            <?php endif; ?>
        </p>
    </div>

    <!-- Stats Row -->
    <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(170px,1fr));gap:14px;margin-bottom:24px;">
        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:24px;font-weight:700;color:#16a085;font-family:'Poppins',sans-serif;"><?= (int) ($stats['total_students'] ?? 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Total Students</div>
            <div style="font-size:0.72rem;color:#27ae60;margin-top:2px;"><?= (int) ($stats['active_students'] ?? 0) ?> active</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:24px;font-weight:700;color:#6c3fe8;font-family:'Poppins',sans-serif;"><?= (int) ($stats['certificates_generated'] ?? 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Certificates Issued</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:24px;font-weight:700;color:#f39c12;font-family:'Poppins',sans-serif;"><?= (int) ($stats['exams_conducted'] ?? 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Exams Conducted</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:24px;font-weight:700;color:#e84118;font-family:'Poppins',sans-serif;"><?= (int) ($stats['pending_approvals'] ?? 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Pending Approvals</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:22px;font-weight:700;color:#16a085;font-family:'Poppins',sans-serif;">&#8377;<?= number_format((float) ($stats['fee_collected_month'] ?? 0), 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Fee This Month</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:22px;font-weight:700;color:#27ae60;font-family:'Poppins',sans-serif;">&#8377;<?= number_format((float) ($stats['fee_collected_total'] ?? 0), 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Total Fee Collected</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:22px;font-weight:700;color:#3498db;font-family:'Poppins',sans-serif;">&#8377;<?= number_format((float) ($stats['wallet_balance'] ?? 0), 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Wallet Balance</div>
        </div>
        <div style="background:#fff;border-radius:10px;padding:18px;box-shadow:0 2px 6px rgba(0,0,0,0.05);">
            <div style="font-size:22px;font-weight:700;color:#9b59b6;font-family:'Poppins',sans-serif;">&#8377;<?= number_format((float) ($stats['referral_earnings'] ?? 0), 0) ?></div>
            <div style="font-size:0.78rem;color:#666;margin-top:4px;text-transform:uppercase;">Referral Earnings</div>
        </div>
    </div>

    <!-- Two-column section -->
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">

        <!-- Recent Announcements -->
        <div 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.05rem;color:#0d3b3b;">📢 Announcements from Super Admin</h2>
            <?php if (empty($recentAnnouncements)): ?>
                <p style="text-align:center;color:#999;padding:20px 0;margin:0;">No recent announcements.</p>
            <?php else: ?>
                <div style="display:flex;flex-direction:column;gap:10px;">
                    <?php foreach ($recentAnnouncements as $ann): ?>
                        <div style="padding:10px 12px;background:#f9f9f9;border-left:3px solid <?= ($ann['type'] ?? 'general') === 'urgent' ? '#e84118' : (($ann['type'] ?? 'general') === 'info' ? '#3498db' : '#16a085') ?>;border-radius:6px;">
                            <div style="font-weight:600;color:#0d3b3b;font-size:0.88rem;"><?= htmlspecialchars($ann['title'] ?? $ann['text'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                            <?php if (!empty($ann['text'])): ?>
                                <div style="font-size:0.82rem;color:#666;margin-top:4px;"><?= htmlspecialchars(mb_substr($ann['text'], 0, 120), ENT_QUOTES, 'UTF-8') ?><?= mb_strlen($ann['text']) > 120 ? '...' : '' ?></div>
                            <?php endif; ?>
                            <div style="font-size:0.72rem;color:#bbb;margin-top:4px;"><?= date('M j, Y', strtotime($ann['created_at'] ?? 'now')) ?> <?= !empty($ann['branch_id']) ? '· Branch-specific' : '· Global' ?></div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

        <!-- Recent Students -->
        <div 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.05rem;color:#0d3b3b;">👥 Recent Admissions</h2>
            <?php if (empty($recentStudents)): ?>
                <p style="text-align:center;color:#999;padding:20px 0;margin:0;">No students admitted yet.</p>
                <p style="text-align:center;font-size:0.82rem;margin-top:8px;"><a href="javascript:void(0)" style="color:#16a085;text-decoration:none;font-weight:600;">Admit first student (Phase 3) →</a></p>
            <?php else: ?>
                <div style="display:flex;flex-direction:column;gap:8px;">
                    <?php foreach ($recentStudents as $s): ?>
                        <div style="display:flex;justify-content:space-between;align-items:center;padding:8px 10px;background:#fafafa;border-radius:6px;">
                            <div>
                                <div style="font-weight:600;color:#0d3b3b;font-size:0.86rem;"><?= htmlspecialchars($s['name'] ?? 'Unknown', ENT_QUOTES, 'UTF-8') ?></div>
                                <div style="font-size:0.74rem;color:#999;margin-top:2px;"><?= htmlspecialchars($s['course_name'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?> · <?= htmlspecialchars($s['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                            </div>
                            <div style="text-align:right;font-size:0.74rem;color:#bbb;"><?= date('M j', strtotime($s['created_at'] ?? 'now')) ?></div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Recent Fees + Branch Ranking -->
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
        <div 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.05rem;color:#0d3b3b;">💰 Recent Fee Payments</h2>
            <?php if (empty($recentFees)): ?>
                <p style="text-align:center;color:#999;padding:20px 0;margin:0;">No fee payments recorded yet.</p>
            <?php else: ?>
                <div style="display:flex;flex-direction:column;gap:8px;">
                    <?php foreach ($recentFees as $f): ?>
                        <div style="display:flex;justify-content:space-between;align-items:center;padding:8px 10px;background:#fafafa;border-radius:6px;">
                            <div>
                                <div style="font-weight:600;color:#0d3b3b;font-size:0.86rem;"><?= htmlspecialchars($f['student_name'] ?? 'Student', ENT_QUOTES, 'UTF-8') ?></div>
                                <div style="font-size:0.72rem;color:#999;margin-top:2px;"><?= htmlspecialchars($f['receipt_number'] ?? $f['receipt_no'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?> · <?= htmlspecialchars($f['payment_mode'] ?? 'cash', ENT_QUOTES, 'UTF-8') ?></div>
                            </div>
                            <div style="text-align:right;">
                                <div style="font-weight:700;color:#27ae60;font-size:0.9rem;">&#8377;<?= number_format((float) ($f['amount'] ?? 0), 0) ?></div>
                                <div style="font-size:0.7rem;color:#bbb;"><?= date('M j', strtotime($f['payment_date'] ?? $f['created_at'] ?? 'now')) ?></div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

        <!-- Branch Ranking -->
        <div 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.05rem;color:#0d3b3b;">🏆 Branch Rankings</h2>
            <?php if (empty($topBranches)): ?>
                <p style="text-align:center;color:#999;padding:20px 0;margin:0;">No ranking data yet.</p>
            <?php else: ?>
                <div style="display:flex;flex-direction:column;gap:8px;">
                    <?php foreach ($topBranches as $i => $b): ?>
                        <div style="display:flex;justify-content:space-between;align-items:center;padding:8px 10px;background:<?= (int) ($branch['id'] ?? 0) === (int) ($b['id'] ?? 0) ? '#eafaf1' : '#fafafa' ?>;border-radius:6px;<?= (int) ($branch['id'] ?? 0) === (int) ($b['id'] ?? 0) ? 'border:1px solid #27ae60;' : '' ?>">
                            <div style="display:flex;align-items:center;gap:8px;">
                                <div style="width:22px;height:22px;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.74rem;"><?= $i + 1 ?></div>
                                <div>
                                    <div style="font-weight:600;color:#0d3b3b;font-size:0.82rem;"><?= htmlspecialchars($b['name'] ?? '', ENT_QUOTES, 'UTF-8') ?> <code style="font-size:0.72rem;color:#999;"><?= htmlspecialchars($b['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?></code> <?= (int) ($branch['id'] ?? 0) === (int) ($b['id'] ?? 0) ? '<span style="color:#27ae60;font-size:0.72rem;">(You)</span>' : '' ?></div>
                                </div>
                            </div>
                            <div style="text-align:right;font-size:0.74rem;">
                                <strong style="color:#16a085;"><?= (int) ($b['total_students'] ?? 0) ?></strong> students
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <?php if (!empty($myRank)): ?>
                    <div style="margin-top:12px;padding:10px;background:#eafaf1;border-radius:6px;text-align:center;font-size:0.86rem;color:#1e7e34;">
                        <strong>Your Rank: #<?= (int) $myRank ?></strong> out of <?= count($topBranches) >= 5 ? 'all active branches' : count($topBranches) . ' branch(es)' ?>
                    </div>
                <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>
</div>
