<?php
session_start();
if (!isset($_SESSION['eggzie_iboPlayer2']) || !isset($_SESSION['aviso_seller'])) {
    header('Location: login.php');
    exit();
}

$db = new SQLite3('../a/.eggziedb.db');
$seller_name = $_SESSION['seller_name'];
$result = $db->query("SELECT COUNT(*) as count FROM revenda WHERE usuario = '$seller_name'");
$row = $result->fetchArray(SQLITE3_ASSOC);
$count = $row['count'];

if($count == 0) {
    header('Location: login.php');
    exit();
}

$seller_id = $_SESSION['id'];
$IIIIIIIlI1lI = $_SESSION['id'];
$IIIIIIIlI1ll = new SQLite3('../a/.eggziepanels.db');
$adb = new SQLite3('../api/.adb.db');
$IIIIIIIlI1l1 = $IIIIIIIlI1ll->query('SELECT * FROM USERS WHERE id=\'1\'')->fetchArray();
$IIIIIIIlI11l = $IIIIIIIlI1l1['NAME'];
$IIIIIIIlI111 = $IIIIIIIlI1l1['LOGO'];

$IIIIIIIllIII = file_get_contents('../includes/eggzie.json');
$IIIIIIIllIIl = json_decode($IIIIIIIllIII, true);
$IIIIIIIllII1 = $IIIIIIIllIIl['info']['aa'];
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="google" content="notranslate">
    <title>LDS Play APKs - Master Panel</title>
    <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
    
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Orbitron:400,700|Poppins:300,400,600" rel="stylesheet">
    <link href="../css/sb-admin-<?php echo $IIIIIIIllII1; ?>.css" rel="stylesheet">
    
    <style>
        :root {
            --neon-green: #00ff88;
            --neon-gold: #ffcc00;
            --dark-bg: #050a0e;
            --glass-bg: rgba(255, 255, 255, 0.02);
            --glass-border: rgba(255, 255, 255, 0.08);
        }

        body { 
            font-family: 'Poppins', sans-serif; 
            background-color: var(--dark-bg);
            color: #e0e0e0;
        }

        /* Sidebar Futurística Esmeralda */
        .sidebar {
            background: linear-gradient(180deg, #041210 0%, #0a2e29 50%, #041210 100%) !important;
            border-right: 1px solid var(--glass-border);
            box-shadow: 10px 0 30px rgba(0,0,0,0.6);
        }

        .sidebar-brand {
            padding: 2rem 1rem !important;
            filter: drop-shadow(0 0 8px var(--neon-green));
        }

        .img-profile-nav {
            border-radius: 12px;
            border: 2px solid var(--neon-green);
            padding: 3px;
            box-shadow: 0 0 15px rgba(0, 255, 136, 0.3);
            background: #000;
            transition: 0.4s;
        }

        .img-profile-nav:hover {
            transform: scale(1.1) rotate(2deg);
            box-shadow: 0 0 25px var(--neon-green);
        }

        .user-info-box {
            background: var(--glass-bg);
            backdrop-filter: blur(12px);
            border: 1px solid var(--glass-border);
            border-radius: 15px;
            padding: 15px;
            margin: 0 15px 20px 15px;
            text-align: center;
        }

        .user-info-box strong {
            font-family: 'Orbitron', sans-serif;
            color: var(--neon-green);
            letter-spacing: 1px;
            text-shadow: 0 0 5px var(--neon-green);
        }

        /* TARJA PRETA DO ID */
        .id-badge {
            display: inline-block;
            background-color: #000;
            color: var(--neon-gold);
            padding: 4px 12px;
            border-radius: 6px;
            font-size: 11px;
            border: 1px solid rgba(255, 204, 0, 0.3);
            margin-top: 8px;
            font-weight: 600;
            box-shadow: 0 2px 8px rgba(0,0,0,0.8);
        }

        .nav-item .nav-link {
            margin: 4px 15px;
            border-radius: 8px;
            transition: 0.3s;
            color: rgba(255,255,255,0.7) !important;
        }

        .nav-item .nav-link i {
            color: var(--neon-green);
            text-shadow: 0 0 8px var(--neon-green);
        }

        .nav-item .nav-link:hover, .nav-item.active .nav-link {
            background: rgba(0, 255, 136, 0.08) !important;
            color: #fff !important;
            transform: translateX(8px);
        }

        /* Topbar Esmeralda */
        .topbar {
            background: rgba(5, 10, 14, 0.85) !important;
            backdrop-filter: blur(20px);
            border-bottom: 1px solid var(--glass-border);
            height: 5rem !important;
        }

        .badge-macs {
            background: transparent !important;
            border: 1px solid #ffcc00 !important;
            color: #ffffff !important;
            padding: 8px 18px !important;
            height: 32px !important;
            display: inline-flex !important;
            align-items: center;
            justify-content: center;
            font-family: 'Orbitron', sans-serif;
            font-size: 11px;
            border-radius: 6px !important;
            text-transform: uppercase;
        }

        @keyframes pulse-gold {
            0% { opacity: 0.8; }
            50% { opacity: 1; text-shadow: 0 0 10px var(--neon-gold); }
            100% { opacity: 0.8; }
        }

        .btn-logout-top {
            background: rgba(255, 204, 0, 0.05) !important;
            border: 1px solid #ffcc00 !important;           /* Contorno fino Ouro */
            color: #ffffff !important;
            border-radius: 6px !important;                 /* Arredondamento igual ao dos MACs */
    
            /* Ajuste de tamanho para ficar pequeno */
            padding: 2px 10px !important;                  /* Reduzi o padding para diminuir a altura */
            height: 32px !important;                       /* Força uma altura similar ao botão de MACs */
    
            display: inline-flex !important;               /* Alinha itens sem esticar */
            align-items: center;
            justify-content: center;
    
            font-weight: 600;
            text-transform: uppercase;
            font-size: 10px;                               /* Fonte levemente menor para caber no box pequeno */
            transition: 0.3s;
            text-decoration: none !important;
        }

        .btn-logout-top i {
            color: #00ff88 !important;
            font-size: 12px !important;
            margin-left: 8px;
            text-shadow: 0 0 5px rgba(0, 255, 136, 0.5);
        }

        .btn-logout-top:hover {
            background: rgba(255, 204, 0, 0.15) !important;
            box-shadow: 0 0 8px rgba(255, 204, 0, 0.2);
        }

        #content-wrapper { background-color: var(--dark-bg); }
        .sidebar-divider { border-top: 1px solid var(--glass-border); }

        /* ================= POPUP ALERTA GLOBAL (REVENDAS) ================= */
        #globalAlertOverlay {
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,.78);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 999999;
            padding: 18px;
        }
        #globalAlertBox {
            width: 100%;
            max-width: 560px;
            background: #000;
            border: 2px solid #00d4ff;
            border-radius: 14px;
            padding: 18px 18px 14px;
            color: #fff;
            box-shadow: 0 0 20px rgba(0, 212, 255, 0.35);
            transform: scale(.96);
            opacity: 0;
            transition: all .18s ease;
        }
        #globalAlertOverlay.show #globalAlertBox { transform: scale(1); opacity: 1; }
        #globalAlertTitle {
            font-family: 'Orbitron', sans-serif;
            font-weight: 700;
            letter-spacing: 1px;
            margin-bottom: 10px;
            display: flex;
            gap: 10px;
            align-items: center;
        }
        #globalAlertTitle i { color: #00d4ff; text-shadow: 0 0 10px rgba(0, 212, 255, .6); }
        #globalAlertText { line-height: 1.35; opacity: .95; margin-bottom: 14px; white-space: pre-wrap; }
        #globalAlertActions { display: flex; gap: 10px; justify-content: flex-end; }
        .globalAlertBtn {
            border: 1px solid rgba(0, 212, 255, .55);
            background: transparent;
            color: #00d4ff;
            padding: 8px 14px;
            border-radius: 10px;
            font-family: 'Orbitron', sans-serif;
            font-size: 12px;
            cursor: pointer;
            transition: .2s;
        }
        .globalAlertBtn:hover {
            background: #00d4ff;
            color: #000;
            box-shadow: 0 0 15px rgba(0, 212, 255, .45);
        }
        .globalAlertBtnClose {
            border-color: rgba(255, 75, 43, .7);
            color: #ff4b2b;
        }
        .globalAlertBtnClose:hover {
            background: #ff4b2b;
            color: #fff;
            box-shadow: 0 0 15px rgba(255, 75, 43, .35);
        }
        /* ================= FIM POPUP ================= */
    </style>
