<?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))');

$search = trim($_GET['search'] ?? '');
$clientes = [];
if ($search !== '') {
    $stmt = $db->prepare("SELECT * FROM ibo WHERE mac_address LIKE :busca OR title LIKE :busca OR username LIKE :busca ORDER BY id DESC");
    $stmt->bindValue(':busca', '%' . $search . '%', SQLITE3_TEXT);
    $res = $stmt->execute();
} else {
    $res = $db->query('SELECT * FROM ibo ORDER BY id DESC');
}
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
    $clientes[] = $row;
}
$total_clientes = count($clientes);
include 'includes/header.php';
?>
<style>
    .client-hero { background: linear-gradient(135deg, #101f46 0%, #153c72 55%, #087f9b 100%); border: 0; border-radius: 18px; color: #fff; overflow: hidden; }
    .client-card { height: 100%; border: 1px solid #334155; border-radius: 16px; background: #1e293b; color: #f8fafc; box-shadow: 0 8px 24px rgba(0, 0, 0, .25); transition: transform .2s ease, box-shadow .2s ease; overflow: hidden; }
    .client-card:hover { transform: translateY(-4px); box-shadow: 0 14px 30px rgba(14, 165, 233, .22); border-color: #0ea5e9; }
    .client-card-top { padding: 18px 20px; background: linear-gradient(135deg, #24344d, #1e293b); border-bottom: 1px solid #334155; }
    .client-icon { width: 44px; height: 44px; border-radius: 13px; display: inline-flex; align-items: center; justify-content: center; background: rgba(14, 165, 233, .18); color: #38bdf8; font-size: 1.15rem; }
    .client-title { font-weight: 700; color: #f8fafc; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .client-mac { font-family: monospace; letter-spacing: .4px; color: #38bdf8; font-weight: 700; }
    .client-card-body { padding: 18px 20px; }
    .client-meta { display: flex; align-items: center; gap: 9px; color: #cbd5e1; font-size: .88rem; margin-bottom: 10px; }
    .client-meta i { width: 18px; color: #38bdf8; }
    .active-pill { border-radius: 30px; padding: 5px 10px; background: #daf7e8; color: #16834b; font-size: .72rem; font-weight: 700; text-transform: uppercase; }
    .empty-client { 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="client-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.2px; opacity:.75;">Área exclusiva</div>
                <h1 class="h3 mb-2 text-white"><i class="fas fa-users mr-2"></i>MEUS CLIENTES</h1>
                <p class="mb-0" style="opacity:.82;">Todos os clientes ativados ficam salvos aqui permanentemente.</p>
            </div>
            <div class="text-center mt-3 mt-md-0 px-4 py-3" style="background:rgba(255,255,255,.12); border-radius:14px; min-width:145px;">
                <div class="h2 mb-0 font-weight-bold"><?php echo $total_clientes; ?></div>
                <div class="small">clientes ativos</div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body py-3">
            <form method="get" class="form-inline">
                <div class="input-group w-100">
                    <input type="text" name="search" class="form-control bg-light border-0" placeholder="Buscar por MAC, título ou usuário..." value="<?php echo htmlspecialchars($search, ENT_QUOTES, 'UTF-8'); ?>">
                    <div class="input-group-append"><button class="btn btn-primary" type="submit"><i class="fas fa-search mr-1"></i> Buscar</button></div>
                </div>
            </form>
        </div>
    </div>

    <?php if (!$clientes): ?>
        <div class="empty-client"><i class="fas fa-user-slash fa-3x text-info mb-3"></i><h4 class="text-white">Nenhum cliente ativo</h4><p class="text-light mb-0">Os clientes ativados aparecerão nesta área.</p></div>
    <?php else: ?>
        <div class="row">
        <?php foreach ($clientes as $cliente): ?>
            <div class="col-xl-4 col-lg-6 mb-4">
                <div class="client-card">
                    <div class="client-card-top d-flex align-items-center justify-content-between">
                        <div class="d-flex align-items-center min-width-0"><span class="client-icon mr-3"><i class="fas fa-user-check"></i></span><div class="client-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="client-card-body">
                        <div class="client-mac mb-3"><?php echo htmlspecialchars(strtoupper($cliente['mac_address'] ?? ''), ENT_QUOTES, 'UTF-8'); ?></div>
                        <div class="client-meta"><i class="fas fa-user"></i><span><?php echo htmlspecialchars($cliente['username'] ?? '-', ENT_QUOTES, 'UTF-8'); ?></span></div>
                        <div class="client-meta"><i class="fas fa-calendar-check"></i><span>Vencimento: <?php echo htmlspecialchars($cliente['expire_date'] ?? '-', ENT_QUOTES, 'UTF-8'); ?></span></div>
                        <div class="client-meta mb-3"><i class="fas fa-server"></i><span class="text-truncate"><?php echo htmlspecialchars($cliente['url'] ?? '-', 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=meus_clientes" 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>
<?php include 'includes/footer.php'; ?>
