<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$db = new SQLite3('a/.eggziedb.db');
$db->exec('CREATE TABLE IF NOT EXISTS ibo(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, mac_address VARCHAR(100), username VARCHAR(100), password VARCHAR(100), expire_date VARCHAR(100), url VARCHAR(100), title VARCHAR(100), created_at VARCHAR(100))');
$total = (int) $db->querySingle('SELECT COUNT(*) FROM ibo');
$ultimos = [];
$res = $db->query('SELECT * FROM ibo ORDER BY id DESC LIMIT 5');
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
    $ultimos[] = $row;
}
include 'includes/header.php';
?>
<style>
    .recent-hero { background: linear-gradient(135deg, #18264d 0%, #264e88 55%, #4b6cb7 100%); border-radius: 18px; color: #fff; }
    .recent-card { height: 100%; border: 1px solid #334155; border-radius: 16px; background: #1e293b; color: #f8fafc; box-shadow: 0 8px 24px rgba(0, 0, 0, .25); overflow: hidden; transition: transform .2s ease, box-shadow .2s ease; }
    .recent-card:hover { transform: translateY(-4px); box-shadow: 0 14px 30px rgba(14, 165, 233, .22); border-color: #0ea5e9; }
    .recent-card-head { padding: 18px 20px; background: linear-gradient(135deg, #24344d, #1e293b); border-bottom: 1px solid #334155; }
    .recent-number { width: 34px; height: 34px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; background: rgba(14, 165, 233, .18); color: #38bdf8; font-weight: 800; }
    .recent-title { color: #f8fafc; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .recent-mac { color: #38bdf8; font-family: monospace; font-weight: 700; letter-spacing: .3px; }
    .recent-body { padding: 18px 20px; }
    .recent-meta { color: #cbd5e1; font-size: .88rem; margin-bottom: 10px; display: flex; align-items: center; gap: 9px; }
    .recent-meta i { color: #38bdf8; width: 17px; }
    .active-pill { border-radius: 30px; padding: 5px 10px; background: #daf7e8; color: #16834b; font-size: .7rem; font-weight: 700; text-transform: uppercase; }
    .empty-recent { border: 2px dashed #334155; border-radius: 18px; padding: 64px 20px; text-align: center; background: #1e293b; color: #f8fafc; }
    .client-actions { display: flex; gap: 10px; }
    .client-actions .btn { flex: 1 1 0; }
</style>
<div class="container-fluid">
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <div><h1 class="h3 mb-1 text-gray-800"><i class="fas fa-list text-primary mr-2"></i>Lista de Clientes</h1><p class="text-muted mb-0">Os 5 últimos clientes ativados aparecem aqui.</p></div>
        <a href="users_create.php" class="btn btn-primary shadow-sm mt-3 mt-sm-0"><i class="fas fa-plus mr-1"></i> Novo Cliente</a>
    </div>

    <div class="recent-hero shadow-sm mb-4">
        <div class="card-body p-4 d-flex flex-wrap align-items-center justify-content-between">
            <div><div class="text-uppercase small font-weight-bold mb-2" style="letter-spacing:1.1px; opacity:.75;">Resumo rápido</div><h2 class="h4 text-white mb-2">Últimos clientes ativados</h2><p class="mb-0" style="opacity:.82;">Acompanhe rapidamente as ativações mais recentes do painel.</p></div>
            <div class="text-center mt-3 mt-md-0 px-4 py-3" style="background:rgba(255,255,255,.13); border-radius:14px; min-width:145px;"><div class="h2 mb-0 font-weight-bold"><?php echo $total; ?></div><div class="small">clientes ativos no total</div></div>
        </div>
    </div>

    <?php if (!$ultimos): ?>
        <div class="empty-recent"><i class="fas fa-users-slash fa-3x text-info mb-3"></i><h4 class="text-white">Ainda não há clientes ativados</h4><p class="text-light mb-0">Quando você ativar um cliente, ele aparecerá neste painel.</p></div>
    <?php else: ?>
        <div class="row">
        <?php foreach ($ultimos as $posicao => $cliente): ?>
            <div class="col-xl-4 col-lg-6 mb-4">
                <div class="recent-card">
                    <div class="recent-card-head d-flex align-items-center justify-content-between"><div class="d-flex align-items-center min-width-0"><span class="recent-number mr-3"><?php echo $posicao + 1; ?></span><div class="recent-title" title="<?php echo htmlspecialchars($cliente['title'] ?? '', ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlspecialchars($cliente['title'] ?: 'Cliente sem título', ENT_QUOTES, 'UTF-8'); ?></div></div><span class="active-pill ml-2">Ativo</span></div>
                    <div class="recent-body"><div class="recent-mac mb-3"><?php echo htmlspecialchars(strtoupper($cliente['mac_address'] ?? ''), ENT_QUOTES, 'UTF-8'); ?></div><div class="recent-meta"><i class="fas fa-user"></i><span><?php echo htmlspecialchars($cliente['username'] ?? '-', ENT_QUOTES, 'UTF-8'); ?></span></div><div class="recent-meta"><i class="fas fa-calendar-check"></i><span>Vencimento: <?php echo htmlspecialchars($cliente['expire_date'] ?? '-', ENT_QUOTES, 'UTF-8'); ?></span></div><div class="recent-meta mb-3"><i class="fas fa-clock"></i><span>Ativado: <?php echo htmlspecialchars($cliente['created_at'] ?? '-', ENT_QUOTES, 'UTF-8'); ?></span></div><div class="client-actions"><a href="users_update.php?update=<?php echo (int) $cliente['id']; ?>" class="btn btn-outline-primary btn-sm"><i class="fas fa-edit mr-1"></i> Gerenciar</a><a href="delete_client.php?id=<?php echo (int) $cliente['id']; ?>&from=users" class="btn btn-outline-danger btn-sm" onclick="return confirm('Tem certeza que deseja excluir este cliente? Esta ação não pode ser desfeita.');"><i class="fas fa-trash-alt mr-1"></i> Excluir</a></div></div>
                </div>
            </div>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>

    <div class="text-center mt-2 mb-4"><a href="meus_clientes.php" class="btn btn-light border shadow-sm"><i class="fas fa-users mr-1 text-primary"></i> Ver todos em MEUS CLIENTES</a></div>
</div>
<?php include 'includes/footer.php'; ?>
