<?php

use Source\Helpers\Check;
?>

<!-- Header Section Start From Here -->
<header class="header-wrapper" >
    <!-- Header Nav Start -->
    <h1 class="d-none"><?=SITENAME?></h1>
    <?php
    $local_banner = 1;
    require "component/rozer/banner/comp-banner-headbar.php";
    ?>    
    <div class="col-header"> 
        <!-- Header Nav End -->
        <div class="header-top  ptb-10px d-xl-block d-none " itemscope itemtype="https://schema.org/Organization">
            <div class="container">
                <div class="row">
                    <div class="col-md-2">
                        <div class="logo">
                            <a href="<?= SITE ?>"><img class="img-responsive" src="<?= LOGO ?>" alt="<?= SITENAME ?>" /></a>
                        </div>
                    </div>
                    <!--                    <div class="col-md-1 d-sm-none d-lg-block">
                                        </div>-->
                    <div class="col-md-6 align-self-center col-lg-push-8" >
                        <div class="header-right-element d-flex" itemscope itemtype="https://schema.org/WebSite">
                            <meta itemprop="title" content="Pesquisar produtos <?= SITENAME ?>"/>
                            <link itemprop="url" href="<?= SITE ?>"/>
                            <div class="search-element media-body" data-action="pesquisar" itemprop="potentialAction" itemscope itemtype="https://schema.org/SearchAction">

                                <form class="d-flex" action="#" method="POST">    
                                    <meta itemprop="target" content="<?= SITE ?>produtos?s={searchs}"/>
                                    <input itemprop="query-input" type="search" autocomplete="off" placeholder="Pesquise por produto, categoria ou marca " name="searchs" />
                                    <button type="submit"><i class="icon-magnifier"></i></button>
                                </form>
                                <div class="search-result"></div>
                            </div>
                        </div>
                    </div>


                    <!--Cart info Start -->
                    <div class="col-md-4 d-flex header-tools align-content-end">
                        <div class="cart-info  d-flex align-self-center"> 
                            <!--<div class=" d-fle align-center justify-content-center">-->  
                            <div class="costumer-link">
                                <?php if (LOOJA_MODO != 1): ?>  
                                    <div class="dropdown dropdown-account">  
                                        <div class="circle-user"><i class="icon-user"></i></div>                                        
                                        <div id="dropdown-user" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="user">Olá!<br>
                                            <?= ($USER ? $USER['user_name'] : 'Minha Conta' ) ?>
                                        </div>

                                        <ul class="dropdown-menu animation slideDownIn" aria-labelledby="dropdown-user">
                                            <?php if (!$USER): ?>
                                                <li><a href="#" data-target='#modal-login' data-toggle='modal' title="Faça seu Login" alt="Faça o seu Login" class="btn btn-sm btn-primary"><i class="fa fa-sign-in text-light"></i> ENTRAR </a></li>
                      
                                                <?php if (CONF_FB_CLIENT_SECRET): ?>
                                                <li class="dropdown-divider"></li>
                                                <li><a href="<?= SITE . "login-facebook" ?>" title="Faça seu Login com facebook" alt="Faça o seu Login com facebook" class="btn btn-sm btn-facebook"><i class="fa fa-facebook-square text-light"></i> FACEBOOK </a></li>
                                                 <?php endif; ?>    
                                                <li class="dropdown-header">Cliente novo? &nbsp; <a href="<?= BASECLIENTE . "cadastro" ?>">Cadastre-se</a></li>
                                            <?php endif; ?>
                                            <li><a href="<?= BASECLIENTE ?>" title="Minha Conta" alt="Minha conta"><i class="icon-pencil"></i> Minha conta</a></li>
                                            <li><a href="<?= BASECLIENTE . "pedidos" ?>" title="Meus pedidos" alt="Meus pedidos"><i class="icon-bag"></i> Meus pedidos</a></li>
                                            <li><a href="<?= BASECLIENTE . "wishlist" ?>" title="Meus Favoritos" alt="Meus Favoritos"><i class="icon-heart"></i> Meus favoritos</a></li>
                                            <?php if ($USER): 
                                                   $afiliado = Read(DB_AFILIADO,"user_id=:uid","uid={$USER['user_id']}")->getUnique();
                                                   ?>
                                            <?php if($user_afiliado): ?>
                                                <li><a href="#" onclick="navigator.clipboard.writeText(window.location.href+'<?=$link_afiliado?>'); alert('Copiado');" title="Copiar link" alt="Copiar link"><i class="icon-link"></i> Copiar Link de Afiliado </a></li>
                                                <?php endif; ?>
                                                <li><a href="<?= BASECLIENTE . "&logoff=true" ?>" ><i class="icon-logout"></i> Sair</a></li>
                                            <?php else: ?>
                                                <li><a href="<?= BASECLIENTE . "login" ?>" title="Entre em sua conta" alt="Entre em sua conta"><i class="icon-login"></i> Entre</a></li>
                                                <li><a href="<?= BASECLIENTE . "cadastro" ?>" title="Faça seu cadastro" alt="Faça seu cadastro"><i class="icon-logout"></i> Cadastre-se</a></li>                                 
                                            <?php endif; ?>
                                        </ul>                                        

                                    </div>
                                <?php endif; ?>
                                
                                <?php if (LOOJA_MODO == 7): ?>  
                                    <div class="dropdown dropdown-user">                                
                                        <?php if ($USER):
                                            $afiliado = Read(DB_AFILIADO,"user_id=:uid","uid={$USER['user_id']}")->getUnique();
                                            ?>
                                            <button type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="icon-user"></i> <?= $USER['user_name'] ?>  <i class="ion-ios-arrow-down"></i></button>
                                            <ul class="dropdown-menu animation slideDownIn" aria-labelledby="dropdownMenuButton">
                                                <li><a href="<?= BASECLIENTE . "login" ?>" title="Minha Conta" alt="Minha conta"><i class="icon-pencil"></i> Minha conta</a></li>
                                                <li><a href="<?= BASECLIENTE . "compras" ?>" title="Meus pedidos" alt="Meus pedidos"><i class="icon-bag"></i> Minhas compras</a></li>
                                                <?php if($afiliado): ?>
                                                <li><a href="#" onclick="navigator.clipboard.writeText(window.location.href+'&par=<?=$afiliado['key_afiliado']?>')" title="Copiar link" alt="Copiar link"><i class="icon-bag"></i> Copiar Link de Afiliado </a></li>
                                                <?php endif; ?>
                                                <li><a href="<?= BASECLIENTE . "&logoff=true" ?>"><i class="icon-logout"></i> Sair</a></li>
                                            </ul>
                                        <?php else: ?>
                                            <a href="<?= BASECLIENTE . "login" ?>" title="Faça seu Login" alt="Faça o seu Login" >Entre</a> ou <a href="<?= BASECLIENTE . "cadastro" ?>" title="Faça seu cadastro" alt="Faça seu cadastro">Cadastre-se</a>                                  
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </div> 



                            <?php if (LOOJA_MODO == 1): ?>    
                                <a href="#offcanvas-cart" class="bag offcanvas-toggle">
                                    <i class="icon-bag"></i>
                                    <label class="cart-count">0</label>
                                    <div class="">Minha Lista    <span class="total-carrinho">R$ 0.00</span></div>
                                </a>
                            <?php else: ?>
                                <a href="#offcanvas-wishlist" class="heart offcanvas-toggle">
                                    <i class="icon-heart"></i>
                                    <div class="">Meus Favoritos</div>
                                    <label class="heart-count">0</label>
                                </a>      
                            <?php if(CONF_B2B_STATUS && is_auth() || !CONF_B2B_STATUS): ?>
                                <a href="#offcanvas-cart" class="bag offcanvas-toggle">
                                    <i class="icon-bag"></i>
                                    <label class="cart-count">0</label>
                                    <div class="">Meu Carrinho
                                        <span class="total-carrinho">R$ 0.00</span></div>

                                </a>
                            <?php endif; ?>
                            <?php endif; ?>
                        </div> 
                    </div> 
                </div>
            </div>
            <!--Cart info End -->
        </div>
    </div>

    <!-- Header Nav End -->
    <div class="header-menu bg-casa-red sticky-nav d-xl-block d-none padding-0px">
        <div class="container">
            <div class="row">
                <div class="col-lg-3 custom-col-2">
                    <div class="header-menu-vertical">
                        <h4 class="menu-title">Departamentos</h4>
                        <?php
                        $Read->ExeRead(DB_SECAO, "WHERE secao_de='ecommerce'  AND secao_status=1 ORDER BY secao_ordem ASC");
                        if ($Read->getResult()):
                            $MenuCategory = $Read->getResult();

                            $navmenu = '<ul class="menu-content display-none">';
                            foreach ($MenuCategory as $secao):
                                //  var_dump($secao);
                                $Read->ExeRead(DB_CATEGORY, "WHERE category_parent=:pa ORDER BY category_title", "pa={$secao['secao_id']}");
                                $secao_icone = '';
                                if ($secao['secao_icone'] && CONF_LOJA_EXIBIR_DEP_MENU):
                                    $menuicone = Check::CheckPhoto($secao['secao_icone'], DIR_UPLOADS, true);
                                    $secao_icone = "<img class='menu-icon' src='{$menuicone}' alt='{$secao['secao_icone']}' >";
                                endif;
                                if (!$Read->getResult()):
                                    $navmenu .= '<li class="menu-item"> <a href="' . SITE . $secao['secao_name'] . '" title="' . $secao['secao_title'] . '" title="' . $secao['secao_title'] . '" alt="' . $secao['secao_title'] . '">' . $secao_icone . " " . $secao['secao_title'] . '</a></li>';
                                else:
                                    $navmenu .= '<li class="menu-item"> <a href="' . SITE . $secao['secao_name'] . '" title="' . $secao['secao_title'] . '" title="' . $secao['secao_title'] . '" alt="' . $secao['secao_title'] . '" >' . $secao_icone . " " . $secao['secao_title'] . ' <i class="ion-ios-arrow-right"></i></a> ';
                                    $count = 0;
                                    $submenu = '<ul class="sub-menu flex-wrap">';
                                    $subsubmenu = '<ul class="sub-menu sub-menu-2"><li><ul class="submenu-item">';
                                    foreach ($Read->getResult() as $category):

                                        $Read->ExeRead(DB_CATEGORY, "WHERE category_para=:ca AND category_parent=:pa ORDER BY category_title", "ca={$category['category_id']}&pa={$secao['secao_id']}");
                                        $result_subcategoria = $Read->getResult();
                                        if (!$result_subcategoria):
                                            $subsubmenu .= '<li class="menu-item"> <a href="' . SITE . $secao['secao_name'] . DIRECTORY_SEPARATOR . $category['category_name'] . '" title="' . $category['category_title'] . '"  alt="' . $category['category_title'] . '">' . $category['category_title'] . '</a></li>';
                                        //$submenu .= '<li >  <a href="' . SITE . $secao['secao_name'] . DIRECTORY_SEPARATOR . $category['category_name'] . '" title="' . $category['category_title'] . '"><span> <strong>' . $category['category_title'] . '</strong></span></a> ';
