<?php
/**
 * Branch Admin Layout — Gmind Institute
 *
 * Wraps all Branch Admin panel pages with sidebar navigation and top bar.
 *
 * Sidebar menus:
 *   - Dashboard
 *   - New Admission
 *   - Students
 *   - Fee Collection
 *   - Course Fees (old)
 *   - My Courses & Fees (NEW — branch-specific course fee + 3 subjects)
 *   - Batches
 *   - Exams
 *   - Exam Results
 *   - Admit Cards
 *   - ID Cards
 *   - Generate Cert & Marksheet
 *   - Print Queue (NEW — bulk certificate printing)
 *   - Facilities
 *   - Documents
 *   - My Wallet (NEW — wallet balance + transactions)
 *   - Branch Profile
 *   - Logout
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';

$userInitial = strtoupper(mb_substr(htmlspecialchars($currentUser['name'] ?? 'B', ENT_QUOTES, 'UTF-8'), 0, 1));
$branchCode  = $branchCode  ?? '';
$branchName  = $branchName  ?? '';
$branchWallet = $branchWallet ?? 0.0;
$branchStatus = $branchStatus ?? 'active';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($title ?? 'Branch Admin', ENT_QUOTES, 'UTF-8') ?> | <?= htmlspecialchars($branchCode, ENT_QUOTES, 'UTF-8') ?> - Gmind Branch</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600;700&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?= $bp ?>/css/admin.css">
    <meta name="csrf-token" content="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
    <style>
        /* Branch Admin accent color (teal) — distinct from /admin (navy) and /super-admin (purple) */
        .admin-sidebar { background: linear-gradient(180deg, #0d3b3b 0%, #072525 100%) !important; }
        .admin-sidebar-brand { background: linear-gradient(135deg, #16a085, #117a65) !important; }
        .admin-nav-link.active { background-color: rgba(22, 160, 133, 0.25) !important; border-left-color: #16a085 !important; color: #fff !important; }
        .admin-nav-link:hover { background-color: rgba(22, 160, 133, 0.15) !important; }
        .admin-topbar { background: #fff; border-bottom: 2px solid #16a085; }
        .admin-page-title { color: #0d3b3b; }
        .admin-btn-primary { background: #16a085; }
        .admin-btn-primary:hover { background: #117a65; }
        .ba-badge { background: #16a085; color: #fff; padding: 2px 8px; border-radius: 10px; font-size: 0.7rem; font-weight: 600; }
        .ba-badge-count { background: #e84118; color: #fff; font-size: 10px; font-weight: 700; min-width: 18px; height: 18px; border-radius: 9px; display: inline-flex; align-items: center; justify-content: center; padding: 0 5px; margin-left: 6px; }
        .ba-branch-header { padding: 10px 16px; background: rgba(255,255,255,0.08); border-radius: 8px; margin-bottom: 14px; font-size: 0.8rem; }
        .ba-branch-header strong { color: #1abc9c; display: block; font-size: 0.92rem; margin-bottom: 2px; }
        .ba-branch-header small { color: rgba(255,255,255,0.6); }
    </style>
</head>
<body class="admin-body">
    <div class="admin-sidebar-overlay" id="sidebarOverlay"></div>

    <aside class="admin-sidebar" id="adminSidebar">
        <div class="admin-sidebar-header">
            <a href="<?= $bp ?>/branch/dashboard" class="admin-logo">
                <span class="admin-logo-text">GMIND</span>
                <span class="admin-logo-subtext">BRANCH ADMIN</span>
            </a>
            <button class="admin-sidebar-close" id="sidebarClose" aria-label="Close sidebar">&times;</button>
        </div>

        <nav class="admin-sidebar-nav">
            <!-- Branch context block -->
            <div class="ba-branch-header">
                <strong><?= htmlspecialchars($branchName ?: 'Branch', ENT_QUOTES, 'UTF-8') ?></strong>
                <small>Code: <code style="color:#fff;background:rgba(255,255,255,0.15);padding:1px 6px;border-radius:3px;"><?= htmlspecialchars($branchCode ?: '-', ENT_QUOTES, 'UTF-8') ?></code></small><br>
                <small>Wallet: <strong style="color:#fff;">&#8377;<?= number_format((float) $branchWallet, 0) ?></strong></small><br>
                <small>Status: <strong style="color:<?= $branchStatus === 'active' ? '#1abc9c' : '#f39c12' ?>;"><?= htmlspecialchars(ucfirst($branchStatus), ENT_QUOTES, 'UTF-8') ?></strong></small>
            </div>

            <ul class="admin-nav-list">
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/dashboard" class="admin-nav-link <?= ($currentPage ?? '') === 'dashboard' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></svg>
                        <span>Dashboard</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/students/create" class="admin-nav-link <?= ($currentPage ?? '') === 'admissions' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><line x1="19" y1="8" x2="19" y2="14"/><line x1="22" y1="11" x2="16" y2="11"/></svg>
                        <span>New Admission</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/students" class="admin-nav-link <?= ($currentPage ?? '') === 'students' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 00-3-3.87"/><path d="M16 3.13a4 4 0 010 7.75"/></svg>
                        <span>Students</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/fees" class="admin-nav-link <?= ($currentPage ?? '') === 'fees' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="12" y1="1" x2="12" y2="23"/><path d="M17 5H9.5a3.5 3.5 0 000 7h5a3.5 3.5 0 010 7H6"/></svg>
                        <span>Fee Collection</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/course-fees" class="admin-nav-link <?= ($currentPage ?? '') === 'course_fees' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
                        <span>Course Fees</span>
                    </a>
                </li>

                <!-- ✅ NEW: My Courses & Fees (branch-specific course fee + 3 subjects) -->
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/courses" class="admin-nav-link <?= ($currentPage ?? '') === 'courses' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M2 3h6a4 4 0 014 4v14a3 3 0 00-3-3H2z"/><path d="M22 3h-6a4 4 0 00-4 4v14a3 3 0 013-3h7z"/></svg>
                        <span>My Courses &amp; Fees</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/batches" class="admin-nav-link <?= ($currentPage ?? '') === 'batches' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
                        <span>Batches</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/exams" class="admin-nav-link <?= ($currentPage ?? '') === 'exams' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
                        <span>Exams</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/exam-results" class="admin-nav-link <?= ($currentPage ?? '') === 'exam_results' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 11H5a2 2 0 00-2 2v7h18v-7a2 2 0 00-2-2h-4"/><polyline points="9 11 12 14 15 11"/><line x1="12" y1="2" x2="12" y2="14"/></svg>
                        <span>Exam Results</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/admit-cards" class="admin-nav-link <?= ($currentPage ?? '') === 'admit_cards' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="4" width="18" height="16" rx="2"/><circle cx="9" cy="10" r="2"/><path d="M15 8h4M15 12h4M7 16h10"/></svg>
                        <span>Admit Cards</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/id-cards" class="admin-nav-link <?= ($currentPage ?? '') === 'id_cards' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="2" y="4" width="20" height="16" rx="2"/><circle cx="8" cy="10" r="3"/><path d="M14 8h6M14 12h6M6 16h12"/></svg>
                        <span>ID Cards</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/cert-gen" class="admin-nav-link <?= ($currentPage ?? '') === 'cert_gen' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="8" r="6"/><path d="M15.477 12.89L17 22l-5-3-5 3 1.523-9.11"/></svg>
                        <span>Generate Cert & Marksheet <span class="ba-badge" style="background:#16a085;">NEW</span></span>
                    </a>
                </li>

                <!-- ✅ NEW: Print Queue (bulk certificate printing) -->
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/print-queue" class="admin-nav-link <?= ($currentPage ?? '') === 'print_queue' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="6 9 6 2 18 2 18 9"/><path d="M6 18H4a2 2 0 01-2-2v-5a2 2 0 012-2h16a2 2 0 012 2v5a2 2 0 01-2 2h-2"/><rect x="6" y="14" width="12" height="8"/></svg>
                        <span>Print Queue <span class="ba-badge" style="background:#e84118;">BULK</span></span>
                    </a>
                </li>

                <li class="admin-nav-divider" style="border-top:1px solid rgba(255,255,255,0.1);margin:12px 0;"></li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/documents" class="admin-nav-link <?= ($currentPage ?? '') === 'documents' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/></svg>
                        <span>Documents</span>
                    </a>
                </li>

                <!-- ✅ NEW: Facilities -->
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/facilities" class="admin-nav-link <?= ($currentPage ?? '') === 'facilities' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-4 0v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83 0 2 2 0 010-2.83l.06-.06a1.65 1.65 0 00.33-1.82 1.65 1.65 0 00-1.51-1H3a2 2 0 010-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 010-2.83 2 2 0 012.83 0l.06.06a1.65 1.65 0 001.82.33H9a1.65 1.65 0 001-1.51V3a2 2 0 014 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 0 2 2 0 010 2.83l-.06.06a1.65 1.65 0 00-.33 1.82V9a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></svg>
                        <span>Facilities</span>
                    </a>
                </li>

                <!-- ✅ NEW: My Wallet (wallet balance + transactions) -->
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/wallet" class="admin-nav-link <?= ($currentPage ?? '') === 'wallet' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 12V7H5a2 2 0 010-4h14v4"/><path d="M3 5v14a2 2 0 002 2h16v-5"/><path d="M18 12a2 2 0 000 4h4v-4z"/></svg>
                        <span>My Wallet</span>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/branch/profile" class="admin-nav-link <?= ($currentPage ?? '') === 'profile' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z"/><circle cx="12" cy="10" r="3"/></svg>
                        <span>Branch Profile</span>
                    </a>
                </li>
            </ul>
        </nav>

        <div class="admin-sidebar-footer">
            <form method="POST" action="<?= $bp ?>/branch/logout" class="admin-logout-form">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <button type="submit" class="admin-nav-link admin-logout-link">
                    <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg>
                    <span>Logout</span>
                </button>
            </form>
        </div>
    </aside>

    <div class="admin-main">
        <header class="admin-topbar">
            <div class="admin-topbar-left">
                <button class="admin-hamburger" id="sidebarToggle" aria-label="Toggle sidebar">
                    <span></span><span></span><span></span>
                </button>
                <div class="admin-breadcrumb">
                    <a href="<?= $bp ?>/branch/dashboard"><?= htmlspecialchars($branchCode ?: 'Branch', ENT_QUOTES, 'UTF-8') ?></a>
                    <span class="admin-breadcrumb-sep">/</span>
                    <span class="admin-breadcrumb-current"><?= htmlspecialchars($title ?? 'Dashboard', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
            </div>
            <div class="admin-topbar-right">
                <div class="admin-user-info">
                    <div class="admin-user-avatar" style="background:linear-gradient(135deg,#16a085,#117a65);color:#fff;">
                        <?= $userInitial ?>
                    </div>
                    <span class="admin-user-name"><?= htmlspecialchars($currentUser['name'] ?? 'Branch Admin', ENT_QUOTES, 'UTF-8') ?></span>
                    <span class="ba-badge">BRANCH ADMIN</span>
                </div>
            </div>
        </header>

        <?php if (!empty($flash_error)): ?>
        <div class="admin-alert admin-alert-error"><span><?= htmlspecialchars($flash_error, ENT_QUOTES, 'UTF-8') ?></span><button class="admin-alert-close" aria-label="Close">&times;</button></div>
        <?php endif; ?>
        <?php if (!empty($flash_success)): ?>
        <div class="admin-alert admin-alert-success"><span><?= htmlspecialchars($flash_success, ENT_QUOTES, 'UTF-8') ?></span><button class="admin-alert-close" aria-label="Close">&times;</button></div>
        <?php endif; ?>
        <?php if (!empty($flash_warning)): ?>
        <div class="admin-alert admin-alert-warning"><span><?= htmlspecialchars($flash_warning, ENT_QUOTES, 'UTF-8') ?></span><button class="admin-alert-close" aria-label="Close">&times;</button></div>
        <?php endif; ?>
        <?php if (!empty($flash_info)): ?>
        <div class="admin-alert admin-alert-info"><span><?= htmlspecialchars($flash_info, ENT_QUOTES, 'UTF-8') ?></span><button class="admin-alert-close" aria-label="Close">&times;</button></div>
        <?php endif; ?>

        <main class="admin-content">
            <div class="admin-page-header">
                <h1 class="admin-page-title"><?= htmlspecialchars($title ?? 'Dashboard', ENT_QUOTES, 'UTF-8') ?></h1>
            </div>
            <?= $content ?? '' ?>
        </main>
    </div>

    <script src="<?= $bp ?>/js/admin.js"></script>
</body>
</html>