</head>

<body id="page-top">
    <div id="wrapper">
        <ul class="navbar-nav sidebar sidebar-dark accordion" id="accordionSidebar">
            
            <a class="sidebar-brand d-flex flex-column align-items-center justify-content-center" href="users.php">
                <div class="sidebar-brand-icon">
                    <?php 
                        $usuario_pasta = $seller_name; 
                        $diretorio_fisico = "../rtx/logo/" . $usuario_pasta . "/logo/";
                        $arquivos = glob($diretorio_fisico . "{*.jpg,*.jpeg,*.png,*.gif,*.webp}", GLOB_BRACE);

                        if ($arquivos && count($arquivos) > 0) {
                            $nome_arquivo = basename($arquivos[0]);
                            $url_final = "/iborevenda/rtx/logo/" . $usuario_pasta . "/logo/" . $nome_arquivo;
                        } else {
                            $url_final = "/iborevenda/img/logo.png";
                        }
                    ?>
                    <img class="img-profile-nav" width="76px" src="<?php echo $url_final; ?>" onerror="this.src='/iborevenda/img/logo.png';">
                </div>
            </a>

            <div class="user-info-box">
                <?php
                $result_ref = $db->query("SELECT id_ref FROM revenda WHERE usuario = '$seller_name'");
                $row_ref = $result_ref->fetchArray();
                $id_ref = $row_ref['id_ref'] ?? '0';
                ?>
                <small style="color: var(--neon-green); opacity: 0.8; font-size: 10px; text-transform: uppercase; letter-spacing: 1px;">Seja Bem Vindo</small>
                <strong class="d-block"><?php echo strtoupper($seller_name); ?></strong>
                <span class="id-badge">ID REF: <?php echo $id_ref; ?></span>
            </div>

            <hr class="sidebar-divider">

            <li class="nav-item">
                <a class="nav-link" href="index.php">
                    <i class="fas fa-users-cog"></i>
                    <span>Usuários</span>
                </a>
            </li>

            <li class="nav-item">
                <a class="nav-link" href="sub_revenda.php">
                    <i class="fas fa-network-wired"></i>
                    <span>Sub Revendas</span>
                </a>
            </li>

            <li class="nav-item">
                <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseAds">
                    <i class="fas fa-satellite-dish"></i>
                    <span>Publicidade</span>
                </a>
                <div id="collapseAds" class="collapse" data-parent="#accordionSidebar">
                    <div class="bg-dark py-2 collapse-inner rounded border border-success">
                        <a class="collapse-item text-white" href="ads.php">Enviar Banner</a>
                    </div>
                </div>
            </li>

            <li class="nav-item">
                <a class="nav-link" href="Image.php">
                    <i class="fas fa-vr-cardboard"></i>
                    <span>Plano de Fundo</span>
                </a>
            </li>

            <li class="nav-item">
                <a class="nav-link" href="logo.php">
                    <i class="fas fa-signature"></i>
                    <span>Logotipo</span>
                </a>
            </li>

            <li class="nav-item">
                <a class="nav-link" href="layouts.php">
                    <i class="fas fa-cubes"></i>
                    <span>Temas</span>
                </a>
            </li>
            <!--
            <li class="nav-item">
                <a class="nav-link" href="profile.php">
                    <i class="fas fa-user-shield"></i>
                    <span>Perfil</span>
                </a>
            </li>
            -->
            <div class="text-center d-none d-md-inline mt-4">
                <button class="rounded-circle border-0" id="sidebarToggle" style="background: var(--neon-green);"></button>
            </div>
        </ul>

        <div id="content-wrapper" class="d-flex flex-column">
            <div id="content">

                <?php