//                                           var_dump($category['category_name']);
                                        else:

                                            $submenu .= '<li>  <a href="' . SITE . $secao['secao_name'] . DIRECTORY_SEPARATOR . $category['category_name'] . '" title="' . $category['category_title'] . '"  alt="' . $category['category_title'] . '"><span> <strong>' . $category['category_title'] . '</strong></span></a>  <ul class="submenu-item">';
                                            $count2 = 0;
                                            foreach ($result_subcategoria as $subcategory):
                                                $submenu .= ' <li><a href="' . SITE . $secao['secao_name'] . DIRECTORY_SEPARATOR . $subcategory['category_name'] . '" title="' . $subcategory['category_title'] . '"  alt="' . $subcategory['category_title'] . '">' . $subcategory['category_title'] . '</a></li>';
                                                $count2++;
                                            endforeach;
                                            $submenu .= '</ul></li>';
                                        endif;

                                        $count++;
                                    endforeach;
                                    $image_secao = ($secao['secao_cover_menu'] ? '<div class="img-secao"><a href="' . SITE . $secao['secao_name'] . '" ><img src="' . Check::CheckPhoto($secao['secao_cover_menu'], DIR_UPLOADS, true) . '" class="img-fluid"/></a></div>' : '');
                                    $submenu .= $image_secao . '</ul>';
                                    $subsubmenu .= "</ul></li></ul>";
                                    $navmenu .= ($submenu ? $submenu : $subsubmenu);
