<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require_once '../db_connect.php'; 

// --- Logout Logic (Must be at the top) ---
if (isset($_GET['logout']) && $_GET['logout'] == 'true') {
    session_unset();    // Clear all session variables
    session_destroy();  // Destroy the session
    header("Location: login.php"); // Redirect to login
    exit;
}

// Security Check: Redirect to login page if not logged in
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header("Location: login.php");
    exit;
}

// Get current page name (to highlight active menu)
$current_page = str_replace('.php', '', basename($_SERVER['PHP_SELF']));

// ==========================================
// ⚙️ FETCH SETTINGS FOR FAVICON & LOGO
// ==========================================
$settings = [];
try {
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings");
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $settings[$row['setting_key']] = $row['setting_value'];
    }
} catch (PDOException $e) {}

// 🟢 SMART LOGO PATH FIXER (For Admin Panel)
$raw_logo = $settings['site_logo'] ?? '';
$site_logo = '../assets/images/logo.png'; // Default logo path

if (!empty($raw_logo)) {
    if (strpos($raw_logo, 'http') === 0) {
        $site_logo = $raw_logo;
    } else {
        // '../' রিমুভ করে নতুন করে '../' যোগ করা হচ্ছে যাতে অ্যাডমিন ফোল্ডার থেকে ঠিকমতো ইমেজ পায়
        $clean_path = ltrim(str_replace('../', '', $raw_logo), '/');
        $site_logo = '../' . $clean_path;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ahmad Academy | Admin Panel</title>
    
    <link rel="icon" type="image/png" href="<?= htmlspecialchars($site_logo) ?>?v=<?= time() ?>">
    <link rel="apple-touch-icon" href="<?= htmlspecialchars($site_logo) ?>?v=<?= time() ?>">
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <style>
        :root {
            --primary: #064e3b;        /* Deep Green */
            --primary-dark: #022c22;   /* Darker Green for Sidebar */
            --accent: #d4af37;         /* Golden */
            --bg-soft: #f8fafc;
            --text-dark: #1e293b;
        }

        body { 
            background-color: var(--bg-soft); 
            font-family: 'Outfit', sans-serif; 
            overflow-x: hidden; 
            color: var(--text-dark);
        }
        
        /* --- Desktop Sidebar Design --- */
        .sidebar { 
            height: 100vh; 
            background-color: var(--primary-dark); 
            color: white; 
            position: fixed; 
            width: 260px; 
            z-index: 2000; 
            transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1); 
            left: 0; 
            top: 0; 
            overflow-y: auto;
            box-shadow: 4px 0 15px rgba(0,0,0,0.05);
            border-right: 2px solid rgba(212, 175, 55, 0.15); /* Subtle golden border */
        }

        /* Custom Scrollbar for Sidebar */
        .sidebar::-webkit-scrollbar { width: 5px; }
        .sidebar::-webkit-scrollbar-thumb { background: rgba(212, 175, 55, 0.3); border-radius: 10px; }

        .sidebar-header { 
            padding: 30px 20px; 
            text-align: center; 
            border-bottom: 1px solid rgba(212, 175, 55, 0.2); 
            margin-bottom: 20px;
        }
        
        .sidebar a { 
            color: rgba(255,255,255,0.8); 
            text-decoration: none; 
            padding: 14px 25px; 
            display: flex; 
            align-items: center;
            transition: 0.3s; 
            font-size: 15px; 
            border-left: 4px solid transparent;
            margin-bottom: 5px;
            font-weight: 500;
        }
        
        .sidebar a i { 
            width: 25px; 
            font-size: 18px; 
            text-align: center; 
            margin-right: 12px; 
            transition: 0.3s; 
        }

        /* Hover and Active Menu Styles */
        .sidebar a:hover, .sidebar a.active { 
            background: rgba(212, 175, 55, 0.1); 
            color: var(--accent); 
            border-left-color: var(--accent); 
        }
        .sidebar a:hover i, .sidebar a.active i { 
            transform: scale(1.15); 
            color: var(--accent); 
        }

        /* --- Main Content Area --- */
        .main-content { 
            margin-left: 260px; 
            padding: 30px; 
            transition: all 0.4s ease; 
            min-height: 100vh;
        }

        /* --- Mobile Responsive Design --- */
        .sidebar-overlay { 
            display: none; 
            position: fixed; 
            top: 0; left: 0; 
            width: 100%; height: 100%; 
            background: rgba(0,0,0,0.6); 
            backdrop-filter: blur(3px); /* Glassmorphism Overlay */
            z-index: 1500; 
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        .sidebar-overlay.active { 
            display: block; 
            opacity: 1; 
        }

        @media (max-width: 991px) {
            .sidebar { left: -280px; } /* Hidden initially on mobile */
            .sidebar.mobile-show { left: 0; } /* Show on toggle */
            .main-content { margin-left: 0; padding: 20px; width: 100%; }
        }
    </style>
</head>
<body>
    <div class="sidebar-overlay" id="sidebarOverlay"></div>