<?php
/**
 * Theme: Demo 01
 * Header Component
 * 
 * Uses inline onclick handlers to guarantee functionality regardless of app.js state.
 */

// Load Menu Model to get dynamic menu items
require_once __DIR__ . '/../../models/Menu.php';
$menuModel = new Menu();
$menuItems = $menuModel->getActive('header');

// Fallback to default menu if database is empty
if (empty($menuItems)) {
    $menuItems = [
        ['title' => 'Início', 'url' => 'home'],
        ['title' => 'Notícias', 'url' => 'noticias'],
        ['title' => 'Programação', 'url' => 'programacao'],
        ['title' => 'Promoções', 'url' => 'promocoes'],
        ['title' => 'Equipe', 'url' => 'equipe'],
        ['title' => 'Contato', 'url' => 'contato'],
    ];
}
?>
<header id="header"
    class="fixed top-0 left-0 w-full z-[9000] transition-all duration-300 bg-white/80 backdrop-blur-md border-b border-gray-100 h-[90px]">
    <div class="container mx-auto px-4 h-full flex items-center justify-between">

        <!-- Logo -->
        <a href="home"
            class="flex-shrink-0 relative z-[9001] transition-transform hover:scale-105 w-[180px] flex items-center">
            <?php if (!empty($siteSettings['logo'])): ?>
                <img src="<?php echo SITE_URL . '/uploads/' . $siteSettings['logo']; ?>"
                    alt="<?php echo htmlspecialchars($siteName); ?>" class="h-12 w-auto object-contain">
            <?php else: ?>
                <span class="text-2xl font-bold text-primary font-display tracking-tight">
                    <?php echo htmlspecialchars($siteName); ?>
                </span>
            <?php endif; ?>
        </a>

        <!-- Desktop Navigation - Dynamic Menu -->
        <nav id="mainNav"
            class="hidden md:flex items-center space-x-1 bg-white/50 px-2 py-1 rounded-full border border-gray-100 shadow-sm backdrop-blur-sm">
            <?php foreach ($menuItems as $item):
                // Limpar URL (remover barra inicial se houver)
                $itemUrl = ltrim($item['url'], '/');
                $isActive = ($page === $itemUrl) || ($itemUrl === '/' && $page === 'home');
                ?>
                <a href="<?php echo htmlspecialchars($itemUrl); ?>"
                    class="nav-link px-5 py-2.5 rounded-full text-sm font-semibold hover:bg-primary/10 transition-all <?php echo $isActive ? 'text-primary bg-primary/5 active' : 'text-text-muted'; ?>">
                    <?php echo htmlspecialchars($item['title']); ?>
                </a>
            <?php endforeach; ?>
        </nav>

        <!-- Right Actions -->
        <div class="hidden md:flex items-center space-x-3">
            <!-- Social Icons (if needed) -->
            <?php if (!empty($siteSettings['whatsapp'])): ?>
                <a href="https://wa.me/<?php echo preg_replace('/[^0-9]/', '', $siteSettings['whatsapp']); ?>"
                    target="_blank"
                    class="w-10 h-10 rounded-full flex items-center justify-center text-white bg-green-500 hover:bg-green-600 transition-colors shadow-md hover:shadow-lg">
                    <i class="fab fa-whatsapp text-xl"></i>
                </a>
            <?php endif; ?>

            <!-- Search Toggle (Visual) -->
            <button
                class="w-10 h-10 rounded-full flex items-center justify-center text-text-muted hover:text-primary hover:bg-gray-100 transition-colors">
                <span class="material-icons-round">search</span>
            </button>
        </div>

        <!-- Mobile Menu Button with Inline Handler -->
        <button id="themeMobileMenuBtn" onclick="toggleThemeMenu(event)"
            class="md:hidden relative z-[9002] p-2 text-text-main hover:text-primary transition-colors focus:outline-none">
            <i class="fas fa-bars text-2xl" id="menuIcon"></i>
        </button>
    </div>

    <!-- Mobile Menu Overlay -->
    <div id="menuOverlay" onclick="closeThemeMenu()"
        class="fixed inset-0 bg-black/60 z-[9998] hidden transition-opacity duration-300 backdrop-blur-sm">
    </div>

    <!-- Mobile Navigation Drawer -->
    <div id="mobileNav"
        class="fixed top-0 right-0 w-[300px] h-full bg-white z-[9999] transform translate-x-full transition-transform duration-300 shadow-2xl flex flex-col md:hidden">

        <!-- Drawer Header -->
        <div class="flex items-center justify-between p-5 border-b border-gray-100 bg-gray-50/50">
            <!-- Logo inside Drawer -->
            <div class="w-[140px]">
                <?php if (!empty($siteSettings['logo'])): ?>
                    <img src="<?php echo SITE_URL . '/uploads/' . $siteSettings['logo']; ?>"
                        alt="<?php echo htmlspecialchars($siteName); ?>" class="max-h-10 w-auto object-contain">
                <?php else: ?>
                    <span class="text-xl font-bold text-primary font-display tracking-tight">
                        <?php echo htmlspecialchars($siteName); ?>
                    </span>
                <?php endif; ?>
            </div>

            <!-- Close Button -->
            <button onclick="closeThemeMenu()"
                class="w-10 h-10 flex items-center justify-center rounded-full bg-gray-100 text-gray-500 hover:bg-red-50 hover:text-red-500 transition-colors">
                <i class="fas fa-times text-lg"></i>
            </button>
        </div>

        <!-- Scrollable Content -->
        <div class="flex-1 overflow-y-auto py-4 px-6 space-y-1">
            <?php foreach ($menuItems as $item):
                $itemUrl = ltrim($item['url'], '/');
                $isActive = ($page === $itemUrl) || ($itemUrl === '/' && $page === 'home');
                ?>
                <a href="<?php echo htmlspecialchars($itemUrl); ?>" onclick="closeThemeMenu()"
                    class="block px-4 py-3 rounded-xl text-base font-semibold transition-all <?php echo $isActive ? 'bg-primary/10 text-primary' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?>">
                    <?php echo htmlspecialchars($item['title']); ?>
                </a>
            <?php endforeach; ?>
        </div>

        <!-- Drawer Footer -->
        <div class="p-6 border-t border-gray-100 bg-gray-50/50">
            <?php if (!empty($siteSettings['whatsapp'])): ?>
                <a href="https://wa.me/<?php echo preg_replace('/[^0-9]/', '', $siteSettings['whatsapp']); ?>"
                    target="_blank"
                    class="flex items-center justify-center gap-2 w-full py-3.5 bg-[#25D366] text-white rounded-xl font-bold shadow-lg shadow-green-200 hover:bg-[#128C7E] hover:shadow-xl transition-all">
                    <i class="fab fa-whatsapp text-lg"></i>
                    <span>Fale Conosco</span>
                </a>
            <?php endif; ?>

            <div class="flex justify-center gap-4 mt-6">
                <!-- Social Links Placeholder if needed -->
            </div>
        </div>
    </div>
</header>
<div class="h-[90px]"></div> <!-- Spacer -->

<script>
    // Robust Menu Functions
    function openThemeMenu(event) {
        if (event) event.preventDefault();
        const nav = document.getElementById('mobileNav');
        const overlay = document.getElementById('menuOverlay');
        
        if (nav && overlay) {
            nav.classList.remove('translate-x-full');
            overlay.classList.remove('hidden');
            // Small delay to allow display:block to apply before opacity transition if we were doing fade
            // But here we rely on hidden class removal.
            document.body.style.overflow = 'hidden'; // Prevent background scrolling
        }
    }

    function closeThemeMenu() {
        const nav = document.getElementById('mobileNav');
        const overlay = document.getElementById('menuOverlay');
        
        if (nav && overlay) {
            nav.classList.add('translate-x-full');
            overlay.classList.add('hidden');
   document.body.style.overflow = ''; // Restore scrolling
        }
    }
    
    // Toggle Function for the Hamburger Button
    function toggleThemeMenu(event) {
        if (event) event.preventDefault();
        const nav = document.getElementById('mobileNav');
        if (nav.classList.contains('translate-x-full')) {
            openThemeMenu(event);
        } else {
            closeThemeMenu();
        }
    }
</script>