<?php
// sidebar.php

// Proteção para não abrir o arquivo diretamente
if (!isset($_SESSION['user_id'])) { exit; }

$role_menu = $_SESSION['user_role'] ?? 'reseller';
$nome_user_menu = $_SESSION['username'] ?? 'Usuário';

// Busca créditos atualizados (opcional, se quiser que o saldo atualize sempre)
try {
    $credits_menu = $pdo->query("SELECT credits FROM reg_users WHERE id = " . $_SESSION['user_id'])->fetchColumn() ?: 0;
} catch (Exception $e) { $credits_menu = 0; }
?>

<style>
/* ==========================================================================
   CSS PREMIUM V4.6 - BLINDAGEM CORRIGIDA + DESTAQUES AZUL E VERDE
   ========================================================================== */
   
/* 1. Variáveis Padrão (Tema Escuro - Estilo Sigma) */
:root {
    --side-bg: #0f1115; 
    --side-shadow: 2px 0 10px rgba(0, 0, 0, 0.5);
    --side-border: transparent; 
    
    --side-user-bg: transparent; 
    --side-user-border: transparent;
    
    --side-text-strong: #ffffff;
    --side-text-normal: #8b949e; 
    --side-text-hover: #ffffff;
    --side-icon: #8b949e;
    --side-cat: #8b949e;
    
    --side-hover-bg: #1877f2; 
    --side-hover-border: transparent;
    --side-scroll: rgba(255, 255, 255, 0.1);
}

/* 2. Gatilho do Tema Claro (Adaptável) */
body.light-mode, body.light-theme, body.theme-light, [data-theme="light"], .light {
    --side-bg: #ffffff;
    --side-shadow: 2px 0 10px rgba(0, 0, 0, 0.05);
    --side-border: rgba(0, 0, 0, 0.05);
    
    --side-user-bg: transparent;
    --side-user-border: transparent;
    
    --side-text-strong: #1f2328;
    --side-text-normal: #656d76;
    --side-text-hover: #ffffff;
    --side-icon: #656d76;
    --side-cat: #656d76;
    
    --side-hover-bg: #1877f2;
    --side-hover-border: transparent;
    --side-scroll: rgba(0, 0, 0, 0.15);
}

/* 3. BLINDAGEM: Usando #sidebar para dominar o CSS antigo de outras páginas */
nav#sidebar.sidebar {
    background: var(--side-bg) !important;
    box-shadow: var(--side-shadow) !important;
    border-right: 1px solid var(--side-border) !important;
    font-family: 'Inter', '-apple-system', 'Segoe UI', sans-serif !important;
    transition: background 0.3s ease, border 0.3s ease, left 0.3s ease !important;
    width: 300px !important; 
    flex-shrink: 0 !important;
}

@media (max-width: 992px) {
    nav#sidebar.sidebar {
        position: fixed !important;
        left: -300px !important; 
        height: 100% !important;
        z-index: 1001 !important;
    }
    nav#sidebar.sidebar.active {
        left: 0 !important;
    }
}

#sidebar .sidebar-header {
    padding: 30px 20px 10px 20px !important;
    background: transparent !important;
    border-bottom: none !important;
    text-align: left !important;
}

#sidebar .user-info {
    background: var(--side-user-bg) !important;
    margin: 0 20px 20px 20px !important;
    border-radius: 0 !important;
    border: none !important;
    box-shadow: none !important;
    padding: 10px 0 !important;
    text-align: left !important; 
    transition: background 0.3s ease !important;
}

#sidebar .user-info strong {
    color: var(--side-text-strong) !important;
    font-size: 16px !important;
    font-weight: 500 !important;
    letter-spacing: 0.5px !important;
    display: block !important;
    margin-bottom: 2px !important;
}

/* CARGO: Agora é um "card" azul vivo */
#sidebar .user-info .badge {
    background: #1877f2 !important; 
    color: #ffffff !important;
    border-radius: 4px !important;
    padding: 4px 8px !important;
    font-size: 10px !important;
    font-weight: 600 !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
    box-shadow: none !important;
    display: inline-block !important;
    margin-top: 5px !important;
}

body.light-mode #sidebar .user-info .badge { 
    background: #1877f2 !important; 
    color: #ffffff !important;
}

