<?php
// === LÓGICA DO NOME DO SERVIDOR ===
require_once __DIR__ . '/db.php'; 
$nome_painel = "TOP IPTV"; 
try {
    global $pdo;
    $stmt_nome = $pdo->query("SELECT nome_servidor FROM configuracoes LIMIT 1");
    $res_nome = $stmt_nome->fetchColumn();
    if($res_nome) $nome_painel = $res_nome;
} catch(Exception $e){}
// ==================================

$pg = basename($_SERVER['PHP_SELF']);
$user_role = $_SESSION['role'] ?? 'reseller';

if (!isset($_SESSION['role']) && isset($_SESSION['user_id']) && $_SESSION['user_id'] == 1) {
    $user_role = 'admin';
}
$is_admin = ($user_role === 'admin');

$content_pages = ['channels.php', 'movies.php', 'series.php', 'importer.php', 'categories.php', 'atualizar_tmdb.php', 'importar_epg.php', 'bouquets.php'];
$is_content_active = in_array($pg, $content_pages);

// ADICIONEI 'backup.php' AQUI PARA O MENU NÃO FECHAR AO CLICAR
$system_pages = ['manager.php', 'tools.php', 'mover.php', 'firewall.php', 'system_logs.php', 'config_painel.php', 'server_monitor.php', 'servers.php', 'backup.php']; 
$is_system_active = in_array($pg, $system_pages);

// IPTV Clients
$client_pages = ['clients.php', 'monitor.php', 'migrar.php', 'migrar_clientes.php'];
$is_clients_active = in_array($pg, $client_pages);

// P2P Clients
$p2p_pages = ['p2p_manager.php'];
$is_p2p_active = in_array($pg, $p2p_pages);

// Resellers
$reseller_pages = ['resellers.php'];
$is_resellers_active = in_array($pg, $reseller_pages);

// Automação
$automacao_pages = ['chatbot.php'];
$is_automacao_active = in_array($pg, $automacao_pages);
?>

<style>
    .brand-text {
        font-family: 'Righteous', cursive;
        font-size: 1.5rem;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 1px;
        color: #ffffff !important;
        text-decoration: none !important;
        display: block;
        filter: drop-shadow(2px 2px 4px rgba(0,0,0,0.5));
    }
    #sidebar .dismiss { display: none !important; }
    #sidebar > i { display: none !important; }
    #sidebar .logo-nav-item { margin-top: 10px; }
    .nav-sub-link { padding-left: 30px !important; font-size: 0.9rem; }
</style>

<nav id="sidebar" class="bg-dark bg-gradient sidebar">
    <ul class="nav nav-pills flex-column">
        <li class="logo-nav-item text-center py-3">
            <a class="navbar-brand" href="dashboard.php">
                <span class="brand-text"><?php echo htmlspecialchars($nome_painel); ?></span>
            </a>
        </li>
        
        <li><h6 class="nav-header pl-3">Navegação</h6></li>
        <li class="nav-item">
            <a class="nav-link <?php echo ($pg == 'dashboard.php') ? 'active' : ''; ?>" href="dashboard.php">
                <i class="fad fa-fw fa-browser"></i> Dashboard
            </a>
        </li>
        
       <li class="menu-titulo" style="color: #ffffff; padding-left: 25px; margin-top: 10px;">
    <strong>Informações</strong>
</li>

<li class="nav-item">
    <a href="ultimos_adicionados.php" class="nav-link">
        <i class="fa fa-clock-o"></i> <span>Últimos Conteúdos</span>
    </a>