//                                        var_dump($Read->getResult());
//                                        if (!$result_subcategoria):
//                                            $navmenu .= '<li class="menu-item"><a href="' . SITE . $secao['secao_name'] . DIRECTORY_SEPARATOR . $category['category_name'] . '" title="' . $category['category_title'] . '">' . $category['category_title'] . '</a></li>';
//                                        else:
//                                            $navmenu .= '<ul class="sub-menu flex-wrap">' . $submenu . "</ul></li>";
//                                        endif;
                                    $navmenu .= "</li>";
                                endif;
                            endforeach;

                            $navmenu .= "</ul>";
                            echo $navmenu;

                        endif;
                        ?>
                        <!-- menu content -->
                    </div>
                    <!-- header menu vertical -->
                </div>
                <div class="col-lg-9 custom-col-2 ">
                    <div class="header-horizontal-menu">
                        <ul class="menu-content">                                                  
                            <?php
                            $Read->ExeRead(DB_LINKS, 'WHERE link_ativa=1 AND link_menu IS NULL ORDER BY link_position ASC');
                            if ($Read->getResult()):
                                //lista os itens do menu
                                foreach ($Read->getResult() as $menu):
                                    $require_nav = '';
                                    parse_str($menu['link_href'], $shub);
                                    $menu_href = '';
                                    $categoria_href = '';
                                    $menu_icone = '';
                                    if ($menu['link_cover'] && CONF_LOJA_EXIBIR_MENU_ICONE):
