<script defer src="<?= $ruta ?? '' ?>js/sidebarmenu.js"></script>


<?php
require_once ($ruta ?? '') . 'class/c_login.php';
require_once ($ruta ?? '') . 'include/bd_pdo.php';

$user = Login::get_user();

$user->access();
$pagina = substr(basename($_SERVER['PHP_SELF']), 0, -4);
if (!in_array($pagina, ["main", "test"]) && !$user->acceso) {
    header('Location: main.php?error=1');
    exit;
}

$grupos = $user->admin ? queryAll("SELECT * FROM GRUPO ORDER BY grupo_nombre") : $db->query("SELECT * FROM GRUPO WHERE grupo_id IN (SELECT grupo_id FROM PERMISO_VIEW WHERE id = :id) ORDER BY grupo_nombre", array(":id" => $user->user['id']));

function html_header($title, $header = null)
{
    global $grupos, $user, $db, $ruta;
    ?>
    <aside id="sidebar" class="bg-light defaultShadow d-flex flex-column p-4">
        <div class="d-flex align-items-center mb-5">
            <div class="logotipo"><a href="https://lasalle.mx/" target="_blank"><img src="<?= $ruta ?? '' ?>imagenes/logo_lasalle.png"></a>
            </div>
            <div class="flex-grow-1 d-flex justify-content-end">
                <nav class="navbar navbar-expand d-none d-flex">
                    <ul class="navbar-nav">

                    </ul>
                </nav>
                <div class="d-flex mainMenu justify-content-center align-items-center">
                    <div class="max-h iconSesion">
                        <a href="salir.php" class="iconOff max-h pl-3 d-flex justify-content-start align-items-center"><i
                                class="ing-salir"></i></a>
                    </div>
                    <div class="max-h">
                        <div class="bg-primary rounded-circle pointer max-h max-w d-flex justify-content-center align-items-center"
                            id="dismiss">
                            <span class="text-white iconMenuSidebar ing-cancelar"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="accordion px-2" id="accordionMenu">
            <p class="mb-0 mt-3 ml-4 pl-1">
                <a class="d-block side-menu" href="main.php">
                    <span class="ing-home"></span> Menú
                </a>
            </p>

            <?php
            if ($user->admin) { ?>
                <p class="mb-0 mt-3 ml-4 pl-1">
                    <a href="permisos.php" class="d-block side-menu">
                        <span class="ing-pass"></span> Permisos
                    </a>
                </p>
            <?php }
            foreach ($grupos as $key => $grupo) {
                ?>
                <p class="mb-0 mt-3">
                    <a class="d-block side-menu collapsed" data-toggle="collapse" href="#menu_<?= $key ?>" role="button"
                        aria-expanded="false">
                        <i class="ing-caret ing-fw mr-2"></i>
                        <span class="<?= $grupo['grupo_icon'] ?>"></span>
                        <?= ucfirst($grupo['grupo_nombre']) ?>
                    </a>
                </p>
                <div id="menu_<?= $key ?>" class="collapse" data-parent="#accordionMenu" style>
                    <ul class="fa-ul">
                        <?
                        foreach ($user->admin ? $db->query("SELECT * FROM PAGINA WHERE grupo_id = :grupo_id ORDER BY pagina_titulo", array(":grupo_id" => $grupo['grupo_id'])) : $db->query("SELECT * FROM PAGINA WHERE grupo_id = :grupo_id and pagina_id IN (SELECT pagina_id FROM PERMISO_VIEW WHERE id = :id) ORDER BY pagina_titulo", array(":grupo_id" => $grupo['grupo_id'], ":id" => $user->user['id']))
                            as $pagina):
                            ?>
                            <li class="mt-1">
                                <a href="<?= $pagina['pagina_ruta'] ?>.php">
                                    <?= $pagina['pagina_titulo'] ?>
                                </a>
                            </li>
                        <? endforeach; ?>
                    </ul>
                </div>
                <?php
            }
            ?>
        </div>
    </aside>
    <div class="overlay"></div>
    <header class="sticky-top bg-white">
        <div class="container marco menu d-flex align-items-center">
            <div class="logotipo">
                <a href="https://lasalle.mx/" target="_blank">
                    <img src="<?= $ruta ?? '' ?>imagenes/logo_lasalle.png">
                </a>
            </div>
            <div class="flex-grow-1 d-flex justify-content-end">
                <nav class="navbar navbar-expand-lg d-flex">
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
                        aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="collapse navbar-collapse" id="navbarNav">
                        <ul class="navbar-nav">
                            <!-- Add your navigation items here -->
                        </ul>
                    </div>
                </nav>
                <div class="d-flex mainMenu justify-content-center align-items-center">
                    <div class="max-h iconSesion">
                        <a href="salir.php" class="iconOff max-h pl-3 d-flex justify-content-start align-items-center">
                            <i class="ing-salir"></i>
                        </a>
                    </div>
                    <div class="max-h">
                        <span id="sidebarCollapse" style="font-size: 44px;"
                            class="ing-menu bg-white rounded-circle pointer max-w d-flex justify-content-center align-items-center"></span>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <div class="row bg-info mx-0 barra-gris d-flex flex-column">
        <?php
        if ($header != null) {
            ?>
            <div class="marco">
                <div class="col-sm-12">
                    <h2 class="text-muted">
                        <?= $header; ?>
                </div>
            </div>
        <?php } ?>
        <div class="marco">
            <div class="col-sm-12 py-3">
                <h2 class="text-uppercase">
                    <?= $title; ?>
                </h2>
            </div>
        </div>
    </div>
<? } ?>