/* SALDO: Apenas as letras/números verdes, sem fundo */
#sidebar .user-info .credits-badge {
    background: transparent !important;
    border: none !important;
    padding: 0 !important;
    color: #2ecc71 !important; /* Verde brilhante */
    font-size: 13px !important;
    font-weight: 600 !important;
    margin-top: 8px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: flex-start !important;
    gap: 6px !important;
}

#sidebar .menu-category {
    color: var(--side-cat) !important;
    font-size: 11px !important;
    text-transform: uppercase !important;
    letter-spacing: 1.5px !important;
    margin: 20px 20px 10px 20px !important;
    padding-bottom: 0 !important;
    border-bottom: none !important;
    font-weight: 600 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
}

#sidebar .menu-item {
    margin: 2px 12px !important;
    border-radius: 8px !important; 
    padding: 12px 16px !important;
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    transition: all 0.2s ease-in-out !important;
    color: var(--side-text-normal) !important; 
    font-size: 14px !important;
    font-weight: 500 !important;
    display: flex !important;
    align-items: center !important;
    white-space: nowrap !important;
}

#sidebar .menu-item i:first-child {
    font-size: 16px !important;
    width: 28px !important;
    text-align: left !important;
    transition: all 0.2s ease !important;
    color: var(--side-icon) !important; 
    flex-shrink: 0 !important; 
}

#sidebar .badge-sigma {
    font-size: 10px !important;
    font-weight: 700 !important;
    padding: 3px 8px !important;
    border-radius: 12px !important;
    margin-left: 8px !important; 
    color: #ffffff !important;
    text-transform: none !important;
    letter-spacing: normal !important;
    flex-shrink: 0 !important;
}
#sidebar .badge-yellow { background: #d4a000 !important; color: #1f2328 !important; }
#sidebar .badge-green { background: #238636 !important; color: #ffffff !important;}
#sidebar .badge-purple { background: #8957e5 !important; color: #ffffff !important;}
#sidebar .badge-red { background: #da3633 !important; color: #ffffff !important;}

/* Efeito Sólido Sigma APENAS NO HOVER (Para evitar o bug do .active fixo) */
#sidebar .menu-item:hover {
    background: var(--side-hover-bg) !important;
    color: var(--side-text-hover) !important;
    transform: none !important; 
    box-shadow: none !important;
}

#sidebar .menu-item:hover i:first-child,
#sidebar .menu-item:hover .badge-sigma.badge-yellow {
    color: var(--side-text-hover) !important; 
    text-shadow: none !important;
}

#sidebar .submenu {
    background: transparent !important;
    border-radius: 0 !important;
    margin: 0 12px 5px 24px !important; 
    padding: 0 !important;
    border: none !important;
    border-left: 2px solid rgba(139, 148, 158, 0.2) !important; 
}

#sidebar .submenu .menu-item {
    margin: 2px 0 2px 10px !important;
    padding: 10px 15px !important;
    font-size: 13px !important;
}

#sidebar .submenu .menu-item:hover {
    transform: none !important;
}

#sidebar .arrow-icon {
    margin-left: auto !important; 
    font-size: 12px !important;
    transition: transform 0.3s ease !important;
    color: var(--side-icon) !important;
    flex-shrink: 0 !important;
}

#sidebar .menu-item:hover .arrow-icon {
    color: var(--side-text-hover) !important;
}

#sidebar .logout-btn {
    margin: 20px 15px !important;
    border-radius: 8px !important;
    background: transparent !important;
    color: var(--side-text-normal) !important;
    padding: 12px 16px !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    text-transform: none !important;
    letter-spacing: normal !important;
    box-shadow: none !important;
    transition: all 0.2s ease !important;
    border: none !important;
    display: flex !important;
    justify-content: flex-start !important;
    align-items: center !important;
    gap: 12px !important;
}

#sidebar .logout-btn i {
    font-size: 16px !important;
    color: var(--side-icon) !important;
}

#sidebar .logout-btn:hover {
    background: rgba(255, 75, 43, 0.1) !important;
    color: #ff4b2b !important;
    transform: none !important;
}

