<?php
header('Content-Type: text/html; charset=utf-8');
session_start();

if (!isset($_SESSION['usuario'])) {
    header("Location: rf_acceso.php");
    exit;
}

$tipo = $_SESSION['tipo'];
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Menú Principal</title>
    <link rel="icon" href="Iconos/rf_icono_vineta.png" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
/* ======== RESETEO BÁSICO ======== */
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Helvetica', sans-serif;
    background-color: #f3f4f8;
    color: #333;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ======== BARRA SUPERIOR ======== */



/* ======== BARRA SUPERIOR ======== */
.topbar {
    width: 100%;
    background-color: #1D3C7D;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center; /* EL SALUDO QUEDA CENTRADO */
    padding: 10px 30px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    position: fixed;
    top: 0;
    z-index: 1000;
    height: 50px;
}

/* Título StockTools a la IZQUIERDA */
.topbar .title {
    position: absolute;
    left: 20px;
    font-size: 20px;
}

/* Botón cerrar sesión a la DERECHA */
.topbar .logout {
    position: absolute;
    right: 20px;
}

.topbar .logout button {
    display: flex;
    align-items: center;
    gap: 6px;
    background: #bd0909;
    /* background: linear-gradient(to left, rgb(255,120,120), rgb(150,30,30)); */
    border: none;
    padding: 6px 14px;
    border-radius: 6px;
    color: white;
    font-size: 13px;
    cursor: pointer;
}

.topbar .logout button:hover {
    background-color: red;
}

.topbar .logout img {
    width: 30px;
    height: 30px;
}

.user-info {
    font-size: 16px;
    font-family: 'Helvetica', sans-serif;
    font-weight: 600; /* intermedio entre 500 y 600 */
    letter-spacing: 0.9px; /* texto un poco más "aireado" */
}



/* ======== CONTENEDOR PRINCIPAL ======== */
.main-container {
    display: flex;
    height: 100%;
    margin-top: 60px;
}

/* ======== BARRA LATERAL ======== */
.sidebar {
    width: 300px;
    background-color: #243B72;
    color: white;
    display: flex;
    flex-direction: column;
    padding-top: 20px;
    height: calc(100vh - 60px);
    position: fixed;
    top: 60px;
    left: 0;
    overflow-y: auto;
    box-shadow: 2px 0 6px rgba(0,0,0,0.15);
    text-align: center;
    font-size: 20px;
}

.sidebar h1 { margin-bottom: 8px; font-size: 16px; }
.sidebar img.logo { width: 30px !important; height: 30px !important; object-fit: contain; margin: 0 auto 12px; }

.menu-section { margin-top: 10px; padding: 0 0 20px; }

.menu-section button {
    display: flex; align-items: center; gap: 10px;
    background: none; border: none; color: white;
    font-size: 15px; text-align: left; padding: 12px 10px;
    cursor: pointer; width: 100%; transition: background 0.3s;
}
.menu-section button:hover { background-color: #3959A4; }
.menu-section img { width: 22px; height: 22px; }





.logout-container {
    display: flex;
    justify-content: center;  /* Centra el botón horizontalmente */
    margin-top: 150px;         /* Ajusta para bajar el botón */
}







/* ======= CONTENIDO ======= */
.content { flex: 1; margin-left: 250px; padding: 30px; overflow-y: auto; }
.content-box { background-color: white; border-radius: 10px; padding: 30px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); text-align: center; }
.fondo-leyenda { background-color: #ffcc00; text-align: center; padding: 5px; font-size: 13px; font-weight: bold; color: #333; }

@media (max-width: 768px) {
    .sidebar { width: 200px; }
    .menu-section button { font-size: 13px; }
    .content { margin-left: 200px; padding: 20px; }
}

/* ======= SUBMENÚ (robusto y animado) ======= */
.submenu-container { margin: 10px; position: relative; }

.submenu-btn {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    font-family: 'Helvetica', sans-serif;
    font-weight: 600; /* intermedio entre 500 y 600 */
    letter-spacing: 0.9px; /* texto un poco más "aireado" */
    color: white;
    padding: 12px 15px;
    background: linear-gradient(to left, rgb(0,255,255), rgb(31,78,121));
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: transform .18s ease, box-shadow .18s ease;
    width: 100%;
}

/* Stexto de opciones del menu principales */
.submenu-text {
    font-size: 15px; /* Ajusta aquí */
     font-family: 'Helvetica', sans-serif;
    font-weight: 200; /* intermedio entre 500 y 600 */
    letter-spacing: 0.9px; /* texto un poco más "aireado" */
    
}

/* texto de opciones del submenu */



.submenu-btn .icon { width: 65px; height: 65px; margin-right: 4px; }
.submenu-btn .arrow { margin-left: 8px; transition: transform .25s ease; }

/* SUBMENU: no usar display:none para permitir transiciones CSS */
.submenu {
    max-height: 0;
    opacity: 0;
    transform: translateY(-6px);
    overflow: hidden;
    transition: max-height .28s cubic-bezier(.2,.9,.2,1), opacity .28s ease, transform .28s ease;
    background: linear-gradient(to left, rgb(0,255,255), rgb(31,78,121));
    margin-top: 8px;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.12);
}