</li>      
<li class="nav-item"><a href="ajuda.php" class="nav-link"><i class="fa fa-book"></i> <span>Material de Ajuda</span></a></li>
        <li><h6 class="nav-header pl-3">Gestão</h6></li>
        
        <li class="nav-item">
            <a class="nav-link <?php echo $is_clients_active ? '' : 'collapsed'; ?>" href="#menuClientes" data-toggle="collapse" aria-expanded="<?php echo $is_clients_active ? 'true' : 'false'; ?>">
                <i class="fad fa-fw fa-users"></i> Clientes IPTV <i class="fa fa-caret-down float-right mt-1"></i>
            </a>
            <div class="collapse <?php echo $is_clients_active ? 'show' : ''; ?>" id="menuClientes">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'clients.php' && isset($_GET['filter']) && $_GET['filter'] == 'trial') ? 'active font-weight-bold' : ''; ?>" href="clients.php?filter=trial">
                            <i class="fad fa-fw fa-clock text-warning"></i> Gerenciar Testes
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'clients.php' && !isset($_GET['filter'])) ? 'active font-weight-bold' : ''; ?>" href="clients.php">
                            <i class="fad fa-fw fa-user-friends text-info"></i> Gerenciar Clientes
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'monitor.php') ? 'active font-weight-bold' : ''; ?>" href="monitor.php">
                            <i class="fad fa-fw fa-heartbeat text-danger"></i> Clientes Online
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'migrar.php') ? 'active font-weight-bold' : ''; ?>" href="migrar_clientes.php">
                            <i class="fad fa-fw fa-exchange-alt text-primary"></i> Migrar Clientes
                        </a>
                    </li>
                </ul>
            </div>
        </li>

        <li class="nav-item">
            <a class="nav-link <?php echo $is_p2p_active ? '' : 'collapsed'; ?>" href="#menuP2P" data-toggle="collapse" aria-expanded="<?php echo $is_p2p_active ? 'true' : 'false'; ?>">
                <i class="fad fa-fw fa-share-alt text-success"></i> Clientes P2P <i class="fa fa-caret-down float-right mt-1"></i>
            </a>
            <div class="collapse <?php echo $is_p2p_active ? 'show' : ''; ?>" id="menuP2P">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'p2p_manager.php') ? 'active font-weight-bold' : ''; ?>" href="p2p_manager.php">
                            <i class="fad fa-fw fa-users text-success"></i> Gerenciar P2P
                        </a>
                    </li>
                </ul>
            </div>
        </li>

        <?php if($is_admin): ?>
        <li><h6 class="nav-header pl-3">Conteúdos</h6></li>
        
        <li class="nav-item">
            <a class="nav-link <?php echo $is_content_active ? '' : 'collapsed'; ?>" href="#menuConteudos" data-toggle="collapse" aria-expanded="<?php echo $is_content_active ? 'true' : 'false'; ?>">
                <i class="fad fa-fw fa-film"></i> Conteúdos <i class="fa fa-caret-down float-right mt-1"></i>
            </a>
            <div class="collapse <?php echo $is_content_active ? 'show' : ''; ?>" id="menuConteudos">
                <ul class="nav flex-column">
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="channels.php">📺 Canais</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="movies.php">🎬 Filmes</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="series.php">🍿 Séries</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="categories.php">📂 Categorias</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="bouquets.php"><i class="fas fa-box-open"></i> Pacotes (Bouquets)</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="importer.php">📥 Importador</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-info fw-bold" href="atualizar_tmdb.php"><i class="fas fa-magic"></i> Sincronizar TMDB</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-warning fw-bold" href="importar_epg.php"><i class="fas fa-list-alt"></i> Importar EPG</a></li>
                </ul>
            </div>
        </li>
        <?php endif; ?>

        <li><h6 class="nav-header pl-3">Equipe</h6></li>
        <li class="nav-item">
            <a class="nav-link <?php echo $is_resellers_active ? '' : 'collapsed'; ?>" href="#menuRevenda" data-toggle="collapse" aria-expanded="<?php echo $is_resellers_active ? 'true' : 'false'; ?>">
                <i class="fad fa-fw fa-users"></i> Revendedores <i class="fa fa-caret-down float-right mt-1"></i>
            </a>
            <div class="collapse <?php echo $is_resellers_active ? 'show' : ''; ?>" id="menuRevenda">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'resellers.php') ? 'active font-weight-bold' : ''; ?>" href="resellers.php">
                            <i class="fad fa-fw fa-user-tie text-info"></i> Gerenciar Revendedor
                        </a>
                    </li>
                </ul>
            </div>
        </li>

        <li><h6 class="nav-header pl-3 text-success">Ferramentas</h6></li>
        <li class="nav-item">
            <a class="nav-link <?php echo $is_automacao_active ? '' : 'collapsed'; ?>" href="#menuAutomacao" data-toggle="collapse" aria-expanded="<?php echo $is_automacao_active ? 'true' : 'false'; ?>">
                <i class="fab fa-fw fa-whatsapp text-success"></i> Automação <i class="fa fa-caret-down float-right mt-1"></i>
            </a>
            <div class="collapse <?php echo $is_automacao_active ? 'show' : ''; ?>" id="menuAutomacao">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'chatbot.php') ? 'active font-weight-bold text-white bg-success' : ''; ?>" href="chatbot.php">
                            <i class="fab fa-fw fa-whatsapp text-white"></i> Chatbot (Auto)
                        </a>
                    </li>
                    </ul>
            </div>
        </li>
        
        <?php if($is_admin): ?>
        <li><h6 class="nav-header pl-3 text-danger">Configurações</h6></li>
        
        <li class="nav-item">
            <a class="nav-link <?php echo $is_system_active ? '' : 'collapsed'; ?>" href="#menuSistema" data-toggle="collapse" aria-expanded="<?php echo $is_system_active ? 'true' : 'false'; ?>">
                <i class="fad fa-fw fa-cogs text-danger"></i> Sistema <i class="fa fa-caret-down float-right mt-1"></i>
            </a>
            <div class="collapse <?php echo $is_system_active ? 'show' : ''; ?>" id="menuSistema">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'server_monitor.php') ? 'active font-weight-bold text-white bg-info' : ''; ?>" href="server_monitor.php">
                            <i class="fad fa-fw fa-server text-info"></i> Monitor de Recursos
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'servers.php') ? 'active font-weight-bold text-white bg-success' : ''; ?>" href="servers.php">
                            <i class="fad fa-fw fa-network-wired text-success"></i> Gerenciar Load Balance
                        </a>
                    </li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="config_painel.php"><i class="fad fa-fw fa-sliders-h text-warning"></i> Configurações Gerais</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light <?php echo ($pg == 'firewall.php') ? 'active font-weight-bold text-white bg-danger' : ''; ?>" href="firewall.php"><i class="fad fa-fw fa-shield-alt text-danger text-white"></i> Firewall & Segurança</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light <?php echo ($pg == 'system_logs.php') ? 'active font-weight-bold text-white bg-primary' : ''; ?>" href="system_logs.php"><i class="fad fa-fw fa-file-contract text-warning"></i> Logs do Sistema</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="manager.php"><i class="fad fa-fw fa-trash-alt text-danger"></i> Limpeza / Reset</a></li>
                    
                    <li class="nav-item">
                        <a class="nav-link nav-sub-link text-light <?php echo ($pg == 'backup.php') ? 'active font-weight-bold text-white bg-warning' : ''; ?>" href="backup.php">
                            <i class="fad fa-fw fa-database text-warning"></i> Backup & Restore
                        </a>
                    </li>

                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="tools.php"><i class="fad fa-fw fa-sync-alt text-info"></i> Trocar DNS</a></li>
                    <li class="nav-item"><a class="nav-link nav-sub-link text-light" href="mover.php"><i class="fad fa-fw fa-exchange-alt text-warning"></i> Mover Conteúdo</a></li>
                </ul>
            </div>
        </li>
        <?php endif; ?>

        <li class="nav-item mt-3">
            <a class="nav-link text-danger" href="logout.php"><i class="fad fa-fw fa-power-off"></i> Sair</a>
        </li>
    </ul>
</nav>