#sidebar .logout-btn:hover i {
    color: #ff4b2b !important;
}

#sidebar .menu-list::-webkit-scrollbar {
    width: 5px !important;
}
#sidebar .menu-list::-webkit-scrollbar-track {
    background: transparent !important;
}
#sidebar .menu-list::-webkit-scrollbar-thumb {
    background: var(--side-scroll) !important;
    border-radius: 10px !important;
}
</style>

<nav class="sidebar" id="sidebar">
    <div class="sidebar-header">
        <?php if(!empty($logo_url)): ?>
            <img src="<?php echo htmlspecialchars($logo_url); ?>" alt="Logo" class="sidebar-logo" style="max-height: 40px; object-fit: contain;">
        <?php else: ?>
            <strong style="color: var(--side-text-strong); font-size: 20px;"><?php echo htmlspecialchars($nome_painel ?? 'PAINEL'); ?></strong>
        <?php endif; ?>
    </div>

    <div class="user-info">
        <strong><?php echo htmlspecialchars($nome_user_menu); ?></strong>
        <div class="credits-badge"><?php echo number_format($credits_menu, 1); ?> créditos</div>
        <span class="badge"><?php echo $role_menu === 'admin' ? 'Administrador' : 'Revendedor'; ?></span>
    </div>
    
    <div class="menu-list">
            <a href="dashboard.php" class="menu-item active"><i class="fas fa-chart-line"></i> Dashboard</a>
            <a href="perfil.php" class="menu-item active"><i class="fas fa-user-circle"></i> Meu Perfil</a>
            
            <a href="lancamentos.php" class="menu-item active">
                <i class="fas fa-fire"></i> 
                <span style="flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">Novos Conteúdos</span> 
                <span class="badge-sigma badge-purple">Atualizado</span>
            </a>
            
            <a href="ajuda.php" class="menu-item active"><i class="fas fa-question-circle"></i> Material de Ajuda</a>
            <a href="meus_links.php" class="menu-item active"><i class="fas fa-link"></i> Central de Links</a>
            
            <div class="menu-category">Gestão</div>
            <a href="clientes.php" class="menu-item active"><i class="fas fa-users"></i> Clientes IPTV</a>
            <a href="testes.php" class="menu-item active"><i class="fas fa-vial"></i> Gerenciar Testes</a>
            <a href="online.php" class="menu-item active"><i class="fas fa-plug"></i> Clientes Online</a>
            <a href="revendedores.php" class="menu-item active"><i class="fas fa-user-tie"></i> Revendedores</a>
            <a href="migrar_clientes.php" class="menu-item active"><i class="fas fa-exchange-alt"></i> Migrar Clientes</a>
            <a href="logs_creditos.php" class="menu-item active"><i class="fas fa-list-alt"></i> Extrato de Créditos</a>
            <a href="gerador_banners.php" class="menu-item active"><i class="fas fa-bullhorn"></i> Gerador de Banners</a>
            <a href="gerador_futebol.php" class="menu-item active"><i class="fas fa-futbol"></i> Gerador de Banners Futebol</a>

            
            
            <div class="menu-category">Clientes P2P</div>
            <a href="p2p_clientes.php" class="menu-item active"><i class="fas fa-tv"></i> Clientes P2P</a>
            
            <?php if (!empty($api_conf['api_binstream_url'])): ?>
                <a href="binstream_p2p.php" class="menu-item active">
                    <i class="fas fa-tv"></i> 
                    <span style="flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">Binstream P2P</span> 
                    <span class="badge-sigma badge-purple">Beta</span>
                </a>
            <?php endif; ?>

 <?php if($role === 'admin'): ?>
<div class="menu-category">Gerar Licença</div>
<a href="admin_licencas.php" class="menu-item active"> <i class="fas fa-key"></i> <span>Gerador SaaS</span> </a>
<?php endif; ?>