// ====================== POPUP ALERTA GLOBAL (REVENDAS) ======================
$alert_file = __DIR__ . '/../a/revenda_mensagem.json';
$alert_title = '';
$alert_text  = '';

if (file_exists($alert_file)) {
    $alert_data = json_decode(@file_get_contents($alert_file), true);
    if (is_array($alert_data)) {
        $alert_title = trim($alert_data['title'] ?? '');
        $alert_text  = trim($alert_data['content'] ?? '');
    }
}

// SÓ EXIBE SE: existir mensagem E se a sessão 'alerta_visto' NÃO estiver marcada
if ($alert_title !== '' && $alert_text !== '' && !isset($_SESSION['alerta_visto'])):
    // Marca a sessão para que não apareça ao navegar nos menus
    $_SESSION['alerta_visto'] = true;
?>
<style>
    #globalAlertBox { 
        background: #0a0f14 !important; 
        border: 2px solid #00ff88 !important; 
        box-shadow: 0 0 20px rgba(0, 255, 136, 0.3) !important;
    }
    #globalAlertTitle { color: #00ff88 !important; }
    #globalAlertText { border-left: 3px solid #ffcc00 !important; color: #fff !important; }
    #globalAlertOk { border: 1px solid #00ff88 !important; color: #00ff88 !important; }
    #globalAlertOk:hover { background: #00ff88 !important; color: #000 !important; }
    #globalAlertClose { border: 1px solid #ffcc00 !important; color: #ffcc00 !important; }
    #globalAlertClose:hover { background: #ffcc00 !important; color: #000 !important; }
