<?php
session_start();
require_once 'config.php';

$pdo = getConexaoAtiva(); // Banco Principal
$pdo_int = getConexaoInterna(); // Banco Local

$pdo->exec("SET NAMES utf8mb4");
$pdo_int->exec("SET NAMES utf8mb4");

// ====================================================================
// INTELIGÊNCIA ARTIFICIAL: DETECTA SE É XUI ONE OU XTREAM UI
// ====================================================================
$modo_ativo = 'interno';
try {
    $modo_ativo = $pdo_int->query("SELECT modo_ativo FROM configuracoes LIMIT 1")->fetchColumn() ?: 'interno';
} catch (Exception $e) {}

$tabela_revendedores = ($modo_ativo === 'xui') ? '`users`' : '`reg_users`';

// --- INÍCIO: BUSCA DE PERSONALIZAÇÃO DINÂMICA ---
$nome_painel = 'TOP IPTV';
$logo_url = '';
$cor_primaria = '#007bff';

try {
    $tabela_existe = $pdo_int->query("SHOW TABLES LIKE 'painel_config'")->rowCount() > 0;
    if ($tabela_existe) {
        $config_painel = $pdo_int->query("SELECT * FROM painel_config LIMIT 1")->fetch(PDO::FETCH_ASSOC);
        if ($config_painel) {
            $nome_painel = !empty($config_painel['nome_painel']) ? $config_painel['nome_painel'] : $nome_painel;
            $logo_url = !empty($config_painel['logo_url']) ? $config_painel['logo_url'] : $logo_url;
            $cor_primaria = !empty($config_painel['cor_primaria']) ? $config_painel['cor_primaria'] : $cor_primaria;
        }
    }
} catch (Exception $e) {}

// Verificação de segurança nativa do nosso painel
if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit; }

$role = $_SESSION['user_role'] ?? 'reseller'; 
$nome_usuario = $_SESSION['username'] ?? 'Usuário';
$user_id_logado = $_SESSION['user_id'];

try { $user_credits = $pdo->query("SELECT credits FROM $tabela_revendedores WHERE id = " . intval($user_id_logado))->fetchColumn() ?: 0; } catch (Exception $e) { $user_credits = 0; }

// ==============================================================================
// DESCOBRE A URL (DNS) ATUAL DO PAINEL AUTOMATICAMENTE
// ==============================================================================
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$dominio_atual = $_SERVER['HTTP_HOST'];
$dns_completo = $protocol . "://" . $dominio_atual;
// ==============================================================================

