<?php
//<----------------------------------------------------------->
//<-------------------USERS.PHP CORRIGIDO--------------------->
//<----------------------------------------------------------->

ini_set('display_errors', 0); // Desliga warnings
error_reporting(E_ALL);

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

// Buscar todos os usuários
$result = $db->query('SELECT * FROM ibo ORDER BY id DESC');
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gerenciar Usuários - IBO Notification</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background: linear-gradient(135deg, #0a192f 0%, #020c1b 100%);
            color: #e6f1ff;
            font-family: 'Segoe UI', system-ui, sans-serif;
            min-height: 100vh;
            padding: 2rem;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: rgba(17, 34, 64, 0.7);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 2rem;
            border: 1px solid rgba(100, 255, 218, 0.1);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
        }
        
        h1 {
            font-size: 2rem;
            background: linear-gradient(90deg, #64ffda, #57cbff);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            margin-bottom: 1.5rem;
            display: inline-block;
        }
        
        .btn-create {
            background: linear-gradient(135deg, #64ffda, #57cbff);
            color: #0a192f;
            padding: 0.8rem 1.5rem;
            border-radius: 12px;
            text-decoration: none;
            font-weight: 600;
            float: right;
            transition: all 0.3s ease;
        }
        
        .btn-create:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(100, 255, 218, 0.4);
        }
        
        .clearfix::after {
            content: "";
            clear: both;
            display: table;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 2rem;
            overflow-x: auto;
            display: block;
        }
        
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid rgba(100, 255, 218, 0.2);
        }
        
        th {
            background: rgba(100, 255, 218, 0.1);
            color: #64ffda;
            font-weight: 600;
            font-size: 0.9rem;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        tr:hover {
            background: rgba(100, 255, 218, 0.05);
        }
        
        .btn-edit, .btn-delete {
            display: inline-block;
            padding: 5px 12px;
            border-radius: 6px;
            text-decoration: none;
            font-size: 0.85rem;
            margin: 0 3px;
            transition: all 0.3s ease;
        }
        
        .btn-edit {
            background: #ffb052;
            color: #0a192f;
        }
        
        .btn-edit:hover {
            background: #ff9f2e;
            transform: translateY(-1px);
        }
        
        .btn-delete {
            background: #ff5252;
            color: white;
        }
        
        .btn-delete:hover {
            background: #ff3838;
            transform: translateY(-1px);
        }
        
        .empty-state {
            text-align: center;
            padding: 3rem;
            color: #8892b0;
        }
        
        @media (max-width: 768px) {
            body { padding: 1rem; }
            .container { padding: 1rem; }
            th, td { padding: 8px 10px; font-size: 0.85rem; }
            .btn-create { float: none; display: inline-block; margin-top: 1rem; }
            h1 { display: block; }
        }
        
        @media (max-width: 480px) {
            th, td { padding: 6px 8px; font-size: 0.75rem; }
            .btn-edit, .btn-delete { padding: 3px 8px; font-size: 0.7rem; }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="clearfix">
            <h1><i class="fas fa-users"></i> Gerenciar Usuários</h1>
            <a href="users_create.php" class="btn-create">+ Criar Novo Usuário</a>
        </div>
        
        <div style="overflow-x: auto;">
            <table>
                <thead>
                    <tr>
                        <th>Cliente</th>
                        <th>MAC Address</th>
                        <th>Usuário</th>
                        <th>Vendedor</th>
                        <th>Expiração</th>
                        <th>DNS</th>
                        <th>Extra</th>
                        <th>Ações</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $temRegistros = false;
                    while($row = $result->fetchArray(SQLITE3_ASSOC)): 
                        $temRegistros = true;
                    ?>
                    <tr>
                        <td><?php echo isset($row['title']) ? htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8') : '-'; ?></td>
                        <td><?php echo isset($row['mac_address']) ? htmlspecialchars($row['mac_address'], ENT_QUOTES, 'UTF-8') : '-'; ?></td>
                        <td><?php echo isset($row['username']) ? htmlspecialchars($row['username'], ENT_QUOTES, 'UTF-8') : '-'; ?></td>
                        <td><?php echo isset($row['seller']) ? htmlspecialchars($row['seller'], ENT_QUOTES, 'UTF-8') : '-'; ?></td>
                        <td><?php echo isset($row['expire_date']) ? htmlspecialchars($row['expire_date'], ENT_QUOTES, 'UTF-8') : '-'; ?></td>
                        <td><?php echo isset($row['url']) ? htmlspecialchars($row['url'], ENT_QUOTES, 'UTF-8') : '-'; ?></td>
                        <td><?php echo isset($row['extra']) ? htmlspecialchars($row['extra'], ENT_QUOTES, 'UTF-8') : '-'; ?></td>
                        <td>
                            <a href="users_edit.php?id=<?php echo isset($row['id']) ? $row['id'] : ''; ?>" class="btn-edit">Editar</a>
                            <a href="users_delete.php?id=<?php echo isset($row['id']) ? $row['id'] : ''; ?>" class="btn-delete" onclick="return confirm('Tem certeza que deseja excluir este usuário?')">Excluir</a>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                    
                    <?php if(!$temRegistros): ?>
                    <tr>
                        <td colspan="8" class="empty-state">
                            Nenhum usuário cadastrado. 
                            <a href="users_create.php" style="color: #64ffda;">Clique aqui para criar o primeiro</a>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <script>
        // Adicionar ícones Font Awesome se necessário
        if(!document.querySelector('link[href*="font-awesome"]')) {
            var link = document.createElement('link');
            link.rel = 'stylesheet';
            link.href = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css';
            document.head.appendChild(link);
        }
    </script>
</body>
</html>

<?php
$db->close();
?>