/* cuando está abierto */
.submenu.open {
    opacity: 1;
    transform: translateY(0);
    max-height: 600px; /* suficiente para varios items */
}

/* items */
.submenu button {
    background: none;
    border: none;
    padding: 10px 35px;
    text-align: left;
    font-size: 14px;
    font-family: 'Helvetica', sans-serif;
    font-weight: normal; /* intermedio entre 500 y 600 */
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background .18s ease, padding-left .18s ease, transform .18s ease;
}
.submenu button:hover {
    background: rgba(255,255,255,0.12);
    padding-right: 30px;
    transform: translateX(10px);
}
.submenu .dot {
    width: 6px;
    height: 6px;
    background: #fff;
    border-radius: 50%;
    display:inline-block;
    
    
        font-size: 14px;
    font-family: 'Helvetica', sans-serif;
    font-weight: 600; /* intermedio entre 500 y 600 */
    letter-spacing: 0.9px; /* texto un poco más "aireado" */
    
    
}

/* when arrow should rotate (aria-expanded handled in JS) */
.submenu-btn[aria-expanded="true"] .arrow { transform: rotate(180deg); }

</style>
</head>

<body>

<div class="fondo-leyenda">Sistema en Modo Pruebas - @Sergio Bravo contacto +56 9 46237231</div>


<div class="topbar">
    <div class="title">StockTools</div>

    <div class="user-info">
        Bienvenido, <?php echo htmlspecialchars($_SESSION['usuario']); ?>
    </div>

    <div class="logout">
        <button onclick="location.href='st_logout.php'">
            <img src="Iconos/st_icono_salir.png">
            Cerrar Sesión
        </button>
    </div>
</div>




<div class="main-container">
    <div class="sidebar">
        <h1>Menú Principal</h1>
        <img src="Iconos/icono_menu1.png" alt="Logo" class="logo">

        <div class="menu-section">
            <?php if ($tipo === '0'): ?>

            <!-- SUBMENÚ 1 -->
            <div class="submenu-container">
                <button class="submenu-btn" type="button" aria-expanded="false">
                    <span style="display:flex; align-items:center;">
                        <img src="Iconos/st_icono_mbuscar.png" class="icon" alt="">
                        <span class="submenu-text">Ver Herramientas y Equipos</span>
                    </span>
                    <span class="arrow">&#9662;</span>
                </button>

                <div class="submenu" aria-hidden="true">
                    <button onclick="location.href='conf_usuarios.php'"><span class="dot"></span> Buscar</button>
                    <button onclick="location.href='st_menuppl.php?form=st_maestro'"><span class="dot"></span> Ver Todos Nueva forma</button>
                    <button onclick="location.href='st_maestro.php'"><span class="dot"></span> Ver Todos</button>
                    <button onclick="location.href='conf_restricciones.php'"><span class="dot"></span> Arriendos / Estados</button>
                    <button onclick="location.href='conf_restricciones.php'"><span class="dot"></span> RF</button>
                </div>
            </div>

            <!-- SUBMENÚ 2 -->
            <div class="submenu-container">
                <button class="submenu-btn" type="button" aria-expanded="false">
                    <span style="display:flex; align-items:center;">
                        <img src="Iconos/st_icono_mprestamo.png" class="icon" alt="">
                        <span class="submenu-text">Préstamos</span>
                    </span>
                    <span class="arrow">&#9662;</span>
                </button>

                <div class="submenu" aria-hidden="true">
                    <button onclick="location.href='conf_usuarios.php'"><span class="dot"></span> Entregar Préstamo</button>
                    <button onclick="location.href='conf_clases.php'"><span class="dot"></span> Ver Herr./Eq. en Préstamo</button>
                    <button onclick="location.href='conf_restricciones.php'"><span class="dot"></span> Devolución</button>
                    <button onclick="location.href='conf_proveedores.php'"><span class="dot"></span> Pendientes</button>
                </div>
            </div>

            <!-- SUBMENÚ 3 -->
            <div class="submenu-container">
                <button class="submenu-btn" type="button" aria-expanded="false">
                    <span style="display:flex; align-items:center;">
                        <img src="Iconos/st_icono_mprestasub.png" class="icon" alt="">
                        <span class="submenu-text">Préstamos Sub Contratos</span>
                    </span>
                    <span class="arrow">&#9662;</span>
                </button>

                <div class="submenu" aria-hidden="true">
                    <button><span class="dot"></span> Entrega</button>
                    <button><span class="dot"></span> Devolución</button>
                    <button><span class="dot"></span> Entrega RF</button>
                    <button><span class="dot"></span> Devolución RF</button>
                </div>
            </div>

            <!-- SUBMENÚ 4 -->
            <div class="submenu-container">
                <button class="submenu-btn" type="button" aria-expanded="false">
                    <span style="display:flex; align-items:center;">
                        <img src="Iconos/st_icono_mconfig.png" class="icon" alt="">
                        <span class="submenu-text">Configuraciones</span>
                    </span>
                    <span class="arrow">&#9662;</span>
                </button>

                <div class="submenu" aria-hidden="true">
                    <button><span class="dot"></span> Herramientas / Equipos</button>
                    <button><span class="dot"></span> Usuarios</button>
                    <button><span class="dot"></span> Restricciones uso por Clase</button>
                    <button><span class="dot"></span> Proveedores</button>
                </div>
            </div>

            <?php endif; ?>
        </div>



    </div>

    <!-- CONTENIDO CENTRAL -->