//                                        $menuicone = Check::CheckPhoto($menu['link_cover'], DIR_UPLOADS, true);
                                        $menuicone = image($menu['link_cover'], 120);
                                        $menu_icone = "<img  class='menu-icon' src='{$menuicone}' alt='{$menu['link_title']}' >";
                                    endif;

                                    if (!empty($shub['pagina'])) {
                                        $mpage = Read(DB_PAGE, 'page_id=:id', "id={$shub['pagina']}", "page_link,page_title")->getUnique();
                                        $menu_href .= ($mpage ? $mpage['page_link'] . "/" : '');
                                    }
                                    if (!empty($shub['grupoproduto'])) {
                                        $grupo = (new Source\Models\ProdutoGrupo())->find("id=:id AND status=1", "id={$shub['grupoproduto']}", "name")->fetch();
                                        $menu_href .= ($grupo ? "grupo/" . $grupo->name . "/" : '');
                                    }
                                    if (!empty($shub['secao'])) {
                                        $msecao = Read(DB_SECAO, 'secao_id=:id', "id={$shub['secao']}")->getUnique();
                                        $menu_href .= ($msecao ? $msecao['secao_name'] . "/" : '');
                                        $MenuCategory = [];
                                        $MenuCategory[] = $msecao;

                                        $require_nav = "component/rozer/category/menu_category.php";
                                    }
                                    if (!empty($shub['categoria'])) {
                                        $mcategory = Read(DB_CATEGORY, 'category_id=:id', "id={$shub['categoria']}", "category_name, category_title")->getUnique();
                                        $categoria_href = ($mcategory ? $mcategory['category_name'] . "/" : '');
                                    }
                                    if (!empty($shub['subcategoria'])) {
                                        $msubcategory = Read(DB_CATEGORY, 'category_id=:id', "id={$shub['subcategoria']}", "category_name, category_title")->getUnique();
                                        $categoria_href = ($msubcategory ? $msubcategory['category_name'] . "/" : '');
                                    }
                                    $menu_href .= $categoria_href;



                                    //agora verifica se existe submenus 
                                    $Read->ExeRead(DB_LINKS, 'WHERE link_ativa=1 AND link_menu=:id', "id={$menu['link_id']}");
                                    $DLINKS = $Read->getResult();