?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <link rel="stylesheet" href="tema.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Material de Ajuda - <?php echo htmlspecialchars($nome_painel); ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { 
            --bg-dark: #0f1114; 
            --bg-sidebar: #1e2227; 
            --bg-card: #272c33; 
            --text-main: #d1d5da; 
            --text-muted: #8a94a1; 
            --accent: <?php echo $cor_primaria; ?>; 
            --border-color: #3a4149; 
            --success: #00c851; 
            --danger: #dc3545; 
            --warning: #f39c12;
            --info: #33b5e5;
        }
        body { margin: 0; font-family: 'Segoe UI', sans-serif; background-color: var(--bg-dark); color: var(--text-main); display: flex; height: 100vh; overflow: hidden; }

        /* SIDEBAR (HERDADO DA SIDEBAR.PHP) */
        .sidebar { width: 260px; background-color: var(--bg-sidebar); border-right: 1px solid var(--border-color); display: flex; flex-direction: column; transition: all 0.3s ease; z-index: 1001; }
        .sidebar-header { padding: 25px 20px; text-align: center; font-size: 22px; font-weight: bold; border-bottom: 1px solid var(--border-color); color: white; }
        .sidebar-header span { color: var(--accent); }
        .sidebar-logo { max-width: 180px; max-height: 60px; object-fit: contain; }
        .user-info { padding: 20px; text-align: center; border-bottom: 1px solid var(--border-color); }
        .user-info .badge { background: rgba(255, 255, 255, 0.05); color: var(--accent); padding: 4px 10px; border-radius: 4px; font-size: 11px; text-transform: uppercase; font-weight: bold; margin-top: 5px; display: inline-block; }
        .user-info .credits-badge { display: block; margin-top: 10px; color: var(--success); font-weight: bold; font-size: 13px; }
        .menu-list { list-style: none; padding: 0; margin: 0; flex: 1; overflow-y: auto; }
        .menu-item { padding: 14px 25px; color: var(--text-main); text-decoration: none; display: flex; align-items: center; transition: 0.2s; font-size: 15px; border-left: 4px solid transparent; cursor: pointer; }
        .menu-item i:first-child { margin-right: 15px; width: 20px; text-align: center; font-size: 16px; color: var(--text-muted); }
        .menu-item:hover, .menu-item.active { background: #2c3138; color: white; border-left-color: var(--accent); }
        .submenu { display: none; background: #121519; list-style: none; padding: 0; margin: 0; }
        .submenu .menu-item { padding-left: 52px; font-size: 14px; border-left: none; }
        .arrow-icon { margin-left: auto; transition: 0.3s; font-size: 12px; }
        .rotate { transform: rotate(180deg); }
        .menu-category { padding: 20px 25px 8px; font-size: 11px; text-transform: uppercase; color: var(--text-muted); font-weight: bold; letter-spacing: 1px; }
        .logout-btn { padding: 15px; background-color: var(--danger); color: white; text-align: center; text-decoration: none; font-weight: bold; margin-top: auto; }

        /* MAIN CONTENT */
        .main { flex: 1; display: flex; flex-direction: column; overflow-y: auto; width: 100%; position: relative; }
        .topbar { display: flex; justify-content: space-between; align-items: center; padding: 15px 30px; background-color: var(--bg-sidebar); border-bottom: 1px solid var(--border-color); }
        .content-area { padding: 25px; flex: 1; max-width: 1000px; margin: 0 auto; width: 100%; box-sizing: border-box; }
        .mode-badge { padding: 6px 15px; background: rgba(0, 123, 255, 0.15); color: var(--accent); border-radius: 20px; font-size: 12px; font-weight: bold; border: 1px solid var(--accent); white-space: nowrap; display: inline-block; }

        /* ESTILOS DO CARD DE AJUDA NATIVOS DO PAINEL */
        .help-card { background: var(--bg-card); border: 1px solid var(--border-color); border-radius: 8px; overflow: hidden; margin-bottom: 30px; box-shadow: 0 4px 15px rgba(0,0,0,0.2); }
        .help-header { background: #1a1d21; padding: 20px 25px; font-size: 18px; font-weight: bold; color: white; border-bottom: 1px solid var(--border-color); display: flex; align-items: center; gap: 10px; }
        .help-body { padding: 30px 25px; font-size: 14px; line-height: 1.8; color: var(--text-main); }
        
        .help-title { color: var(--success); font-weight: bold; font-size: 16px; margin-bottom: 15px; display: block; text-transform: uppercase; letter-spacing: 1px; }
        .help-subtitle { font-weight: bold; color: white; margin-top: 20px; display: block; font-size: 15px; border-left: 3px solid var(--accent); padding-left: 10px; }
        
        .help-link { color: var(--info); text-decoration: none; word-break: break-all; font-weight: bold; transition: 0.2s; }
        .help-link:hover { color: #0099cc; text-decoration: underline; }
        
        .check-icon { color: var(--success); margin-right: 10px; font-size: 16px; }
        .item-box { background: #121519; padding: 15px 20px; border-radius: 6px; border: 1px solid var(--border-color); margin-top: 15px; transition: 0.2s; }
        .item-box:hover { border-color: var(--accent); }

        .menu-toggle { display: none; background: none; border: none; color: white; font-size: 24px; cursor: pointer; }
        .sidebar-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.7); z-index: 1000; }
        
        
        
        
        
        
        /* ==================================================================== */
        /* FORÇA A COR AZUL NO ADMINISTRADOR E VERDE NO SALDO */
        /* ==================================================================== */
        .user-info > div:nth-child(2), .user-info .badge { background: rgba(0, 123, 255, 0.2) !important; color: var(--accent) !important; padding: 4px 10px !important; border-radius: 4px !important; font-size: 11px !important; text-transform: uppercase !important; font-weight: bold !important; margin-top: 5px !important; margin-bottom: 5px !important; display: inline-block !important; }
        .user-info > div:nth-child(3), .user-info .credits-badge, .user-info i.fa-coins { color: var(--success) !important; font-weight: bold !important; font-size: 13px !important; display: inline-block !important; }
        .user-info > div:nth-child(3) { margin-top: 8px !important; display: block !important; }

        /* ==================================================================== */
        /* FORÇA OS ÍCONES DA SIDEBAR A FICAREM AZUIS */
        /* ==================================================================== */
        .sidebar i, .sidebar span[class*="icon"], .sidebar .fas, .sidebar .fab { color: var(--accent) !important; }
        .menu-item i:first-child { margin-right: 12px; width: 20px; text-align: center; color: var(--accent) !important; }
        .menu-item:hover, .menu-item.active { background-color: #2c3138 !important; color: white !important; border-left-color: var(--accent) !important; }
        .sidebar .active, .sidebar .menu-item.active, .sidebar li.active > a { border-left-color: var(--accent) !important; color: white !important; background: #24282d !important; }
        .sidebar a:hover, .sidebar .menu-item:hover { border-left-color: var(--accent) !important; color: white !important; background: #2c3138 !important; }
        
        
        
        
        
        
        
        
        

        @media (max-width: 768px) {
            .sidebar { position: fixed; left: -260px; height: 100%; }
            .sidebar.active { left: 0; }
            .sidebar-overlay.active { display: block; }
            .menu-toggle { display: block; margin-right: 15px; }
            .topbar { padding: 15px 20px; justify-content: flex-start; }
            .topbar-right { margin-left: auto; }
            .content-area { padding: 15px; }
        }
    </style>
</head>
<body>

    <div class="sidebar-overlay" id="overlay" onclick="toggleMenu()"></div>

    <?php include 'sidebar.php'; ?>

    <div class="main">
        <header class="topbar">
            <div style="display: flex; align-items: center;">
                <button class="menu-toggle" id="btn-menu" onclick="toggleMenu()"><i class="fas fa-bars"></i></button>
                <h2 style="margin:0; color: white; margin-left: 10px;">Central de Suporte</h2>
            </div>
            <div class="topbar-right">
                <span class="mode-badge"><i class="fas fa-database"></i> MODO: <?php echo defined('MODO_SISTEMA') ? strtoupper(MODO_SISTEMA) : strtoupper($modo_ativo); ?></span>
            </div>
        </header>

        <div class="content-area">

            <div class="help-card">
                <div class="help-header">
                    <i class="fas fa-book"></i> MATERIAL DE AJUDA
                </div>
                
                <div class="help-body">
                    
                    <span class="help-title">
                        <i class="fas fa-check-square"></i> INFORMAÇÕES PARA O SISTEMA IPTV
                    </span>
                    
                    <div class="item-box">
                        <span class="help-subtitle" style="margin-top: 0;">DNS PARA SMARTERS PLAYER IPTV | XCIPTV ENTRE OUTROS</span>
                        <div style="margin-top: 10px; font-size: 15px;">
                            <div style="margin-bottom: 5px;"><i class="fas fa-check check-icon"></i> <?php echo $dns_completo; ?>:80</div>
                            <div><i class="fas fa-check check-icon"></i> <?php echo $dns_completo; ?></div>
                        </div>
                    </div>

                    <div class="item-box">
                        <span class="help-subtitle" style="margin-top: 0;">BAY IPTV (EXCLUSIVO PARA SAMSUNG)</span>
                        <div style="margin-top: 10px; font-size: 15px;">
                            <i class="fas fa-check check-icon"></i> <a href="https://cms.bayip.tv/" target="_blank" class="help-link">https://cms.bayip.tv/</a>
                        </div>
                    </div>

                    <hr style="border: none; border-top: 1px solid var(--border-color); margin: 30px 0;">
                    
                    <h3 style="color: white; margin-bottom: 20px;"><i class="fas fa-upload" style="color: var(--accent);"></i> SITES PARA ADICIONAR LISTA M3U</h3>

                    <div class="item-box">
                        <div style="margin-bottom: 5px; color: white;"><strong>SMART IPTV</strong></div>
                        <i class="fas fa-check check-icon"></i> <a href="https://siptv.eu/mylist/" target="_blank" class="help-link">https://siptv.eu/mylist/</a>
                    </div>

                    <div class="item-box">
                        <div style="margin-bottom: 5px; color: white;"><strong>SSIPTV</strong></div>
                        <i class="fas fa-check check-icon"></i> <a href="https://ss-iptv.com/en/users/playlist" target="_blank" class="help-link">https://ss-iptv.com/en/users/playlist</a>
                    </div>

                    <div class="item-box">
                        <div style="margin-bottom: 5px; color: white;"><strong>DUPLEX PLAY</strong></div>
                        <i class="fas fa-check check-icon"></i> <a href="http://edit.duplexplay.com/" target="_blank" class="help-link">http://edit.duplexplay.com/</a>
                    </div>
                    
                    <div class="item-box">
                        <div style="margin-bottom: 5px; color: white;"><strong>FLIX IPTV</strong></div>
                        <i class="fas fa-check check-icon"></i> <a href="https://flixiptv.eu/mylist" target="_blank" class="help-link">https://flixiptv.eu/mylist</a>
                    </div>
                    
                    <hr style="border: none; border-top: 1px solid var(--border-color); margin: 30px 0;">

                    <h3 style="color: white; margin-bottom: 20px;"><i class="fab fa-youtube" style="color: #ff0000;"></i> VÍDEOS TUTORIAIS</h3>

                    <div class="item-box">
                        <div style="margin-bottom: 5px; color: white;"><strong>COMO ALTERAR DNS E INSTALAR O STB NA SMART TV LG</strong></div>
                        <i class="fas fa-play-circle" style="color: #ff0000; margin-right: 10px;"></i> <a href="https://youtu.be/zUImRaFN7Bc" target="_blank" class="help-link">https://youtu.be/zUImRaFN7Bc</a>
                    </div>

                    <div class="item-box">
                        <div style="margin-bottom: 5px; color: white;"><strong>COMO ALTERAR DNS NA SAMSUNG PARA USAR O STB</strong></div>
                        <i class="fas fa-play-circle" style="color: #ff0000; margin-right: 10px;"></i> <a href="https://youtu.be/fg5paElD4Xg" target="_blank" class="help-link">https://youtu.be/fg5paElD4Xg</a>
                    </div>

                    <div class="item-box">
                        <div style="margin-bottom: 5px; color: white;"><strong>INSTALAR O SMART IPTV NA SMARTTV SAMSUNG (VIA DNS)</strong></div>
                        <i class="fas fa-play-circle" style="color: #ff0000; margin-right: 10px;"></i> <a href="https://youtu.be/Qwl8-5n0tAA" target="_blank" class="help-link">https://youtu.be/Qwl8-5n0tAA</a>
                    </div>

                    <div class="item-box">
                        <div style="margin-bottom: 5px; color: white;"><strong>COMO INSTALAR O MEGASAT NA SMART TV SAMSUNG (SS IPTV)</strong></div>
                        <i class="fas fa-play-circle" style="color: #ff0000; margin-right: 10px;"></i> <a href="https://youtu.be/SWYols6gQaM" target="_blank" class="help-link">https://youtu.be/SWYols6gQaM</a>
                    </div>

                </div>
            </div>
            </div>
            <?php include 'rodape.php'; ?>
    </div>

    <script>
        // Lógica Padrão do Menu Lateral via Include
        const sidebar = document.getElementById('sidebar');
        const overlay = document.getElementById('overlay');
        
        function toggleMenu() { 
            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'); }
        }
    </script>
    <script src="tema.js"></script>
</body>
</html>