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

$db = new SQLite3('./a/.eggziedb.db');

// Estatísticas
$total_users = $db->querySingle("SELECT COUNT(*) FROM ibo") ?: 0;
$total_links = $db->querySingle("SELECT COUNT(*) FROM client_links") ?: 0;
$expired_users = $db->querySingle("SELECT COUNT(*) FROM ibo WHERE expire_date < date('now')") ?: 0;
$active_users = $total_users - $expired_users;

// Últimos usuários
$recent_users = $db->query("SELECT * FROM ibo ORDER BY id DESC LIMIT 5");

include 'includes/header.php';
?>

<style>
    :root {
        --primary: #00BFFF; /* Azul Celeste */
        --primary-dark: #4682B4; /* Azul Metálico */
        --bg-dark: #0a192f;
        --card-bg: rgba(17, 34, 64, 0.7);
        --text-main: #e6f1ff;
        --text-muted: #8892b0;
        --glow: 0 0 15px rgba(0, 191, 255, 0.4);
    }

    .dashboard-container {
        padding: 1.5rem;
        color: var(--text-main);
        animation: fadeIn 0.5s ease-in-out;
    }

    @keyframes fadeIn {
        from { opacity: 0; transform: translateY(10px); }
        to { opacity: 1; transform: translateY(0); }
    }

    .welcome-section {
        margin-bottom: 2rem;
        text-align: left;
    }

    .welcome-section h1 {
        font-size: 2.2rem;
        font-weight: 800;
        background: linear-gradient(90deg, var(--primary), var(--primary-dark));
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        margin-bottom: 0.5rem;
    }

    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        gap: 1.2rem;
        margin-bottom: 2.5rem;
    }

    .stat-card {
        background: var(--card-bg);
        padding: 1.5rem;
        border-radius: 20px;
        border: 1px solid rgba(0, 191, 255, 0.1);
        backdrop-filter: blur(10px);
        transition: all 0.3s ease;
        display: flex;
        flex-direction: column;
        justify-content: center;
        position: relative;
        overflow: hidden;
    }

    .stat-card::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 3px;
        background: linear-gradient(90deg, transparent, var(--primary), transparent);
        transform: scaleX(0);
        transition: transform 0.3s ease;
    }

    .stat-card:hover {
        transform: translateY(-5px);
        border-color: var(--primary);
        box-shadow: var(--glow);
    }

    .stat-card:hover::after {
        transform: scaleX(1);
    }

    .stat-card h3 {
        color: var(--text-muted);
        font-size: 0.85rem;
        text-transform: uppercase;
        letter-spacing: 1.5px;
        margin-bottom: 0.8rem;
    }

    .stat-card .value {
        font-size: 2.5rem;
        font-weight: 800;
        color: var(--primary);
        text-shadow: 0 0 10px rgba(0, 191, 255, 0.3);
    }

    .content-grid {
        display: grid;
        grid-template-columns: 2fr 1fr;
        gap: 1.5rem;
    }

    @media (max-width: 992px) {
        .content-grid {
            grid-template-columns: 1fr;
        }
        .welcome-section h1 {
            font-size: 1.8rem;
        }
    }

    .panel-card {
        background: var(--card-bg);
        padding: 1.5rem;
        border-radius: 20px;
        border: 1px solid rgba(0, 191, 255, 0.1);
        backdrop-filter: blur(10px);
    }

    .panel-card h2 {
        margin-bottom: 1.5rem;
        color: var(--primary);
        font-size: 1.3rem;
        font-weight: 700;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .table-responsive {
        width: 100%;
        overflow-x: auto;
    }

    table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0 8px;
    }

    th {
        text-align: left;
        color: var(--text-muted);
        font-size: 0.75rem;
        padding: 1rem;
        text-transform: uppercase;
        letter-spacing: 1px;
    }

    td {
        padding: 1rem;
        background: rgba(255, 255, 255, 0.03);
        font-size: 0.9rem;
    }

    td:first-child { border-radius: 12px 0 0 12px; }
    td:last-child { border-radius: 0 12px 12px 0; }

    .status-badge {
        padding: 0.4rem 1rem;
        border-radius: 50px;
        font-size: 0.7rem;
        font-weight: 800;
        letter-spacing: 0.5px;
    }

    .status-active { background: rgba(0, 191, 255, 0.1); color: var(--primary); border: 1px solid var(--primary); }
    .status-expired { background: rgba(255, 82, 82, 0.1); color: #ff5252; border: 1px solid #ff5252; }

    .quick-actions {
        display: grid;
        grid-template-columns: 1fr;
        gap: 1rem;
    }

    .action-btn {
        display: flex;
        align-items: center;
        gap: 1.2rem;
        padding: 1.2rem;
        background: rgba(0, 191, 255, 0.05);
        border-radius: 16px;
        text-decoration: none !important;
        color: var(--text-main);
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        border: 1px solid transparent;
    }

    .action-btn:hover {
        background: rgba(0, 191, 255, 0.12);
        transform: translateX(8px);
        border-color: var(--primary);
        box-shadow: var(--glow);
        color: var(--primary);
    }

    .action-btn i {
        background: linear-gradient(135deg, var(--primary), var(--primary-dark));
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        font-size: 1.4rem;
    }
</style>

<div class="dashboard-container">
    <div class="welcome-section">
        <h1>Dashboard IBO-NOTE</h1>
        <p>Bem-vindo ao seu painel de controle otimizado.</p>
    </div>

    <div class="stats-grid">
        <div class="stat-card">
            <h3>Clientes Totais</h3>
            <div class="value"><?php echo $total_users; ?></div>
        </div>
        <div class="stat-card">
            <h3>Ativos Agora</h3>
            <div class="value"><?php echo $active_users; ?></div>
        </div>
        <div class="stat-card">
            <h3>Expirados</h3>
            <div class="value" style="color: #ff5252;"><?php echo $expired_users; ?></div>
        </div>
        <div class="stat-card">
            <h3>Faturamento (Links)</h3>
            <div class="value"><?php echo $total_links; ?></div>
        </div>
    </div>

    <div class="content-grid">
        <div class="panel-card">
            <h2><i class="fas fa-history"></i> Atividades Recentes</h2>
            <div class="table-responsive">
                <table>
                    <thead>
                        <tr>
                            <th>MAC Address</th>
                            <th>Usuário</th>
                            <th>Expiração</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if ($recent_users): ?>
                            <?php while($row = $recent_users->fetchArray(SQLITE3_ASSOC)): 
                                $is_expired = strtotime($row['expire_date']) < time();
                            ?>
                            <tr>
                                <td><code><?php echo htmlspecialchars($row['mac_address']); ?></code></td>
                                <td><strong><?php echo htmlspecialchars($row['username']); ?></strong></td>
                                <td><?php echo date('d/m/Y', strtotime($row['expire_date'])); ?></td>
                                <td>
                                    <span class="status-badge <?php echo $is_expired ? 'status-expired' : 'status-active'; ?>">
                                        <?php echo $is_expired ? 'EXPIRADO' : 'ATIVO'; ?>
                                    </span>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr><td colspan="4" style="text-align:center;">Nenhum usuário encontrado.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="panel-card">
            <h2><i class="fas fa-bolt"></i> Acesso Rápido</h2>
            <div class="quick-actions">
                <a href="users_create.php" class="action-btn">
                    <i class="fas fa-user-plus"></i>
                    <span>Adicionar Novo Cliente</span>
                </a>
                <a href="users.php" class="action-btn">
                    <i class="fas fa-users-cog"></i>
                    <span>Gerenciar Todos Clientes</span>
                </a>
                <a href="create_link.php" class="action-btn">
                    <i class="fas fa-credit-card"></i>
                    <span>Gerar Link de Pagamento</span>
                </a>
                <a href="layouts.php" class="action-btn">
                    <i class="fas fa-paint-brush"></i>
                    <span>Temas e Layouts</span>
                </a>
            </div>
        </div>
    </div>
</div>

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