<div class="menu-category">Api WhatsApp</div>
                <a href="integracoes_api_whatsapp.php" class="menu-item active"><i class="fas fa-plug"></i> Integrações API WhatsApp</a>


            <div class="menu-category">Integrações</div>
            <a href="pagamentos.php" class="menu-item active"><i class="fas fa-dollar-sign"></i> Pagamento Automático</a>
            <a href="admin_loja_creditos.php" class="menu-item active"><i class="fas fa-store"></i> Configurar Loja (PIX)</a>

            <div class="menu-category">Cobranças</div>
            <a href="cobranca.php" class="menu-item active"><i class="fas fa-comment-dollar"></i> Central de Cobrança</a>
            <a href="admin_planos.php" class="menu-item active"><i class="fas fa-tags"></i> Meus Planos de Venda</a>
            
            <a href="comprar_creditos.php" class="menu-item active">
                <i class="fas fa-shopping-cart"></i> 
                <span style="flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">Comprar Créditos</span> 
                <span class="badge-sigma badge-yellow">Comprar</span>
            </a>
          
            <div class="menu-item active" onclick="toggleSubmenu('appsSub')">
                <i class="fas fa-rocket"></i>
                <span style="flex: 1;">Aplicações</span>
                <i class="fas fa-chevron-down arrow-icon" id="arrow-appsSub"></i>
            </div>
            <div class="submenu" id="appsSub">
                <a href="loja_apps.php" class="menu-item active"><i class="fas fa-store"></i> Loja de Aplicativos</a>
                <?php if($role === 'admin'): ?>
                    <a href="admin_loja.php" class="menu-item active"><i class="fas fa-cogs"></i> Gerenciar Loja</a>
                <?php endif; ?>
                <a href="chatbot.php" class="menu-item active"><i class="fab fa-whatsapp"></i> Chatbot Automático</a>
            </div>
           <a href="admin_player_revenda.php" class="menu-item active"><i class="fas fa-play-circle"></i> WebPlayer</a>
            
   <div class="menu-category">Segurança</div>
            <a href="historico_logins.php" class="menu-item active">
                <i class="fas fa-shield-alt"></i> 
                <span style="flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">Sessões Ativas</span> 
                <span class="badge-sigma badge-green">1</span>
            </a>

            <?php if($role === 'admin'): ?>
                <div class="menu-category">Administração</div>
                <a href="dns_servers.php" class="menu-item active"><i class="fas fa-network-wired"></i> DNS Servers</a>
                
                <a href="admin_monitor.php" class="menu-item active"><i class="fas fa-heartbeat"></i> Monitor do Servidor</a>
                <a href="admin_template.php" class="menu-item active"><i class="fas fa-edit"></i> Editor de Template</a>
                <a href="admin_token.php" class="menu-item active"><i class="fas fa-user-secret"></i> Token Links Xui</a>
                <a href="admin_lb.php" class="menu-item active"><i class="fas fa-server"></i> Load Balancers</a>
                <a href="guia_cloudflare.php" class="menu-item active"><i class="fas fa-cloud"></i> Guia Cloudflare</a>
                <a href="integracoes_api.php" class="menu-item active"><i class="fas fa-plug"></i> Integrações API</a>
                 <a href="admin_robo.php" class="menu-item active"><i class="fas fa-robot"></i> Robô de Cobrança</a>
                 <a href="admin_anti_bloqueio.php" class="menu-item active"><i class="fas fa-bolt"></i> Botão de Pânico</a>
                 <a href="admin_anti_fraude.php" class="menu-item active"><i class="fas fa-user-secret"></i> Radar Anti-Fraude</a>  
                 <a href="admin_watchdog.php" class="menu-item active"><i class="fas fa-dog"></i> Watchdog LB</a>  
                 
                 <a href="admin_epg_sync.php" class="menu-item active">
        <i class="fas fa-clock"></i> Sincronizar EPG
    </a>
    

 <div class="menu-item active" onclick="toggleSubmenu('painelSub')">
                    <i class="fas fa-desktop"></i>
                    <span style="flex: 1;">Painel</span>
                    <i class="fas fa-chevron-down arrow-icon" id="arrow-painelSub"></i>
                </div>
                <div class="submenu" id="painelSub">
                    <a href="personalizacao.php" class="menu-item active"><i class="fas fa-paint-brush"></i> Personalização</a>
                </div>
             
                <div class="menu-item active" onclick="toggleSubmenu('adminSub')">
                    <i class="fas fa-toolbox"></i>
                    <span style="flex: 1;">Ferramentas</span>
                    <i class="fas fa-chevron-down arrow-icon" id="arrow-adminSub"></i>
                </div>
                <div class="submenu" id="adminSub">
                    <a href="importar4.php" class="menu-item active"><i class="fas fa-file-import"></i> Importador</a>
                    <a href="importar3.php" class="menu-item active"><i class="fas fa-file-import"></i> Importador Turbo</a>
                    <a href="dns_manager.php" class="menu-item active"><i class="fas fa-link"></i> Alterar DNS</a>
                    <a href="admin_logos.php" class="menu-item active"><i class="fas fa-image"></i> Alterar Logos</a>
                    <a href="mover_conteudos.php" class="menu-item active"><i class="fas fa-exchange-alt"></i> Mover Conteúdos</a>
                    <a href="limpeza.php" class="menu-item active"><i class="fas fa-trash-alt"></i> Limpeza de Dados</a>
                    <a href="limpar_duplicados.php" class="menu-item active"><i class="fas fa-copy"></i> Limpeza de Duplicados</a>
                    <a href="backup.php" class="menu-item active"><i class="fas fa-database"></i> Backup do Sistema</a>
                    <a href="seguranca.php" class="menu-item active"><i class="fas fa-shield-alt"></i> Sistema de Segurança</a>
                    <a href="configuracoes.php" class="menu-item active"><i class="fas fa-tools"></i> Configurações</a>
                </div>

                 <?php if($role === 'admin'): ?>
    <div class="menu-category">Financeiro</div>
     <div class="menu-item active" onclick="toggleSubmenu('finSub')">
                <i class="fas fa-hand-holding-usd"></i>
                <span style="flex: 1;">Financeiro</span>
                <i class="fas fa-chevron-down arrow-icon rotate" id="arrow-finSub"></i>
            </div>
            <div class="submenu" id="finSub" style="display: block;">
                <a href="financeiro.php" class="menu-item active">Faturamento</a>
            </div>
    
    <?php endif; ?>

                <div class="menu-category">Conteúdos</div>
                <a href="categorias.php" class="menu-item active"><i class="fas fa-folder-open"></i><span>Categorias</span></a>
                <div class="menu-item active" onclick="toggleSubmenu('contentSub')">
                    <i class="fas fa-layer-group"></i>
                    <span style="flex: 1;">Mídias</span>
                    <i class="fas fa-chevron-down arrow-icon" id="arrow-contentSub"></i>
                </div>
                <div class="submenu" id="contentSub">
                    <a href="bouquets.php" class="menu-item active">Editar Bouquets</a>
                    <a href="canais.php" class="menu-item active">Canais Ao Vivo</a>
                    <a href="filmes.php" class="menu-item active">Filmes (VOD)</a>
                    <a href="series.php" class="menu-item active">Séries</a>
                </div>
                 <a href="admin_autofix_vod.php" class="menu-item active"><i class="fas fa-magic"></i> Auto-Fix TMDB</a>
            <?php endif; ?>
        </div>
        <a href="logout.php" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Sair</a>
    </nav>

<script>
function toggleMenu() {
    var sidebar = document.querySelector('.sidebar');
    var overlay = document.querySelector('.sidebar-overlay');
    if (sidebar) sidebar.classList.toggle('active');
    if (overlay) overlay.classList.toggle('active');
}

function toggleSubmenu(id) {
    const sub = document.getElementById(id);
    const arrow = document.getElementById('arrow-' + id);
    if (sub.style.display === "block") {
        sub.style.display = "none";
        if(arrow) arrow.classList.remove('rotate');
    } else {
        sub.style.display = "block";
        if(arrow) arrow.classList.add('rotate');
    }
}

// MÁGICA CELULAR: Fecha o menu lateral automaticamente ao clicar na parte escura (Overlay)
document.addEventListener("DOMContentLoaded", function() {
    var overlays = document.querySelectorAll('.sidebar-overlay');
    overlays.forEach(function(ov) {
        ov.addEventListener('click', function() {
            var sidebar = document.querySelector('.sidebar');
            if (sidebar) sidebar.classList.remove('active');
            ov.classList.remove('active');
        });
    });
});

</script>
<script src="tema.js"></script>