</style>

<div id="globalAlertOverlay" aria-hidden="true" style="position: fixed; inset: 0; background: rgba(0,0,0,.78); display: none; align-items: center; justify-content: center; z-index: 999999; padding: 18px;">
    <div id="globalAlertBox" role="dialog" aria-modal="true" aria-labelledby="globalAlertTitle">
        <div id="globalAlertTitle">
            <i class="fas fa-bullhorn"></i>
            <span><?php echo htmlspecialchars($alert_title); ?></span>
        </div>
        <div id="globalAlertText"><?php echo htmlspecialchars($alert_text); ?></div>

        <div id="globalAlertActions">
            <button class="globalAlertBtn" id="globalAlertOk">Entendi</button>
            <button class="globalAlertBtn" id="globalAlertClose">Fechar</button>
        </div>
    </div>
</div>

<script>
(function() {
    var overlay = document.getElementById("globalAlertOverlay");
    var btnOk = document.getElementById("globalAlertOk");
    var btnClose = document.getElementById("globalAlertClose");

    function openPopup() {
        overlay.style.display = "flex";
        setTimeout(function() { overlay.classList.add("show"); }, 10);
        overlay.setAttribute("aria-hidden", "false");
    }

    function closePopup() {
        overlay.classList.remove("show");
        overlay.setAttribute("aria-hidden", "true");
        setTimeout(function() { overlay.style.display = "none"; }, 120);
    }

    // Abre o popup
    openPopup();

    btnOk.onclick = closePopup;
    btnClose.onclick = closePopup;

    overlay.onclick = function(e) { if (e.target === overlay) closePopup(); };
    document.addEventListener("keydown", function(e) {
        if (e.key === "Escape" && overlay.style.display === "flex") closePopup();
    });
})();
</script>
<?php endif; ?>
<!-- ==================== FIM POPUP ALERTA ==================== -->

                <nav class="navbar navbar-expand navbar-light topbar mb-4 static-top">
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3" style="color: var(--neon-green);">
                        <i class="fa fa-bars"></i>
                    </button>

                    <ul class="navbar-nav ml-auto align-items-center">
                        <li class="nav-item px-2">
                            <?php
                                $db1 = new SQLite3('../a/.eggziedb.db');
                                $result_mac = $db1->query("SELECT macs FROM revenda WHERE usuario = '$seller_name'");
                                $row_mac = $result_mac->fetchArray();
                                $macs = $row_mac['macs'] ?? '0';
                            ?>
                            <span class="badge badge-macs">
                                <i class="fas fa-microchip mr-2"></i><?php echo $macs; ?> MACs
                            </span>
                        </li>

                        <div class="topbar-divider d-none d-sm-block" style="border-right: 1px solid var(--glass-border);"></div>

                        <li class="nav-item">
                            <a class="nav-link btn-logout-top" href="../logout.php">
                                <span>Logout</span>
                                <i class="fas fa-power-off ml-2"></i>
                            </a>
                        </li>
                    </ul>
                </nav>
