<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
date_default_timezone_set('Asia/Dhaka');

// ⚙️ FETCH GLOBAL SETTINGS
$settings = [];
if (isset($pdo)) {
    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) {}
}

$site_title = $settings['site_title'] ?? 'Ahmad Academy';
$site_tagline = $settings['site_tagline'] ?? 'Online Quranic Institute';
$contact_phone = $settings['contact_phone'] ?? '+8801815244716';

// 🟢 SMART LOGO PATH FIXER
// এটি অ্যাডমিন প্যানেল থেকে সেভ হওয়া যেকোনো পাথকে মেইন সাইটের জন্য সঠিক করে নিবে
$raw_logo = $settings['site_logo'] ?? '';
$site_logo = 'https://ahmadonlineacademy.com/assets/images/logo.png'; // Default Logo

if (!empty($raw_logo)) {
    if (strpos($raw_logo, 'http') === 0) {
        // যদি ডাটাবেসে ফুল URL থাকে
        $site_logo = $raw_logo;
    } else {
        // যদি ডাটাবেসে "../uploads/logo.png" বা "uploads/logo.png" থাকে
        // তাহলে "../" রিমুভ করে ডোমেইনের সাথে জোড়া লাগিয়ে দিবে
        $clean_path = ltrim(str_replace('../', '', $raw_logo), '/');
        $site_logo = 'https://ahmadonlineacademy.com/' . $clean_path;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($site_title) ?> | <?= htmlspecialchars($site_tagline) ?></title>
    
    <link rel="icon" type="image/png" href="<?= htmlspecialchars($site_logo) ?>">
    
    <link href="https://fonts.googleapis.com/css2?family=Amiri:wght@400;700&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">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    
    <style>
        :root { --primary: #064e3b; --primary-dark: #022c22; --accent: #d4af37; --white: #ffffff; --bg-soft: #f8fafc; }
        body { font-family: 'Outfit', sans-serif; color: #334155; overflow-x: hidden; background-color: var(--bg-soft); }
        a { text-decoration: none; transition: 0.3s; }
        
        .top-bar { background: var(--primary-dark); padding: 10px 0; color: rgba(255,255,255,0.9); font-size: 13px; border-bottom: 1px solid rgba(212, 175, 55, 0.2); }
        .namaz-times-top { color: var(--accent); font-weight: 700; letter-spacing: 0.5px; }
        .namaz-times-top span { margin: 0 5px; }
        
        .header-main { padding: 15px 0; background: var(--white); border-bottom: 3px solid var(--accent); }
        .header-flex-container { display: flex; align-items: center; justify-content: space-between; width: 100%; flex-wrap: wrap; }
        .ahmad-nav-list { list-style: none; margin: 0; padding: 0; display: flex; }
        .ahmad-nav-list .nav-item { margin: 0; position: relative; }
        .ahmad-nav-list .nav-link { font-weight: 600; transition: 0.3s; display: block; text-decoration: none; }

        @media (min-width: 992px) {
            .header-flex-container { flex-wrap: nowrap; }
            .logo-area { flex-shrink: 0; order: 1; }
            .desktop-nav-area { display: flex !important; flex-grow: 1; justify-content: center; order: 2; }
            .button-area { flex-shrink: 0; order: 3; }
            .ahmad-nav-list { flex-direction: row !important; align-items: center; gap: 8px; }
            .ahmad-nav-list .nav-link { color: var(--primary-dark) !important; padding: 10px 18px !important; font-size: 16px; border-radius: 8px; }
            .ahmad-nav-list .nav-link:hover, .ahmad-nav-list .nav-link.active { color: var(--primary-dark) !important; background: rgba(212, 175, 55, 0.15) !important; }
            .nav-item.dropdown:hover .custom-dropdown-menu { display: block; margin-top: 0; }
            .custom-dropdown-menu { display: none; position: absolute; background-color: var(--white); border-top: 3px solid var(--accent); border-radius: 0 0 12px 12px; padding: 10px 0; box-shadow: 0 15px 35px rgba(0,0,0,0.1); z-index: 1000; min-width: 220px; top: 100%; left: 0; }
            .custom-dropdown-item { display: block; color: var(--primary-dark) !important; padding: 10px 25px; transition: 0.3s; font-weight: 500; }
            .custom-dropdown-item:hover { background-color: var(--bg-soft) !important; color: var(--accent) !important; padding-left: 32px; }
        }

        @media (max-width: 991px) {
            .logo-area { order: 1; }
            .button-area { order: 2; }
            .desktop-nav-area { order: 3; display: none; width: 100%; background: var(--primary); border-radius: 12px; margin-top: 15px; padding: 10px 0; }
            .desktop-nav-area.show-menu { display: block !important; }
            .ahmad-nav-list { flex-direction: column !important; width: 100%; }
            .ahmad-nav-list .nav-link { color: var(--white) !important; text-align: center; padding: 15px !important; border-bottom: 1px solid rgba(255,255,255,0.05); }
            .ahmad-nav-list .nav-link:hover, .ahmad-nav-list .nav-link.active { color: var(--accent) !important; }
            .ahmad-nav-list .nav-item:last-child .nav-link { border-bottom: none; }
            .custom-dropdown-menu { display: none; background-color: #03362a !important; margin: 0 15px 10px 15px; padding: 5px 0; border-radius: 10px; text-align: center; }
            .custom-dropdown-menu.show-drop { display: block !important; }
            .custom-dropdown-item { display: block; color: rgba(255,255,255,0.85) !important; padding: 12px; border-bottom: 1px solid rgba(255,255,255,0.02); font-size: 14px; }
            .custom-dropdown-item:last-child { border-bottom: none; }
            .custom-dropdown-item:hover { background-color: var(--primary-dark) !important; color: var(--accent) !important; }
            
            .logo-area img { height: 45px !important; }
            .brand-text h1 { font-size: 20px !important; }
            .brand-text .tagline { font-size: 9px !important; }
            .custom-toggler { background: var(--primary); border: 1px solid var(--accent); padding: 6px 12px; color: white; border-radius: 5px; cursor: pointer; }

            .mobile-action-buttons { padding: 15px 20px; border-top: 1px solid rgba(255,255,255,0.05); display: flex; flex-direction: column; gap: 10px; }
            .mobile-btn { display: block; text-align: center; padding: 12px; border-radius: 8px; font-weight: 700; text-decoration: none; font-size: 15px; transition: 0.3s;}
            .mobile-btn-login { background: rgba(255,255,255,0.1); color: var(--white); border: 1px solid rgba(255,255,255,0.2); }
            .mobile-btn-login:hover { background: var(--white); color: var(--primary-dark); }
            .mobile-btn-admission { background: var(--accent); color: var(--primary-dark); border: none; }
        }
    </style>
</head>
<body>

    <div class="top-bar d-none d-md-block">
        <div class="container d-flex justify-content-between align-items-center">
            <div>
                <i class="far fa-clock me-1 text-warning"></i> BD Time: <span id="bd-clock" class="fw-bold"></span>
                <span class="mx-3 opacity-25">|</span>
                <i class="fas fa-phone-alt me-1 text-warning"></i> <?= htmlspecialchars($contact_phone) ?>
            </div>
            <div class="namaz-times-top">
                <span id="fajr">...</span> | <span id="dhuhr">...</span> | <span id="asr">...</span> | <span id="maghrib">...</span> | <span id="isha">...</span>
            </div>
            <div>
                <a href="login" class="text-white text-decoration-none me-3"><i class="fas fa-user-graduate me-1 text-warning"></i> Student Login</a>
                <a href="teacher/login" class="text-white text-decoration-none"><i class="fas fa-chalkboard-teacher me-1 text-warning"></i> Teacher Login</a>
            </div>
        </div>
    </div>

    <header class="header-main sticky-top shadow-sm">
        <div class="container">
            <div class="header-flex-container">
                
                <a href="https://ahmadonlineacademy.com/" class="text-decoration-none logo-area">
                    <div class="d-flex align-items-center">
                        <img src="<?= htmlspecialchars($site_logo) ?>" alt="Logo" style="height: 55px; max-width: 200px; object-fit: contain;" class="me-3">
                        <div class="brand-text">
                            <h1 class="m-0" style="color: var(--primary); font-size: 26px; font-weight: 800;"><?= htmlspecialchars($site_title) ?></h1>
                            <div class="tagline text-uppercase" style="color: var(--accent); font-size: 11px; letter-spacing: 2px; font-weight: 700;"><?= htmlspecialchars($site_tagline) ?></div>
                        </div>
                    </div>
                </a>

                <div class="desktop-nav-area" id="mobileMenuContent">
                    <ul class="ahmad-nav-list mx-auto">
                        <li class="nav-item"><a class="nav-link active" href="https://ahmadonlineacademy.com/">Home</a></li>
                        <li class="nav-item"><a class="nav-link" href="about-us">About Us</a></li>
                        <li class="nav-item dropdown position-relative">
                            <a class="nav-link" href="#" id="mobileDropBtn"> Teacher's Panel <i class="fas fa-caret-down ms-1" style="color: var(--accent);"></i></a>
                            <div class="custom-dropdown-menu" id="mobileDropMenu">
                                <a class="custom-dropdown-item" href="teachers"><i class="fas fa-user-tie me-2 text-warning"></i> Teacher's & Staff</a>
                                <a class="custom-dropdown-item" href="board"><i class="fas fa-users me-2 text-warning"></i> Board Members</a>
                                <a class="custom-dropdown-item" href="advisers"><i class="fas fa-user-shield me-2 text-warning"></i> Adviser Panel</a>
                            </div>
                        </li>
                        <li class="nav-item"><a class="nav-link" href="courses">Our Courses</a></li>
                        <li class="nav-item"><a class="nav-link" href="notices">Notices</a></li>
                        <li class="nav-item"><a class="nav-link" href="contact">Contact</a></li>
                    </ul>

                    <div class="mobile-action-buttons d-lg-none">
                        <a href="login" class="mobile-btn mobile-btn-login"><i class="fas fa-user-graduate me-2"></i> Student Login</a>
                        <a href="teacher/login" class="mobile-btn mobile-btn-login"><i class="fas fa-chalkboard-teacher me-2"></i> Teacher Login</a>
                        <a href="admission" class="mobile-btn mobile-btn-admission mt-2"><i class="fas fa-paper-plane me-2"></i> Admission Now</a>
                    </div>
                </div>

                <div class="d-flex align-items-center gap-3 button-area">
                    <a href="admission" class="btn d-none d-lg-block px-4 py-2 fw-bold shadow-sm" style="background: var(--accent); color: var(--primary-dark); border-radius: 50px;">
                        <i class="fas fa-paper-plane me-2"></i> Admission Now
                    </a>
                    <button class="custom-toggler d-lg-none" type="button" id="mobileMenuBtn"><i class="fas fa-bars"></i></button>
                </div>

            </div>
        </div>
    </header>

    <script>
        document.addEventListener("DOMContentLoaded", function() {
            const menuBtn = document.getElementById('mobileMenuBtn');
            const menuContent = document.getElementById('mobileMenuContent');
            if(menuBtn && menuContent) { menuBtn.addEventListener('click', function(e) { e.preventDefault(); menuContent.classList.toggle('show-menu'); }); }

            const dropBtn = document.getElementById('mobileDropBtn');
            const dropMenu = document.getElementById('mobileDropMenu');
            if(dropBtn && dropMenu) { dropBtn.addEventListener('click', function(e) { if (window.innerWidth < 992) { e.preventDefault(); dropMenu.classList.toggle('show-drop'); } }); }

            function updateClock() {
                const options = { timeZone: 'Asia/Dhaka', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true };
                const el = document.getElementById('bd-clock');
                if(el) el.textContent = new Date().toLocaleTimeString('en-US', options);
            }
            setInterval(updateClock, 1000); updateClock();

            async function getPrayerTimes() {
                try {
                    const res = await fetch('https://api.aladhan.com/v1/timingsByCity?city=Dhaka&country=Bangladesh&method=1');
                    const d = await res.json();
                    const t = d.data.timings;
                    const fmt = (time) => { let [h, m] = time.split(':'); let ap = h >= 12 ? 'pm' : 'am'; h = h % 12 || 12; return `${h}.${m}${ap}`; };
                    document.getElementById('fajr').innerText = `Fajr: ${fmt(t.Fajr)}`;
                    document.getElementById('dhuhr').innerText = `Dhuhr: ${fmt(t.Dhuhr)}`;
                    document.getElementById('asr').innerText = `Asr: ${fmt(t.Asr)}`;
                    document.getElementById('maghrib').innerText = `Maghrib: ${fmt(t.Maghrib)}`;
                    document.getElementById('isha').innerText = `Isha: ${fmt(t.Isha)}`;
                } catch (e) { console.log("Prayer time error"); }
            }
            getPrayerTimes();
        });
    </script>