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

// Verifique se está logado como superadmin
if (!isset($_SESSION['superadmin_id'])) {
    header("Location: login.php");
    exit;
}

// Consulta para buscar os totais da tabela users
$stmt_total = $pdo->query("SELECT COUNT(*) FROM users");
$total_usuarios = $stmt_total->fetchColumn();

$stmt_bloqueados = $pdo->query("SELECT COUNT(*) FROM users WHERE active = 0");
$total_bloqueados = $stmt_bloqueados->fetchColumn();

$stmt_liberados = $pdo->query("SELECT COUNT(*) FROM users WHERE active = 1");
$total_liberados = $stmt_liberados->fetchColumn();

// Card de ganho estimado proximo mês (considera todos os usuários ativos)
$valor_mensalidade = 10.99;
$ganho_estimado_prox_mes = $total_liberados * $valor_mensalidade;
$ganho_estimado_prox_mes_formatado = 'R$ ' . number_format($ganho_estimado_prox_mes, 2, ',', '.');
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Painel Super Admin - Dashboard</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@700;400&display=swap" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        body {margin:0;font-family:'Montserrat',Arial,sans-serif;background:#F5FAFF;}
        .sidebar {
            position:fixed;left:0;top:0;bottom:0;width:220px;background:#1876F2;color:#fff;display:flex;flex-direction:column;align-items:center;padding-top:32px;box-shadow:2px 0 12px rgba(24,118,242,0.10);z-index:100;
        }
        .sidebar .logo {
            font-size:1.6rem;font-weight:700;margin-bottom:34px;letter-spacing:0.03em;
        }
        .sidebar nav {width:100%;}
        .sidebar nav a {
            display:flex;align-items:center;gap:13px;color:#fff;text-decoration:none;font-size:1.08rem;font-weight:600;padding:12px 28px;margin:2px 0;border-radius:8px 0 0 8px;transition:background 0.17s;
        }
        .sidebar nav a.active, .sidebar nav a:hover {background:#1356b3;}
        .sidebar .bottom {margin-top:auto;margin-bottom:34px;}
        .sidebar .logout-link {color:#eaf4ff;font-weight:700;font-size:1.02rem;text-decoration:underline;background:none;border:none;cursor:pointer;padding:0;}
        .main-content {margin-left:220px;padding:38px 18px;max-width:1100px;}
        .title-row {display:flex;align-items:center;gap:14px;margin-bottom:24px;}
        .title-row .fa-gauge {font-size:2.1rem;color:#1876F2;}
        .title-row h2 {font-size:1.5rem;font-weight:700;color:#212a3e;margin:0;}
        .cards-row {display:flex;gap:32px;flex-wrap:wrap;margin-top:18px;}
        .card {
            flex:1 1 240px;
            background:#fff;
            border-radius:13px;
            box-shadow:0 2px 14px rgba(24,118,242,0.07);
            padding:32px 22px 26px 22px;
            display:flex;
            flex-direction:column;
            align-items:center;
            justify-content:center;
            min-width:200px;
            max-width:320px;
            margin-bottom:18px;
        }
        .card .card-icon {
            font-size:2.4rem;
            margin-bottom:15px;
        }
        .card .card-title {
            color:#1876F2;
            font-weight:700;
            font-size:1.07rem;
            margin-bottom:8px;
        }
        .card .card-value {
            font-size:2.1rem;
            font-weight:700;
            color:#212a3e;
        }
        .card-total    { border-top:4px solid #1876F2; }
        .card-bloq     { border-top:4px solid #d9534f; }
        .card-liberado { border-top:4px solid #1bbf5c; }
        .card-ganho    { border-top:4px solid #ffc107; }
        @media(max-width:900px) {
            .main-content{margin-left:0;padding:15px 3vw;}
            .sidebar{position:relative;width:100%;height:auto;box-shadow:none;padding:14px 0;}
            .sidebar .logo{font-size:1.2rem;}
            .sidebar nav{gap:5px;width:auto;display:flex;}
            .sidebar nav a{font-size:0.99rem;padding:8px 14px;}
            .cards-row{flex-direction:column;}
        }
    </style>
</head>
<body>
    <aside class="sidebar">
        <div class="logo">
            <i class="fa-solid fa-shield-halved"></i> Fiado Fácil
        </div>
        <nav>
            <a href="dashboard.php" class="active"><i class="fa-solid fa-gauge"></i> Dashboard</a>
            <a href="usuarios.php"><i class="fa-solid fa-user"></i> Usuários</a>
			<a href="cobrancas.php"><i class="fa-solid fa-users"></i> Cobranças</a>
        </nav>
        <div class="bottom">
            <a href="logout.php" class="logout-link"><i class="fa-solid fa-right-from-bracket"></i> Sair</a>
        </div>
    </aside>
    <div class="main-content">
        <div class="title-row">
            <i class="fa-solid fa-gauge"></i>
            <h2>Dashboard Super Admin</h2>
        </div>
        <div class="cards-row">
            <div class="card card-total">
                <div class="card-icon"><i class="fa-solid fa-users"></i></div>
                <div class="card-title">Total Usuários</div>
                <div class="card-value"><?= $total_usuarios ?></div>
            </div>
            <div class="card card-bloq">
                <div class="card-icon"><i class="fa-solid fa-user-lock"></i></div>
                <div class="card-title">Usuários Bloqueados</div>
                <div class="card-value"><?= $total_bloqueados ?></div>
            </div>
            <div class="card card-liberado">
                <div class="card-icon"><i class="fa-solid fa-user-check"></i></div>
                <div class="card-title">Usuários Liberados</div>
                <div class="card-value"><?= $total_liberados ?></div>
            </div>
            <div class="card card-ganho">
                <div class="card-icon"><i class="fa-solid fa-sack-dollar"></i></div>
                <div class="card-title">Ganho Estimado Próx. Mês</div>
                <div class="card-value"><?= $ganho_estimado_prox_mes_formatado ?></div>
            </div>
        </div>
    </div>
</body>
</html>