<?php
// menu_lateral.php
// Verificar se a sessão está ativa
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Verificar se o usuário está logado
if (!isset($_SESSION['logado']) || $_SESSION['logado'] !== true) {
    return; // Não exibir menu se não estiver logado
}

$permissao = $_SESSION['permissao'] ?? 'usuario';
$is_admin = in_array($permissao, ['admin', 'super_admin']);
?>

<!-- Menu Lateral Recolhível -->
<div class="sidebar">
    <div class="sidebar-toggle">
        <i class="fas fa-bars"></i>
    </div>
    <ul class="sidebar-menu">
        <li><a href="index.php"><i class="fas fa-home"></i> <span class="menu-text">Início</span></a></li>

        <?php if ($is_admin): ?>
            <li class="menu-item-sidebar">
                <a href="javascript:void(0);" class="submenu-trigger"><i class="fas fa-cog"></i> <span class="menu-text">Administração</span></a>
                <div class="submenu-sidebar">
                    <a href="cadastrar_usuario.php"><i class="fas fa-plus"></i> <span class="menu-text">Cadastrar Usuário</span></a>
                    <a href="gerenciar_usuarios.php"><i class="fas fa-users-cog"></i> <span class="menu-text">Gerenciar Usuários</span></a>
                </div>
            </li>
        <?php endif; ?>

        <li class="menu-item-sidebar">
            <a href="javascript:void(0);" class="submenu-trigger"><i class="fas fa-tasks"></i> <span class="menu-text">Atividades</span></a>
            <div class="submenu-sidebar">
                <a href="cadastrar_atividade.php"><i class="fas fa-pencil-alt"></i> <span class="menu-text">Cadastrar Tipo</span></a>
                <a href="consulta_atividades.php"><i class="fas fa-search"></i> <span class="menu-text">Consulta</span></a>
                <a href="lista_atividades.php"><i class="fas fa-list"></i> <span class="menu-text">Tipos de Atividades</span></a>
            </div>
        </li>

        <li class="menu-item-sidebar">
            <a href="javascript:void(0);" class="submenu-trigger"><i class="fas fa-chart-line"></i> <span class="menu-text">Análise</span></a>
            <div class="submenu-sidebar">
                <a href="mapa_vendas.php"><i class="fas fa-map-marker-alt"></i> <span class="menu-text">Georreferenciada</span></a>
                <a href="dashboard_clientes.php"><i class="fas fa-chart-bar"></i> <span class="menu-text">Gráfica</span></a>
            </div>
        </li>

        <li><a href="calendario.php"><i class="fas fa-calendar"></i> <span class="menu-text">Calendário</span></a></li>
        <li><a href="chat.php"><i class="fas fa-comment"></i> <span class="menu-text">Chat</span></a></li>

        <?php if (in_array($permissao, ['usuario', 'vendedor', 'admin', 'super_admin'])): ?>
            <li class="menu-item-sidebar">
                <a href="javascript:void(0);" class="submenu-trigger"><i class="fas fa-users"></i> <span class="menu-text">Clientes</span></a>
                <div class="submenu-sidebar">
                    <a href="clientes_perdidos.php"><i class="fas fa-user-times"></i><span class="menu-text">Clientes Perdidos</span>
                    <a href="lista_clientes.php"><i class="fas fa-list"></i> <span class="menu-text">Listar Clientes</span></a>
                    <a href="cadastrar_cliente.php"><i class="fas fa-plus"></i> <span class="menu-text">Novo Cliente</span></a> 
                    </a>
                </div>
            </li>
        <?php endif; ?>
        
     <?php if (in_array($permissao, ['usuario', 'vendedor', 'admin', 'super_admin'])): ?>
    <li class="menu-item-sidebar">
        <a href="javascript:void(0);" class="submenu-trigger">
            <i class="fas fa-envelope-open-text"></i> 
            <span class="menu-text">E-mails</span>
        </a>
        <div class="submenu-sidebar">
            <a href="enviar_email.php">
                <i class="fas fa-paper-plane"></i> 
                <span class="menu-text">Enviar E-mail</span>
            </a>

             <a href="templates_email.php">
                <i class="fas fa-folder-open"></i> 
                <span class="menu-text">Templates E-mail</span>
            </a>
            
             <a href="editar_assinatura.php">
                <i class="fas fa-signature"></i> 
                <span class="menu-text">Editar Assinatura</span>
            </a>
        </div>
    </li>
<?php endif; ?>

