<?php 
require_once __DIR__ . '/theme-init.php'; 

// Carrega as configurações globais de SEO
$globalSeoTitle = $dbThemeConfig['seo_title'] ?? ($dadosLoja['nome'] ?? 'Catálogo 44 - Sua Vitrine online');
$globalSeoDesc = $dbThemeConfig['seo_description'] ?? 'Bem-vindo à nossa loja online.';
$globalSeoKeywords = $dbThemeConfig['seo_keywords'] ?? 'loja, produtos, online';

$pageTitle = isset($pageTitle) ? $pageTitle : $globalSeoTitle;
$pageDesc = isset($pageDescription) ? $pageDescription : $globalSeoDesc;
$pageKeyw = isset($pageKeywords) ? $pageKeywords : $globalSeoKeywords;

$logoSiteFront = !empty($dadosLoja['logo']) ? '/adm/' . (strpos($dadosLoja['logo'], 'uploads/loja/') !== false ? $dadosLoja['logo'] : 'uploads/loja/' . $dadosLoja['logo']) : 'img/logo.png';

$pageImg = isset($pageImage) ? $pageImage : 'https://' . $_SERVER['HTTP_HOST'] . $logoSiteFront;
$pageUrl = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>
<!DOCTYPE html>
<html lang="pt-br">

<head>
    <meta charset="utf-8">
    <title><?= htmlspecialchars($pageTitle) ?></title>
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <meta content="<?= htmlspecialchars($pageKeyw) ?>" name="keywords">
    <meta content="<?= htmlspecialchars($pageDesc) ?>" name="description">
    
    <!-- Open Graph / Facebook -->
    <meta property="og:type" content="<?= isset($ogType) ? $ogType : 'website' ?>">
    <meta property="og:url" content="<?= htmlspecialchars($pageUrl) ?>">
    <meta property="og:title" content="<?= htmlspecialchars($pageTitle) ?>">
    <meta property="og:description" content="<?= htmlspecialchars($pageDesc) ?>">
    <meta property="og:image" content="<?= htmlspecialchars($pageImg) ?>">

    <!-- Twitter -->
    <meta property="twitter:card" content="summary_large_image">
    <meta property="twitter:url" content="<?= htmlspecialchars($pageUrl) ?>">
    <meta property="twitter:title" content="<?= htmlspecialchars($pageTitle) ?>">
    <meta property="twitter:description" content="<?= htmlspecialchars($pageDesc) ?>">
    <meta property="twitter:image" content="<?= htmlspecialchars($pageImg) ?>">

    <!-- Favicon -->
    <link href="img/favicon.ico?v=<?= time() ?>" rel="icon">

    <!-- Google Web Fonts -->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Jost:wght@500;600;700&family=Open+Sans:wght@400;600&display=swap" rel="stylesheet"> 

    <!-- Icon Font Stylesheet -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.1/font/bootstrap-icons.css" rel="stylesheet">

    <!-- Libraries Stylesheet -->
    <link href="lib/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet">
    <link href="lib/animate/animate.min.css" rel="stylesheet">
    <link href="lib/tempusdominus/css/tempusdominus-bootstrap-4.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/glightbox/dist/css/glightbox.min.css" />


    <!-- Customized Bootstrap Stylesheet -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Template Stylesheet -->
    <link href="css/style.css" rel="stylesheet">
    <link href="theme.php?v=<?= filemtime(__DIR__ . '/theme.php') ?>" rel="stylesheet">
<?= isset($extraHead) ? $extraHead : '' ?>
</head>