//                                      
                                    if ($DLINKS && !$MenuCategory):
                                        //lista os submenus
                                        echo "<li class='menu-dropdown ". str_slug($menu['link_title'])."'><a href='#' alt='{$menu['link_title']}' title='{$menu['link_title']}'> {$menu_icone} {$menu['link_title']} <i class='ion-ios-arrow-down'></i> </a>\n<ul class='main-sub-menu'>\n";
                                        foreach ($DLINKS as $submenu):
                                            parse_str($submenu['link_href'], $subshub);
                                            $menu_href = '';
                                            $categoria_href = '';
                                            if (!empty($shub['pagina'])) {
                                                $mpage = Read(DB_PAGE, 'page_id=:id', "id={$subshub['pagina']}", "page_link,page_title")->getUnique();
                                                $menu_href .= ($mpage ? $mpage['page_link'] . "/" : '');
                                            }
                                            if (!empty($shub['grupoproduto'])) {
                                                $grupo = (new Source\Models\ProdutoGrupo())->find("id=:id AND status=1", "id={$shub['grupoproduto']}", "name")->fetch();
                                                $menu_href .= ($grupo ? "grupo/" . $grupo->name . "/" : '');
                                            }
                                            if (!empty($shub['secao'])) {
                                                $msecao = Read(DB_SECAO, 'secao_id=:id', "id={$subshub['secao']}", "secao_name,secao_title")->getUnique();
                                                $menu_href .= ($msecao ? $msecao['secao_name'] . "/" : '');
                                            }
                                            if (!empty($shub['categoria'])) {
                                                $mcategory = Read(DB_CATEGORY, 'category_id=:id', "id={$subshub['categoria']}", "category_name, category_title")->getUnique();
                                                $categoria_href = ($mcategory ? $mcategory['category_name'] . "/" : '');
                                            }
                                            if (!empty($shub['subcategoria'])) {
                                                $msubcategory = Read(DB_CATEGORY, 'category_id=:id', "id={$subshub['subcategoria']}", "category_name, category_title")->getUnique();
                                                $categoria_href = ($msubcategory ? $msubcategory['category_name'] . "/" : '');
                                            }
                                            $menu_href .= $categoria_href;

                                            $sublinkcerto = (strripos($submenu['link_href'], 'http') === false ? SITE . $menu_href : $submenu['link_href']);
                                            echo "<li class='" . ($seo->getLocal()[0] == $menu_href ? 'active' : null) . "'><a href='{$sublinkcerto}' title='{$submenu['link_title']}' alt='{$submenu['link_title']}'    target='{$submenu['link_open']}'>{$submenu['link_title']}</a></li>\n";
                                        endforeach;
                                        echo '</ul></li>';
                                    else:
                                        //não existindo submenus exibe apenas o link principal
                                        $linkcerto = (strripos($menu['link_href'], 'http') === false ? SITE . $menu_href : $menu['link_href']);
                                    
                                        echo "<li class=' ".($require_nav?'menu-dropdown ' .str_slug($menu['link_title']):''). ($router == $menu_href ? 'active' : null) . "'><a href='{$linkcerto}' title='{$menu['link_title']}'  alt='{$menu['link_title']}'  target='{$menu['link_open']}'>{$menu_icone} {$menu['link_title']} " .($require_nav?'<i class="ion-ios-arrow-down"></i>':'')."</a> ";
                                        if($require_nav):
                                        require $require_nav;
                                        endif;
                                        echo "</li>\n";

                                    endif;
                                endforeach;
                            endif;
                            ?>
                        </ul>
                    </div>
                    <!-- header horizontal menu -->
                    <div class="intro-text-shipping text-right">
                        <!--<div class="free-ship">Frete grátis para Goiás</div>-->
                    </div>
                </div>
            </div>
            <!-- row -->
        </div>
        <!-- container -->
    </div>
    <!-- header menu -->
</div>
</header>
<!-- Header Section End Here -->   