<li><a href="fluxos_automatizados.php">  <i class="fas fa-project-diagram" aria-hidden="true"></i>
    <span class="menu-text">Fluxos</span></a></li>

        <li class="menu-item-sidebar">
            <a href="javascript:void(0);" class="submenu-trigger"><i class="fas fa-file-alt"></i> <span class="menu-text">Formulários</span></a>
            <div class="submenu-sidebar">
                <a href="cadastrar_formulario.php"><i class="fas fa-plus"></i> <span class="menu-text">Cadastrar formulário</span></a>
                <a href="lista_formularios.php"><i class="fas fa-list"></i> <span class="menu-text">Listar Formulários</span></a>
            </div>
        </li>
        
           <li><a href="funis.php"><i class="fas fa-filter"></i> <span class="menu-text">Gerenciar Funis</span></a></li>

        <li class="menu-item-sidebar">
            <a href="javascript:void(0);" class="submenu-trigger"><i class="fas fa-globe"></i> <span class="menu-text">Origens</span></a>
            <div class="submenu-sidebar">
                <a href="cadastrar_origem.php"><i class="fas fa-plus"></i> <span class="menu-text">Cadastrar Origem</span></a>
                <a href="lista_origens.php"><i class="fas fa-list"></i> <span class="menu-text">Listar Origens</span></a>
            </div>
        </li>

        <li class="menu-item-sidebar">
            <a href="javascript:void(0);" class="submenu-trigger"><i class="fas fa-box"></i> <span class="menu-text">Produtos</span></a>
            <div class="submenu-sidebar">
                <a href="lista_produtos.php"><i class="fas fa-list"></i> <span class="menu-text">Lista de Produtos</span></a>
                <a href="cadastrar_produto.php"><i class="fas fa-plus"></i> <span class="menu-text">Novo Produto</span></a>
            </div>
        </li>

        <!-- Rodapé da versão -->
        <li class="sidebar-footer">
            <span>Versão 0.1.0</span>
            <!-- Marca d'água discreta e centralizada no final do menu -->
            <div class="sidebar-watermark">
                <img src="tvitec_watermark.png" alt="Marca d'água">
            </div>
        </li>
    </ul>
</div>

<!-- Conteúdo principal com espaço para o menu -->
<main class="main-content">
    <!-- Seu conteúdo aqui -->
</main>

<style>
    /* Menu Lateral Recolhível - Corrigido para direita */
    .sidebar {
        position: fixed;
        right: 0;
        top: 70px;
        height: calc(100vh - 70px);
        width: 60px;
        background: white;
        box-shadow: 2px 0 10px rgba(0,0,0,0.2);
        z-index: 1001;
        transition: width 0.3s ease;
        overflow-x: hidden;
        display: flex;
        flex-direction: column;
    }

    .sidebar.expanded {
        width: 250px;
    }

    .sidebar-toggle {
        display: none;
        position: absolute;
        right: 10px;
        top: 10px;
        cursor: pointer;
        z-index: 1002;
    }

    .sidebar-menu {
        list-style: none;
        padding: 0;
        margin: 0;
        flex: 1;
    }

    .sidebar-menu li {
        border-bottom: 1px solid #eee;
    }

    .sidebar-menu a {
        display: flex;
        align-items: center;
        padding: 15px 20px;
        text-decoration: none;
        color: #333;
        white-space: nowrap;
        transition: background-color 0.3s;
    }

    .sidebar-menu a:hover {
        background-color: #f5f5f5;
    }

    .sidebar-menu i {
        width: 24px;
        height: 24px;
        margin-right: 15px;
        text-align: center;
        flex-shrink: 0;
    }

    .sidebar-menu .menu-text {
        opacity: 0;
        transition: opacity 0.2s ease 0.1s;
    }

    .sidebar.expanded .sidebar-menu .menu-text {
        opacity: 1;
    }

    .submenu-sidebar {
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s ease;
        background-color: #f9f9f9;
    }

    .submenu-sidebar.active {
        max-height: 500px;
    }

    .submenu-sidebar a {
        padding-left: 50px;
        font-size: 0.9em;
    }

    .menu-item-sidebar {
        position: relative;
    }

    .menu-item-sidebar > a:after {
        content: "▶";
        margin-left: auto;
        font-size: 0.8em;
        transition: transform 0.3s;
    }

    .menu-item-sidebar > a.active:after {
        transform: rotate(90deg);
    }

    /* Ajuste para o conteúdo principal */
    .main-content {
        margin-right: 60px;
        padding: 20px;
        transition: margin-right 0.3s ease;
    }

    .main-content.expanded {
        margin-right: 250px;
    }

    /* Barra de pesquisa com menu */
    .search-container-with-sidebar {
        left: 60px;
        transition: left 0.3s ease;
    }

    /* Rodapé da sidebar */
    .sidebar-footer {
        text-align: center;
        font-size: 12px;
        color: #777;
        padding: 10px 0;
        background: #fafafa;
        border-top: 1px solid #eee;
    }

    .sidebar.expanded .sidebar-footer span {
        opacity: 1;
        transition: opacity 0.3s ease;
    }

    .sidebar .sidebar-footer span {
        opacity: 0.7;
    }

    /* Marca d'água discreta e centralizada */
    .sidebar-watermark {
        padding: 15px 10px;
        text-align: center;
        border-top: 1px solid #eee;
        background: #fafafa;
        opacity: 0.6;
        transition: opacity 0.3s ease;
    }

    .sidebar-watermark:hover {
        opacity: 0.8;
    }

    .sidebar-watermark img {
        max-width: 100%;
        height: auto;
        max-height: 80px;
        object-fit: contain;
    }

    /* Responsividade */
    @media (max-width: 768px) {
        .sidebar {
            width: 0;
            z-index: 1000;
        }
        
        .sidebar.expanded {
            width: 250px;
        }
        
        .sidebar-toggle {
            display: block;
        }
        
        .main-content {
            margin-right: 0;
        }
        
        .main-content.expanded {
            margin-right: 0;
        }
        
        .sidebar-watermark img {
            max-height: 25px;
        }

        .header-left {
            flex-direction: column;
            align-items: flex-start;
            gap: 5px;
        }

        .funil-selector {
            flex-direction: column;
            align-items: flex-start;
        }

        .filtros-container {
            flex-direction: column;
            gap: 10px;
        }

        .filtros-section {
            flex-wrap: wrap;
            justify-content: center;
        }
    }
</style>