<body>
<!-- CARRINHO INCLUÍDO -->
<?php include 'carrinho_ui.php'; ?>
    
    <!-- Spinner Start -->

    <style>
    /* ── Mega Menu & Dropdown Estilos ───────────────────────── */
    @media (min-width: 992px) {
        /* Abre no hover */
        .navbar .dropdown:hover > .dropdown-menu {
            display: block;
            margin-top: 0;
            opacity: 1;
            visibility: visible;
        }

        /* Mega menu: ocupa largura total da navbar */
        .navbar .dropdown-mega {
            position: static !important;
        }
        .navbar .dropdown-mega > .dropdown-menu {
            width: 100%;
            left: 0;
            right: 0;
            top: 100%;
            border-radius: 0 0 8px 8px;
            background-color: var(--navbarcor, #003366) !important;
            padding: 28px 30px !important;
            box-shadow: 0 12px 40px rgba(0,0,0,0.25) !important;
        }

        /* Dropdown simples */
        .navbar .dropdown:not(.dropdown-mega) > .dropdown-menu {
            border-radius: 0 0 8px 8px;
            background-color: var(--navbarcor, #003366) !important;
            padding: 10px 0 !important;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2) !important;
            min-width: 220px;
        }
        .navbar .dropdown:not(.dropdown-mega) .dropdown-item {
            color: #ffffff !important;
            font-size: 14px;
            padding: 8px 20px;
            transition: 0.2s;
        }
        .navbar .dropdown:not(.dropdown-mega) .dropdown-item:hover {
            background-color: rgba(255,255,255,0.15) !important;
        }
    }

    /* Links dentro do mega menu */
    .dropdown-item-mega {
        color: #ffffff !important;
        text-decoration: none !important;
        font-size: 14px;
        display: block;
        padding: 4px 10px;
        border-radius: 4px;
        transition: background 0.2s;
        background-color: transparent !important;
    }
    .dropdown-item-mega:hover {
        color: #ffffff !important;
        text-decoration: none !important;
        background-color: rgba(255, 255, 255, 0.18) !important;
    }

    /* Título de coluna no mega menu */
    .mega-menu-title {
        color: #ffffff !important;
        font-size: 15px;
        font-weight: 800;
        margin-bottom: 10px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        border-bottom: 1px solid rgba(255,255,255,0.25) !important;
        padding-bottom: 7px !important;
        display: inline-block;
        width: 100%;
        cursor: default;
    }

    /* Seta do dropdown */
    .navbar .dropdown-toggle::after {
        color: var(--primary) !important;
    }
    </style>


    <!-- Topbar Start -->
    <div class="container-fluid bg-light ps-5 pe-0 d-none d-lg-block">
        <div class="row gx-0">
            <div class="col-md-6 text-center text-lg-start mb-2 mb-lg-0<?= empty($dbThemeConfig['texto_topbar']) ? ' d-none' : '' ?>">
                <div class="d-inline-flex align-items-center pulse" >  
                    <small class="py-2"><i class="far fa-clock text-primary me-2"></i><?= htmlspecialchars($dbThemeConfig['texto_topbar'] ?? '') ?> </small>
                </div>
            </div>
            <div class="col-md-6 text-center text-lg-end ms-auto">
                <div class="position-relative d-inline-flex align-items-center bg-primary text-white top-shape px-5">
                    <div class="me-3 pe-3 border-end py-2">
                        <p class="m-0"><i class="fa fa-envelope-open me-2"></i><?= htmlspecialchars($emailDisplayLoja ?? '') ?></p>
                     </div>
                    <div class="py-2">
                       <p class="m-0"><i class="fab fa-whatsapp me-2"></i><?= htmlspecialchars($whatsappDisplayLoja ?? '') ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Topbar End -->



    <div class="site-header-wrapper sticky-top w-100" style="top: -1px; z-index: 1020; transition: ease .5s;">
        <!-- Theme Switcher Top Bar REMOVED -->


        <!-- Navbar Start -->
        <nav class="navbar navbar-expand-lg bg-white navbar-light shadow-sm px-5 py-3 py-lg-0">
           <a href="index.php" class="navbar-brand p-0">
                <img src="<?= htmlspecialchars($logoSiteFront) ?>" width="auto" alt="Catalogo 44" class="site-logo-image" style="object-fit: contain; max-height: 60px;">
            </a>
          
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarCollapse">
                <div class="navbar-nav ms-auto py-0">  
                 <a href="index.php" class="nav-item nav-link">Home</a>
                 <a href="atacado.php" class="nav-item nav-link" style="color: #f97316; font-weight: 700;">Atacado</a>
                    <?php
                    // Inclui a lógica modular de geração do menu de categorias
                    include 'inc/menu-generativo.php';
                    ?>
                 <a href="contatos.php" class="nav-item nav-link">Contato</a>  
                 <a href="depoimento.php" class="nav-item nav-link">Depoimentos</a>  
                
                <!-- USER MENU -->
                <div class="nav-item dropdown ms-lg-3">
                    <?php if (isset($_SESSION['usuario_id'])): ?>
                        <a href="#" class="nav-link dropdown-toggle d-flex align-items-center justify-content-start" data-bs-toggle="dropdown">
                            <i class="fa fa-user-circle text-primary me-2 fs-4"></i>
                            <?php 
                                $nomeExibicao = 'Usuário';
                                $sessaoNome = $_SESSION['usuario_nome'] ?? $_SESSION['nome_completo'] ?? '';
                                if (!empty($sessaoNome)) {
                                    $nomeExibicao = explode(' ', trim($sessaoNome))[0];
                                }
                            ?>
                            <span>Olá, <?= htmlspecialchars($nomeExibicao) ?></span>
                        </a>
                        <div class="dropdown-menu dropdown-menu-end m-0">
                            <a href="meus-pedidos.php" class="dropdown-item">Meus Pedidos</a>
                            <a href="perfil.php" class="dropdown-item">Meu Perfil</a>
                            <hr class="dropdown-divider">
                            <a href="logout.php" class="dropdown-item text-danger">Sair</a>
                        </div>
                    <?php else: ?>
                        <a href="login.php" class="nav-item nav-link d-flex align-items-center">
                            <i class="fa fa-user text-primary me-2 fs-4"></i>
                            <span>Entrar</span>
                        </a>
                    <?php endif; ?>
                </div>

                <div class="nav-item d-flex align-items-center ms-lg-2">
                    <a href="#" class="nav-link" data-bs-toggle="modal" data-bs-target="#searchModal">
                        <i class="fa fa-search text-primary fs-4"></i>
                    </a>
                </div>
                </div>
            </div>
        </nav>
        <!-- Navbar End -->
    </div>


    <!-- Full Screen Search Start -->
    <div class="modal fade" id="searchModal" tabindex="-1">
        <div class="modal-dialog modal-fullscreen">
            <div class="modal-content" style="background: rgba(9, 30, 62, .7);">
                <div class="modal-header border-0">
                    <button type="button" class="btn bg-white btn-close" data-bs-dismiss="modal" aria-label="Fechar"></button>
                </div>
                <div class="modal-body d-flex align-items-center justify-content-center">
                    <form action="busca.php" method="GET" class="w-100 d-flex justify-content-center">
                        <div class="input-group" style="max-width: 600px;">
                            <input type="text" name="q" id="searchInput" class="form-control bg-transparent border-primary p-3 text-white"
                                   placeholder="Digite o nome do produto..." autocomplete="off"
                                   style="color: #fff !important; font-size: 1.1rem;" required>
                            <button type="submit" class="btn btn-primary px-4">
                                <i class="bi bi-search"></i> Buscar
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- Full Screen Search End -->


    <!-- Tema customizado através do admin: /adm/personalizar_cores.php -->
