<?php
/**
 * Admin Layout — Gmind Institute
 *
 * Wraps all admin panel pages with sidebar navigation and top bar.
 * Uses BASE_PATH constant for all URL paths.
 *
 * Includes Live Chat sidebar item and chat notification icon.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';

// Get chat unread count for badge (only if logged in)
$chatUnread = 0;
try {
    if (isset($_SESSION['user_id'])) {
        $pdo = getDb();
        $stmt = $pdo->query("SELECT SUM(admin_unread) AS total FROM chat_conversations WHERE status IN ('active','pending')");
        $result = $stmt->fetch();
        $chatUnread = (int) ($result['total'] ?? 0);
    }
} catch (\Throwable $e) {
    // Table might not exist yet, ignore
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($title ?? 'Admin Panel', ENT_QUOTES, 'UTF-8') ?> | Gmind Admin</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') ?>">
</head>
<body class="admin-body">
    <!-- Mobile Sidebar Overlay -->
    <div class="admin-sidebar-overlay" id="sidebarOverlay"></div>

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

        <nav class="admin-sidebar-nav">
            <ul class="admin-nav-list">
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/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 ?>/admin/sliders" class="admin-nav-link <?= ($currentPage ?? '') === 'sliders' ? '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"/><line x1="6" y1="2" x2="6" y2="6"/><line x1="18" y1="2" x2="18" y2="6"/></svg>
                        <span>Sliders</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/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="M4 19.5A2.5 2.5 0 016.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 014 19.5v-15A2.5 2.5 0 016.5 2z"/></svg>
                        <span>Courses</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/blog-posts" class="admin-nav-link <?= ($currentPage ?? '') === 'blog_posts' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                        <span>Blog Posts</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/branches" class="admin-nav-link <?= ($currentPage ?? '') === 'branches' ? '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>Branches</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/faqs" class="admin-nav-link <?= ($currentPage ?? '') === 'faqs' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 015.83 1c0 2-3 3-3 3"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
                        <span>FAQs</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/testimonials" class="admin-nav-link <?= ($currentPage ?? '') === 'testimonials' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>
                        <span>Testimonials</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/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 ?>/admin/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"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></svg>
                        <span>Exams</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/practice-sets" class="admin-nav-link <?= ($currentPage ?? '') === 'practice-sets' ? '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>Practice Sets</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/gallery" class="admin-nav-link <?= ($currentPage ?? '') === 'gallery' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>
                        <span>Gallery</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/toppers" class="admin-nav-link <?= ($currentPage ?? '') === 'toppers' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>
                        <span>Toppers</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/affiliations" class="admin-nav-link <?= ($currentPage ?? '') === 'affiliations' ? '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>Affiliations</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/demo-class" class="admin-nav-link <?= ($currentPage ?? '') === 'demo-class' ? '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" ry="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"/><line x1="9" y1="16" x2="15" y2="16"/></svg>
                        <span>Demo Class</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/newsletter" class="admin-nav-link <?= ($currentPage ?? '') === 'newsletter' ? '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>Newsletter</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/offer-popup" class="admin-nav-link <?= ($currentPage ?? '') === 'offer-popup' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" y1="21" x2="16" y2="21"/><line x1="12" y1="17" x2="12" y2="21"/><path d="M7 8l3 3-3 3"/><line x1="13" y1="14" x2="17" y2="14"/></svg>
                        <span>Offer Popup</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/video-testimonials" class="admin-nav-link <?= ($currentPage ?? '') === 'video-testimonials' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="23 7 16 12 23 17 23 7"/><rect x="1" y="5" width="15" height="14" rx="2" ry="2"/></svg>
                        <span>Video Testimonials</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/batch-schedules" class="admin-nav-link <?= ($currentPage ?? '') === 'batch-schedules' ? '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" ry="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>Batch Schedules</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/news" class="admin-nav-link <?= ($currentPage ?? '') === 'news' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M19 20H5a2 2 0 01-2-2V6a2 2 0 012-2h10a2 2 0 012 2v1m2 13a2 2 0 01-2-2V7m2 13a2 2 0 002-2V9a2 2 0 00-2-2h-2m-4-3H9M7 16h6M7 8h6v4H7V8z"/></svg>
                        <span>News</span>
                    </a>
                </li>
                <!-- ★ Live Chat ★ -->
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/chat" class="admin-nav-link <?= ($currentPage ?? '') === 'chat' ? 'active' : '' ?>">
                        <svg class="admin-nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>
                        <span>Live Chat</span>
                        <?php if ($chatUnread > 0): ?>
                        <span style="margin-left:auto; background:#FF4757; 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;"><?= $chatUnread ?></span>
                        <?php endif; ?>
                    </a>
                </li>

                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/settings" class="admin-nav-link <?= ($currentPage ?? '') === 'settings' ? '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 004.68 15a1.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 012.83-2.83l.06.06A1.65 1.65 0 009 4.68a1.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 2.83l-.06.06A1.65 1.65 0 0019.4 9a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></svg>
                        <span>Settings</span>
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="<?= $bp ?>/admin/smtp-settings" class="admin-nav-link <?= ($currentPage ?? '') === 'smtp-settings' ? '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>SMTP Settings</span>
                    </a>
                </li>
            </ul>
        </nav>

        <div class="admin-sidebar-footer">
            <form method="POST" action="<?= $bp ?>/admin/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>

    <!-- Main Content Area -->
    <div class="admin-main">
        <!-- Top Bar -->
        <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 ?>/admin/dashboard">Admin</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">
                <!-- Chat Notification -->
                <a href="<?= $bp ?>/admin/chat" class="admin-notification" title="Live Chat Messages" style="position:relative;">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>
                    <?php if ($chatUnread > 0): ?>
                    <span class="admin-notification-badge" style="background:#4A6CF7;"><?= $chatUnread ?></span>
                    <?php endif; ?>
                </a>
                <a href="<?= $bp ?>/admin/testimonials" class="admin-notification" title="Pending Testimonials">
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20"><path d="M18 8A6 6 0 006 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 01-3.46 0"/></svg>
                    <?php if (isset($pendingTestimonials) && $pendingTestimonials > 0): ?>
                    <span class="admin-notification-badge"><?= (int) $pendingTestimonials ?></span>
                    <?php endif; ?>
                </a>
                <div class="admin-user-info">
                    <div class="admin-user-avatar">
                        <?= strtoupper(mb_substr(htmlspecialchars($currentUser['name'] ?? 'A', ENT_QUOTES, 'UTF-8'), 0, 1)) ?>
                    </div>
                    <span class="admin-user-name"><?= htmlspecialchars($currentUser['name'] ?? 'Admin', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
            </div>
        </header>

        <!-- Flash Messages -->
        <?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; ?>

        <!-- Page Content -->
        <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>

    <!-- Delete Confirmation Modal -->
    <div class="admin-modal-overlay" id="deleteModal">
        <div class="admin-modal">
            <div class="admin-modal-header">
                <h3 class="admin-modal-title">Confirm Delete</h3>
                <button class="admin-modal-close" id="deleteModalClose" aria-label="Close">&times;</button>
            </div>
            <div class="admin-modal-body">
                <p>Are you sure you want to delete this item? This action cannot be undone.</p>
            </div>
            <div class="admin-modal-footer">
                <button class="admin-btn admin-btn-secondary" id="deleteModalCancel">Cancel</button>
                <form method="POST" id="deleteForm" style="display:inline">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    <button type="submit" class="admin-btn admin-btn-danger">Delete</button>
                </form>
            </div>
        </div>
    </div>

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