<?php 
session_start();
include 'auth.php'; 

// --- CONEXÃO DIRETA COM O NOVO BANCO (SQLITE) ---
// Substituindo o include ('api/conexao_rev.php') pela conexão direta
if (!file_exists('api')) { @mkdir('api', 0777, true); }
$conn_rev = new SQLite3("api/database.db");

// Ajuste de Fuso Horário
date_default_timezone_set('America/Sao_Paulo');

// --- INTEGRANDO BANCO DE VENCIMENTO ---
$db_venc_path = __DIR__ . '/api/db/studiolivecode_vencimento.db';
$data_vencimento = "Não definida";
try {
    if (file_exists($db_venc_path)) {
        $db_venc = new PDO("sqlite:" . $db_venc_path);
        $res_v = $db_venc->query("SELECT vencimento FROM vencimento WHERE id=1");
        if ($res_v) {
            $row_v = $res_v->fetch(PDO::FETCH_ASSOC);
            $data_vencimento = date('d/m/Y', strtotime($row_v['vencimento']));
        }
    }
} catch(Exception $e) { $data_vencimento = "Erro ao ler"; }

// --- INTEGRANDO BANCO DE CRÉDITOS ---
$db_cred_path = __DIR__ . '/api/db/studiolivecode_creditos.db';
$saldo_display = "0";
try {
    if (file_exists($db_cred_path)) {
        $db_cred = new PDO("sqlite:" . $db_cred_path);
        $res_c = $db_cred->query("SELECT saldo FROM carteira WHERE id=1");
        if ($res_c) {
            $row_c = $res_c->fetch(PDO::FETCH_ASSOC);
            $valor_db = $row_c['saldo'];
            if ($valor_db == -1) { $saldo_display = "ILIMITADOS"; } else { $saldo_display = $valor_db; }
        }
    }
} catch(Exception $e) { $saldo_display = "0"; }

// --- CONTAGEM DE REVENDAS (CORRIGIDO PARA SQLITE3) ---
$total_revendas = 0; $total_masters = 0;
// No SQLite3, usamos fetchArray() em vez de fetch_assoc()
$query_rev = $conn_rev->query("SELECT COUNT(*) as total, SUM(CASE WHEN nivel_rev = 2 THEN 1 ELSE 0 END) as masters FROM dados_rev");
if($query_rev){
    $row_rev = $query_rev->fetchArray(SQLITE3_ASSOC);
    $total_revendas = $row_rev['total'] ?? 0;
    $total_masters = $row_rev['masters'] ?? 0;
}

// Data Atual
$data_hoje = date('d/m/Y');
$dias = ['DOMINGO','SEGUNDA-FEIRA','TERÇA-FEIRA','QUARTA-FEIRA','QUINTA-FEIRA','SEXTA-FEIRA','SÁBADO'];
$dia_semana = $dias[date('w')];

include ('includes/header.php');
?>

