<?php 
session_start();
if(!isset($_SESSION['rev_logado'])){ header("Location: index_rev.php"); exit(); }
include('../api/conexao_rev.php'); 

$id_rev = $_SESSION['rev_logado'];
$sql = $conn_rev->query("SELECT * FROM dados_rev WHERE id_rev = '$id_rev'");
$dados = $sql->fetch_assoc();

$contar_cli = $conn_rev->query("SELECT COUNT(*) as total FROM clientes_rev WHERE id_rev_pai = '$id_rev'");
$total_cli = $contar_cli->fetch_assoc()['total'];

$total_subs = 0;
if($dados['nivel_rev'] == 2){
    $contar_subs = $conn_rev->query("SELECT COUNT(*) as total FROM dados_rev WHERE id_pai = '$id_rev'");
    $total_subs = $contar_subs->fetch_assoc()['total'];
}

date_default_timezone_set('America/Sao_Paulo');
$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')];
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Revenda</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        * { box-sizing: border-box; }
        html, body { background-color: #020617 !important; color: #e2e8f0 !important; font-family: 'Segoe UI', sans-serif; margin: 0; padding: 0; overflow-x: hidden; }
        
        .container-fluid { 
            padding: 20px; 
            max-width: 800px; 
            margin: 0 auto;
        }

        .premium-header { 
            background: linear-gradient(135deg, #0f172a 0%, #020617 100%); 
            padding: 25px; 
            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; }

        .header-date { margin-top: 15px; padding-top: 15px; border-top: 1px solid rgba(255,255,255,0.05); display: flex; justify-content: space-between; }
        .date-text { color: #3b82f6; font-weight: 700; font-size: 0.75rem; text-transform: uppercase; }

        .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; }
        .label-green { color: #22c55e; font-size: 0.75rem; font-weight: 700; }

        .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; }
        
        .blue-glow { background: rgba(59, 130, 246, 0.1); color: #3b82f6; }
        .green-glow { background: rgba(34, 197, 94, 0.1); color: #22c55e; }
        .gold-glow { background: rgba(241, 196, 15, 0.1); color: #f1c40f; }

        .section-label { color: #3b82f6; font-size: 0.75rem; font-weight: 800; text-transform: uppercase; margin: 30px 5px 12px; letter-spacing: 1px; }

        .nav-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
        @media (min-width: 600px) { .nav-grid { grid-template-columns: 1fr 1fr; } }

        .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;
            transition: 0.2s;
        }
        .nav-item-premium:active { transform: scale(0.98); background: #131c31 !important; }

        .nav-item-premium i { 
            width: 42px; height: 42px; 
            background: rgba(255,255,255,0.03); 
            border-radius: 10px; 
            display: flex; 
            align-items: center; 
            justify-content: center; 
            color: #3b82f6; 
            font-size: 1.1rem;
        }
        
        .nav-info strong { color: #fff; font-size: 0.95rem; display: block; }
        .nav-info span { color: #475569; font-size: 0.75rem; }

        .logout-container {
            width: 100%;
            margin-top: 25px;
            padding-bottom: 30px;
        }

        .btn-sair { 
            width: 100%; 
            background: rgba(239, 68, 68, 0.1); 
            color: #ef4444; 
            padding: 18px; 
            border-radius: 15px; 
            text-align: center; 
            text-decoration: none; 
            font-weight: 700; 
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            border: 1px solid rgba(239, 68, 68, 0.2); 
            transition: 0.3s;
        }
        .btn-sair:active { background: rgba(239, 68, 68, 0.2); }
    </style>
</head>
<body>

<div class="container-fluid">
    <div class="premium-header">
        <div class="welcome-text">
            <h1>Olá, <?php echo $dados['login_rev']; ?> 👋</h1>
            <p>Painel de Controle da Revenda</p>
        </div>
        <div class="header-date">
            <div class="date-text"><?php echo $dia_semana; ?></div>
            <div style="font-weight: 700; color: #fff;"><?php echo $data_hoje; ?></div>
        </div>
        <div class="info-container">
            <div class="label-green"><i class="fas fa-wallet"></i> MEUS CRÉDITOS:</div>
            <div style="color: #22c55e; font-weight: 700; font-size: 1.1rem;"><?php echo $dados['creditos_rev']; ?></div>
        </div>
    </div>

    <div class="stat-row">
        <div class="stat-card-premium">
            <div class="icon-circle blue-glow"><i class="fas fa-users"></i></div>
            <h3 style="margin:0; color:#fff;"><?php echo $total_cli; ?></h3>
            <span style="font-size: 0.7rem; color: #64748b; font-weight:700;">MEUS CLIENTES</span>
        </div>
        <div class="stat-card-premium">
            <div class="icon-circle <?php echo ($dados['nivel_rev'] == 2) ? 'gold-glow' : 'green-glow'; ?>"><i class="fas <?php echo ($dados['nivel_rev'] == 2) ? 'fa-sitemap' : 'fa-check-circle'; ?>"></i></div>
            <h3 style="margin:0; color:#fff;"><?php echo ($dados['nivel_rev'] == 2) ? $total_subs : 'Ativa'; ?></h3>
            <span style="font-size: 0.7rem; color: #64748b; font-weight:700;">STATUS CONTA</span>
        </div>
    </div>

    <div class="section-label">Gestão de Vendas</div>
    <div class="nav-grid">
        <a href="clientes_rev.php" class="nav-item-premium" style="border-left: 3px solid #3b82f6;">
            <i class="fas fa-user-plus"></i>
            <div class="nav-info"><strong>Gerenciar Clientes</strong><span>Criar e renovar acessos</span></div>
        </a>
        <?php if($dados['nivel_rev'] == 2): ?>
        <a href="subrevendas_rev.php" class="nav-item-premium" style="border-left: 3px solid #f1c40f;">
            <i class="fas fa-users-cog" style="color: #f1c40f;"></i>
            <div class="nav-info"><strong>Sub-Revendedores</strong><span>Gerenciar sua rede</span></div>
        </a>
        <?php endif; ?>
    </div>

    <div class="section-label">Personalização App</div>
    <div class="nav-grid">
        <a href="layouts_temas_rev.php" class="nav-item-premium" style="border-left: 3px solid #8b5cf6;">
            <i class="fas fa-magic" style="color: #8b5cf6;"></i>
            <div class="nav-info"><strong>Layouts e Temas</strong><span>Trocar aparência do App</span></div>
        </a>

        <a href="panaci_rev_sub_logo_gerenciar.php" class="nav-item-premium" style="border-left: 3px solid #3b82f6;">
            <i class="fas fa-image"></i>
            <div class="nav-info"><strong>Gerenciar Logo</strong><span>Alterar logo da revenda</span></div>
        </a>

        <a href="panaci_rev_sub_bg_gerenciar.php" class="nav-item-premium" style="border-left: 3px solid #8b5cf6;">
            <i class="fas fa-desktop"></i>
            <div class="nav-info"><strong>Background</strong><span>Fundo de tela do App</span></div>
        </a>

        <a href="banner_rev.php" class="nav-item-premium" style="border-left: 3px solid #10b981;">
            <i class="fas fa-ad"></i>
            <div class="nav-info"><strong>Banners</strong><span>Slides e avisos</span></div>
        </a>

        <a href="msg_rev.php" class="nav-item-premium" style="border-left: 3px solid #f59e0b;">
            <i class="fas fa-comment-alt"></i>
            <div class="nav-info"><strong>Mensagens</strong><span>Textos do App</span></div>
        </a>

        <a href="panaci_rev_sub_qr_gerenciar.php" class="nav-item-premium" style="border-left: 3px solid #3b82f6;">
            <i class="fas fa-qrcode"></i>
            <div class="nav-info"><strong>QR-Codes</strong><span>Pagamentos e PIX</span></div>
        </a>
    </div>

    <div class="section-label">Configurações</div>
    <div class="nav-grid">
        <a href="seguranca_rev.php" class="nav-item-premium" style="border-left: 3px solid #f43f5e;">
            <i class="fas fa-shield-alt" style="color: #f43f5e;"></i>
            <div class="nav-info">
                <strong>Segurança</strong>
                <span>Alterar minha senha</span>
            </div>
        </a>
    </div>

    <div class="logout-container">
        <a href="sair_rev.php" class="btn-sair">
            <i class="fas fa-sign-out-alt"></i> 
            SAIR DO PAINEL
        </a>
    </div>
</div>
</body>
</html>