<div class="content">
    <div class="content-box">

        <?php
        // Cargar contenido dinámico desde parámetros GET
        if (isset($_GET['form'])) {
            $archivo = $_GET['form'] . ".php";

            if (file_exists($archivo)) {
                include($archivo);
            } else {
                echo "<p>Error: No se encuentra el formulario solicitado.</p>";
            }
        } else {
            // Pantalla por defecto
        ?>
            <h2 style="margin-bottom:10px;font-size:60px">¡Bienvenido! </h2>
        <!--    <p style="font-size:15px;">Selecciona una opción del menú para comenzar.</p> -->
            <img src="Iconos/st_logo.png" style="width: 800px; margin-top:20px;" alt="logo">
            <p style="font-size:10px; color:#555;">
                CC LOGITIC — Todos los Derechos Reservados
                <a href="https://www.logitic.cl" target="_blank">www.logitic.cl</a>
            </p>
        <?php } ?>

    </div>
</div>






</div>

<script>
(function () {
    const CLOSE_DELAY = 220; // ms
    const timers = new WeakMap();

    function closeAllExcept(exceptSubmenu) {
        document.querySelectorAll('.submenu').forEach(s => {
            if (s !== exceptSubmenu && s.classList.contains('open')) {
                s.classList.remove('open');
                s.previousElementSibling?.setAttribute('aria-expanded','false');
                const arrow = s.previousElementSibling?.querySelector('.arrow');
                if (arrow) arrow.style.transform = 'rotate(0deg)';
                s.setAttribute('aria-hidden','true');
            }
        });
    }

    function scheduleClose(container, submenu) {
        clearClose(container);
        const t = setTimeout(() => {
            submenu.classList.remove('open');
            submenu.previousElementSibling?.setAttribute('aria-expanded','false');
            const arrow = submenu.previousElementSibling?.querySelector('.arrow');
            if (arrow) arrow.style.transform = 'rotate(0deg)';
            submenu.setAttribute('aria-hidden','true');
        }, CLOSE_DELAY);
        timers.set(container, t);
    }

    function clearClose(container) {
        const t = timers.get(container);
        if (t) {
            clearTimeout(t);
            timers.delete(container);
        }
    }

    document.querySelectorAll('.submenu-container').forEach(container => {
        const btn = container.querySelector('.submenu-btn');
        const submenu = container.querySelector('.submenu');

        // abrir/cerrar solo con click
        btn.addEventListener('click', function (e) {
            const isOpen = submenu.classList.contains('open');

            if (!isOpen) {
                closeAllExcept(submenu);
                submenu.classList.add('open');
                submenu.setAttribute('aria-hidden','false');
                btn.setAttribute('aria-expanded','true');
                const arrow = btn.querySelector('.arrow');
                if (arrow) arrow.style.transform = 'rotate(180deg)';
            } else {
                submenu.classList.remove('open');
                submenu.setAttribute('aria-hidden','true');
                btn.setAttribute('aria-expanded','false');
                const arrow = btn.querySelector('.arrow');
                if (arrow) arrow.style.transform = 'rotate(0deg)';
            }

            e.stopPropagation();
        });

        // mantener abierto si mouse entra al submenu o al botón
        container.addEventListener('mouseenter', () => clearClose(container));
        container.addEventListener('mouseover', () => clearClose(container));

        // cerrar cuando el mouse salga del contenedor
        container.addEventListener('mouseleave', () => scheduleClose(container, submenu));

        // evitar que clicks dentro del submenu cierren todo por bubble
        submenu.addEventListener('click', e => e.stopPropagation());
    });

    // click fuera cierra todo
    document.addEventListener('click', () => {
        document.querySelectorAll('.submenu.open').forEach(s => {
            s.classList.remove('open');
            s.previousElementSibling?.setAttribute('aria-expanded','false');
            const arrow = s.previousElementSibling?.querySelector('.arrow');
            if (arrow) arrow.style.transform = 'rotate(0deg)';
            s.setAttribute('aria-hidden','true');
        });
    });
})();
</script>

</body>
</html>