<style>
    /* DESIGN PREMIUM - DEUS DA GUERRA SCRIPT */
    html, body, .wrapper, .main-panel, .content, .container-fluid { 
        background-color: #020617 !important; 
        color: #e2e8f0 !important;
        overflow-x: hidden;
    }
    .lua-icon, .fa-moon, .fa-sun, .navbar, #dark-mode-switch { display: none !important; }
    .container-fluid { padding: 15px !important; }
    .premium-header {
        background: linear-gradient(135deg, #0f172a 0%, #020617 100%);
        padding: 25px 20px; border-radius: 20px; border: 1px solid #1e293b; margin-bottom: 25px;
        box-shadow: 0 10px 25px rgba(0,0,0,0.3);
    }
    .welcome-text h1 { font-size: 1.5rem; font-weight: 800; color: #fff; margin: 0; }
    .welcome-text p { color: #64748b; font-size: 0.85rem; margin-top: 5px; }
    .header-date {
        margin-top: 15px; padding-top: 15px; border-top: 1px solid rgba(255,255,255,0.05);
        display: flex; justify-content: space-between; align-items: center;
    }
    
    .date-text { color: #fff !important; font-weight: 800; font-size: 0.75rem; text-transform: uppercase; }
    .label-blue, .label-green, .label-gold { 
        color: #fff !important; 
        font-size: 0.75rem; 
        font-weight: 800; 
        text-transform: uppercase; 
        display: flex; 
        align-items: center; 
        gap: 6px; 
    }
    .value-white, .value-green { color: #fff !important; font-weight: 800; font-size: 0.85rem; }

    .info-container {
        margin-top: 12px; padding-top: 12px; border-top: 1px solid rgba(255,255,255,0.05);
        display: flex; justify-content: space-between; align-items: center; width: 100%;
    }

    @keyframes wiggle {
        0% { transform: rotate(0deg); }
        25% { transform: rotate(-10deg); }
        50% { transform: rotate(10deg); }
        75% { transform: rotate(-10deg); }
        100% { transform: rotate(0deg); }
    }

    .info-container i, .header-date i {
        color: #fff !important;
        animation: wiggle 2s ease-in-out infinite;
        display: inline-block;
    }

    .nav-item-premium i, .icon-circle i {
        color: #ffaa00 !important;
        animation: wiggle 2s ease-in-out infinite;
        display: inline-block;
    }

    .stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
    .stat-card-premium {
        background: #070e1b !important; border: 1px solid #1e293b !important;
        border-radius: 18px; padding: 20px; text-align: center;
    }
    .icon-circle { 
        width: 50px; height: 50px; border-radius: 50%; margin: 0 auto 12px; 
        display: flex; align-items: center; justify-content: center; 
        font-size: 1.2rem; background: rgba(255, 170, 0, 0.05); 
    }
    .stat-card-premium h3 { font-size: 1.6rem; font-weight: 800; margin: 0; color: #fff; }
    .stat-card-premium span { font-size: 0.7rem; color: #64748b; text-transform: uppercase; font-weight: 700; }

    .nav-grid { display: grid; grid-template-columns: 1fr; gap: 12px; margin-top: 25px; }
    .nav-item-premium {
        background: #0f172a !important; border: 1px solid #1e293b !important;
        padding: 18px; border-radius: 15px; display: flex; align-items: center; gap: 15px; text-decoration: none !important;
    }
    .nav-item-premium i { width: 42px; height: 42px; background: rgba(255,170,0,0.05); border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 1.1rem; }
    .nav-info { display: flex; flex-direction: column; align-items: flex-start; }
    .nav-info strong { color: #fff; font-size: 0.95rem; font-weight: 700; display: block; }
    .nav-info span { color: #475569; font-size: 0.75rem; display: block; margin-top: 2px; }
    .section-label { color: #3b82f6; font-size: 0.75rem; font-weight: 800; text-transform: uppercase; margin: 30px 5px 12px; }

    @media (min-width: 768px) { .nav-grid { grid-template-columns: 1fr 1fr; } }
</style>

<div class="container-fluid">
    <div class="premium-header">
        <div class="welcome-text">
            <h1>Olá, Panaci Arts 👋</h1>
            <p>Bem-vindo ao seu painel de controle</p>
        </div>
        
        <div class="header-date">
            <div class="date-text"><?php echo $dia_semana; ?></div>
            <div style="font-weight: 800; color: #fff; font-size: 0.9rem;"><?php echo $data_hoje; ?></div>
        </div>

        <div class="info-container">
            <div class="label-blue"><i class="fas fa-calendar-alt"></i> VENCIMENTO:</div>
            <div class="value-white"><?php echo $data_vencimento; ?></div>
        </div>

        <div class="info-container">
            <div class="label-green"><i class="fas fa-dollar-sign"></i> CRÉDITOS:</div>
            <div class="value-white"><?php echo $saldo_display; ?></div>
        </div>

        <div class="info-container">
            <div class="label-gold"><i class="fas fa-crown"></i> REVENDAS MASTER:</div>
            <div class="value-white"><?php echo $total_masters; ?></div>
        </div>
    </div>

    <div class="stat-row">
        <div class="stat-card-premium">
            <div class="icon-circle"><i class="fas fa-users"></i></div>
            <h3><?php echo $total_revendas; ?></h3>
            <span>Revendas</span>
        </div>
        <div class="stat-card-premium">
            <div class="icon-circle"><i class="fas fa-gem"></i></div>
            <h3><?php echo $total_masters; ?></h3>
            <span>Sub-Revendas</span>
        </div>
    </div>

    <div class="section-label">Customização App</div>
    <div class="nav-grid">
        <a href="panaci_gerenciar_notemac.php" class="nav-item-premium" style="border-left: 3px solid #3b82f6;">
            <i class="fas fa-rocket" style="color: #ffaa00 !important;"></i>
            <div class="nav-info"><strong>Pop-up Automático</strong><span>Configurar aviso de 10 segundos</span></div>
        </a>

        <a href="logo.php" class="nav-item-premium" style="border-left: 3px solid #ffaa00;">
            <i class="fas fa-image"></i>
            <div class="nav-info"><strong>Logo Principal</strong><span>Alterar logo do aplicativo</span></div>
        </a>
        <a href="logo2.php" class="nav-item-premium" style="border-left: 3px solid #ffaa00;">
            <i class="fas fa-image"></i>
            <div class="nav-info"><strong>Logo Avatar</strong><span>Alterar logo do aplicativo 2</span></div>
        </a>
        <a href="panaci_manual_jogos.php" class="nav-item-premium" style="border-left: 3px solid #ffaa00;">
            <i class="fas fa-futbol"></i>
            <div class="nav-info"><strong>Futebol P2P</strong><span>Gerenciar jogos e transmissões</span></div>
        </a>
        <a href="Image.php" class="nav-item-premium" style="border-left: 3px solid #ffaa00;">
            <i class="fas fa-paint-brush"></i>
            <div class="nav-info"><strong>Image Background</strong><span>Gerenciar fundo do aplicativo</span></div>
        </a>
        <a href="Image2.php" class="nav-item-premium" style="border-left: 3px solid #ffaa00;">
            <i class="fas fa-fill-drip"></i>
            <div class="nav-info"><strong>Gradient & BG2</strong><span>Gerenciar cores e gradientes</span></div>
        </a>
        <a href="layouts.php" class="nav-item-premium" style="border-left: 3px solid #ffaa00;">
            <i class="fas fa-layer-group"></i>
            <div class="nav-info"><strong>Layouts App</strong><span>Configurar estilos visuais</span></div>
        </a>
    </div>

    <div class="section-label">Controle Geral</div>
    <div class="nav-grid">
        <a href="update.php" class="nav-item-premium" style="border-left: 3px solid #d946ef;">
            <i class="fas fa-sync-alt"></i>
            <div class="nav-info"><strong>Atualização do App</strong><span>Gerenciar versão e link do APK</span></div>
        </a>
        <a href="gerenciar_creditos.php" class="nav-item-premium" style="border-left: 3px solid #22c55e;">
            <i class="fas fa-wallet"></i>
            <div class="nav-info"><strong>Adicionar Créditos</strong><span>Gerenciar saldo disponível</span></div>
        </a>
        <a href="gerenciar_vencimento.php" class="nav-item-premium" style="border-left: 3px solid #3b82f6;">
            <i class="fas fa-history"></i>
            <div class="nav-info"><strong>Renovação do Painel</strong><span>Gerenciar validade e expiração</span></div>
        </a>
        <a href="logs_rev.php" class="nav-item-premium" style="border-left: 3px solid #f59e0b;">
            <i class="fas fa-eye"></i>
            <div class="nav-info"><strong>Logs do Sistema</strong><span>Monitorar ações e revendas</span></div>
        </a>
        <a href="rev/v_logs.php" class="nav-item-premium" style="border-left: 3px solid #3b82f6;">
            <i class="fas fa-user-secret"></i>
            <div class="nav-info"><strong>Logs de Bisbilhoteiros</strong><span>Ver quem acessou o painel</span></div>
        </a>
        <a href="qrcode.php" class="nav-item-premium">
            <i class="fas fa-qrcode"></i>
            <div class="nav-info"><strong>Configurar QR Code</strong><span>Gerenciar recebimentos Pix</span></div>
        </a>
        <a href="gerenciar_planos.php" class="nav-item-premium">
            <i class="fas fa-gem"></i>
            <div class="nav-info"><strong>Planos & QR Code Pix</strong><span>Configurar valores e imagens API</span></div>
        </a>
        <a href="ads.php" class="nav-item-premium">
            <i class="fas fa-th-large"></i>
            <div class="nav-info"><strong>Painel de Anúncios 1</strong><span>Gerenciar imagens e ativos</span></div>
        </a>
        <a href="ads2.php" class="nav-item-premium">
            <i class="fas fa-film"></i>
            <div class="nav-info"><strong>Painel de Anúncios 2</strong><span>Gerenciar vídeos e campanhas</span></div>
        </a>
        <a href="ads3.php" class="nav-item-premium">
            <i class="fas fa-tv"></i>
            <div class="nav-info"><strong>Painel de Anúncios 3</strong><span>Gerenciar banners informativos</span></div>
        </a>
        <a href="edit_texto.php" class="nav-item-premium">
            <i class="fas fa-bullhorn"></i>
            <div class="nav-info"><strong>Avisos do Sistema</strong><span>Editar textos de alertas no App</span></div>
        </a>
        <a href="users.php" class="nav-item-premium">
            <i class="fas fa-user-check"></i>
            <div class="nav-info"><strong>Gestão de Usuários</strong><span>Ativar e remover acessos</span></div>
        </a>
        <a href="gerenciar_rev.php" class="nav-item-premium" style="border-left: 3px solid #a855f7;">
            <i class="fas fa-users-cog"></i>
            <div class="nav-info"><strong>Gestão de Revendedores</strong><span>Administrar sua rede de revenda</span></div>
        </a>
    </div>

    <div class="section-label">Links de Saída (API)</div>
    <div class="nav-grid">
        <a href="api/bg2.php" target="_blank" class="nav-item-premium">
            <i class="fas fa-link"></i>
            <div class="nav-info"><strong>Link BG2 API</strong><span>Link direto para fundo do App</span></div>
        </a>
        <a href="api/allads.php" target="_blank" class="nav-item-premium">
            <i class="fas fa-external-link-alt"></i>
            <div class="nav-info"><strong>Visualizar Link 1</strong><span>Link de Anúncios 1</span></div>
        </a>
        <a href="api/allads2.php" target="_blank" class="nav-item-premium">
            <i class="fas fa-external-link-alt"></i>
            <div class="nav-info"><strong>Visualizar Link 2</strong><span>Link de Anúncios 2</span></div>
        </a>
        <a href="api/allads3.php" target="_blank" class="nav-item-premium">
            <i class="fas fa-external-link-alt"></i>
            <div class="nav-info"><strong>Visualizar Link 3</strong><span>Link de Anúncios 3</span></div>
        </a>
    </div>
</div>

<?php include ('includes/footer.php');?>