<?php
/**
 * UPGRADE PREMIUM BY MANUS AI
 * DASHBOARD DESIGN: MODERN ANALYTICS
 */
session_start();
if (!isset($_SESSION['eggzie_iboPlayer'])) {
    header("Location: login.php");
    exit;
}

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

// Estatísticas Reais
$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 6");

include 'includes/header.php';
?>

<style>
    .dashboard-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
        gap: 25px;
        margin-bottom: 40px;
    }

    .stat-card {
        background: var(--card-bg);
        backdrop-filter: blur(10px);
        padding: 25px;
        border-radius: 24px;
        border: 1px solid var(--border);
        display: flex;
        flex-direction: column;
        gap: 15px;
        transition: var(--transition);
        position: relative;
        overflow: hidden;
    }

    .stat-card:hover {
        transform: translateY(-5px);
        border-color: var(--primary);
        box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    }

    .stat-card::after {
        content: '';
        position: absolute;
        right: -20px;
        bottom: -20px;
        width: 100px;
        height: 100px;
        background: var(--card-accent);
        opacity: 0.05;
        border-radius: 50%;
    }

    .stat-icon {
        width: 48px;
        height: 48px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
    }

    .stat-info h3 {
        font-size: 13px;
        text-transform: uppercase;
        letter-spacing: 1px;
        color: var(--text-muted);
        margin-bottom: 5px;
    }

    .stat-value {
        font-size: 32px;
        font-weight: 800;
        color: white;
    }

    .main-grid {
        display: grid;
        grid-template-columns: 2fr 1fr;
        gap: 25px;
    }

    @media (max-width: 1200px) {
        .main-grid {
            grid-template-columns: 1fr;
        }
    }

    .content-card {
        background: var(--card-bg);
        border-radius: 24px;
        border: 1px solid var(--border);
        padding: 25px;
    }

    .card-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 25px;
    }

    .card-title {
        font-size: 18px;
        font-weight: 700;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    /* Table Styling */
    .custom-table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0 10px;
    }

    .custom-table th {
        text-align: left;
        padding: 10px 15px;
        color: var(--text-muted);
        font-size: 12px;
        text-transform: uppercase;
        font-weight: 700;
    }

    .custom-table td {
        padding: 15px;
        background: rgba(255, 255, 255, 0.03);
        border-top: 1px solid var(--border);
        border-bottom: 1px solid var(--border);
    }

    .custom-table tr td:first-child {
        border-left: 1px solid var(--border);
        border-radius: 12px 0 0 12px;
    }

    .custom-table tr td:last-child {
        border-right: 1px solid var(--border);
        border-radius: 0 12px 12px 0;
    }

    .badge {
        padding: 6px 12px;
        border-radius: 8px;
        font-size: 11px;
        font-weight: 700;
    }

    .badge-success { background: rgba(16, 185, 129, 0.1); color: #10b981; }
    .badge-danger { background: rgba(239, 68, 68, 0.1); color: #ef4444; }

    /* Quick Actions */
    .quick-action-btn {
        display: flex;
        align-items: center;
        gap: 15px;
        padding: 15px;
        background: rgba(255, 255, 255, 0.03);
        border: 1px solid var(--border);
        border-radius: 16px;
        text-decoration: none;
        color: var(--text-main);
        transition: var(--transition);
        margin-bottom: 12px;
    }

    .quick-action-btn:hover {
        background: rgba(99, 102, 241, 0.1);
        border-color: var(--primary);
        transform: translateX(5px);
    }

    .quick-action-btn i {
        width: 40px;
        height: 40px;
        background: var(--primary);
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
    }
</style>

<div class="dashboard-container">
    <!-- Stat Cards -->
    <div class="dashboard-grid">
        <div class="stat-card" style="--card-accent: #6366f1;">
            <div class="stat-icon" style="background: rgba(99, 102, 241, 0.1); color: #6366f1;">
                <i class="fas fa-users"></i>
            </div>
            <div class="stat-info">
                <h3>Total de Clientes</h3>
                <div class="stat-value"><?php echo $total_users; ?></div>
            </div>
        </div>

        <div class="stat-card" style="--card-accent: #10b981;">
            <div class="stat-icon" style="background: rgba(16, 185, 129, 0.1); color: #10b981;">
                <i class="fas fa-user-check"></i>
            </div>
            <div class="stat-info">
                <h3>Clientes Ativos</h3>
                <div class="stat-value"><?php echo $active_users; ?></div>
            </div>
        </div>

        <div class="stat-card" style="--card-accent: #ef4444;">
            <div class="stat-icon" style="background: rgba(239, 68, 68, 0.1); color: #ef4444;">
                <i class="fas fa-user-times"></i>
            </div>
            <div class="stat-info">
                <h3>Clientes Expirados</h3>
                <div class="stat-value"><?php echo $expired_users; ?></div>
            </div>
        </div>


    </div>

    <!-- Main Content Grid -->
    <div class="main-grid">
        <!-- Recent Users Table -->
        <div class="content-card">
            <div class="card-header">
                <h2 class="card-title">
                    <span class="material-icons-round" style="color: var(--primary);">history</span>
                    Atividades Recentes
                </h2>
                <a href="users.php" style="color: var(--primary); font-size: 13px; text-decoration: none; font-weight: 600;">Ver Todos</a>
            </div>
            
            <div style="overflow-x: auto;">
                <table class="custom-table">
                    <thead>
                        <tr>
                            <th>Usuário / MAC</th>
                            <th>Vencimento</th>
                            <th>Status</th>
                            <th>Ações</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while($row = $recent_users->fetchArray(SQLITE3_ASSOC)): 
                            $is_expired = strtotime($row['expire_date']) < time();
                        ?>
                        <tr>
                            <td>
                                <div style="font-weight: 700;"><?php echo htmlspecialchars($row['username']); ?></div>
                                <div style="font-size: 11px; color: var(--text-muted);"><?php echo htmlspecialchars($row['mac_address']); ?></div>
                            </td>
                            <td style="font-size: 13px;">
                                <?php echo date('d/m/Y', strtotime($row['expire_date'])); ?>
                            </td>
                            <td>
                                <span class="badge <?php echo $is_expired ? 'badge-danger' : 'badge-success'; ?>">
                                    <?php echo $is_expired ? 'EXPIRADO' : 'ATIVO'; ?>
                                </span>
                            </td>
                            <td>
                                <a href="users_update.php?id=<?php echo $row['id']; ?>" style="color: var(--text-muted);"><i class="fas fa-edit"></i></a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Quick Actions Sidebar -->
        <div class="content-card">
            <div class="card-header">
                <h2 class="card-title">Ações Rápidas</h2>
            </div>
            
            <a href="users_create.php" class="quick-action-btn">
                <i class="fas fa-user-plus"></i>
                <div>
                    <div style="font-weight: 600; font-size: 14px;">Novo Cliente</div>
                    <div style="font-size: 12px; color: var(--text-muted);">Adicionar manualmente</div>
                </div>
            </a>

            <a href="create_link.php" class="quick-action-btn">
                <i class="fas fa-credit-card"></i>
                <div>
                    <div style="font-weight: 600; font-size: 14px;">Gerar Link</div>
                    <div style="font-size: 12px; color: var(--text-muted);">Pagamento via PIX/Cartão</div>
                </div>
            </a>

            <a href="note.php" class="quick-action-btn">
                <i class="fas fa-bell"></i>
                <div>
                    <div style="font-weight: 600; font-size: 14px;">Enviar Aviso</div>
                    <div style="font-size: 12px; color: var(--text-muted);">Notificar todos usuários</div>
                </div>
            </a>

            <a href="layouts.php" class="quick-action-btn">
                <i class="fas fa-paint-brush"></i>
                <div>
                    <div style="font-weight: 600; font-size: 14px;">Mudar Tema</div>
                    <div style="font-size: 12px; color: var(--text-muted);">Alterar visual do app</div>
                </div>
            </a>
        </div>
    </div>
</div>

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