<?php
require_once __DIR__ . '/includes/header.php';

$message = '';
$message_type = '';

// Processar criação de usuário
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = str_in($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    
    if (empty($username) || empty($password) || empty($confirm_password)) {
        $message = 'Preencha todos os campos.';
        $message_type = 'error';
    } elseif ($password !== $confirm_password) {
        $message = 'As senhas não coincidem.';
        $message_type = 'error';
    } else {
        $result = create_user($username, $password);
        $message = $result['message'] ?? ($result['ok'] ? 'Usuário criado com sucesso!' : 'Erro ao criar usuário.');
        $message_type = $result['ok'] ? 'success' : 'error';
    }
}

// Buscar todos os usuários
$users = db()->query("SELECT id, username, is_active, created_at FROM app_users ORDER BY created_at DESC")->fetchAll();
?>

<style>
    .page-header {
        margin-bottom: 30px;
    }
    
    .page-header h1 {
        font-size: 28px;
        margin-bottom: 8px;
        background: var(--primary-bg);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
    
    .page-header p {
        color: #999;
        font-size: 14px;
    }
    
    .alert {
        padding: 15px 20px;
        border-radius: 8px;
        margin-bottom: 20px;
        animation: slideIn 0.3s ease;
    }
    
    .alert-success {
        background: rgba(46, 204, 113, 0.2);
        border: 1px solid #2ecc71;
        color: #2ecc71;
    }
    
    .alert-error {
        background: rgba(231, 76, 60, 0.2);
        border: 1px solid #e74c3c;
        color: #e74c3c;
    }
    
    .content-grid {
        display: grid;
        grid-template-columns: 400px 1fr;
        gap: 25px;
        max-width: 1400px;
    }
    
    .card {
        background: #1a1a1a;
        padding: 30px;
        border-radius: 12px;
        border: 1px solid rgba(255, 255, 255, 0.1);
    }
    
    .card h2 {
        font-size: 18px;
        margin-bottom: 20px;
        padding-bottom: 15px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        color: #fff;
    }
    
    .form-group {
        margin-bottom: 20px;
    }
    
    .form-group label {
        display: block;
        margin-bottom: 8px;
        color: #fff;
        font-weight: 500;
        font-size: 14px;
    }
    
    .form-group input {
        width: 100%;
        padding: 12px 15px;
        background: #111;
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-radius: 6px;
        color: #fff;
        font-size: 14px;
        transition: all 0.2s ease;
    }
    
    .form-group input:focus {
        outline: none;
        border-color: var(--primary-bg);
        box-shadow: 0 0 0 3px rgba(192, 57, 43, 0.1);
    }
    
    .submit-btn {
        background: var(--primary-bg);
        color: #fff;
        padding: 12px 30px;
        border: none;
        border-radius: 6px;
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.2s ease;
        width: 100%;
    }
    
    .submit-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }
    
    .users-table {
        width: 100%;
        border-collapse: collapse;
    }
    
    .users-table thead {
        background: #0a0a0a;
    }
    
    .users-table th {
        padding: 15px;
        text-align: left;
        font-size: 14px;
        font-weight: 600;
        color: #fff;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }
    
    .users-table td {
        padding: 15px;
        font-size: 14px;
        color: #ccc;
        border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    }
    
    .users-table tbody tr {
        transition: background 0.2s ease;
    }
    
    .users-table tbody tr:hover {
        background: rgba(255, 255, 255, 0.02);
    }
    
    .status-badge {
        display: inline-block;
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 600;
    }
    
    .status-active {
        background: rgba(46, 204, 113, 0.2);
        color: #2ecc71;
    }
    
    .status-inactive {
        background: rgba(231, 76, 60, 0.2);
        color: #e74c3c;
    }
    
    @media (max-width: 1024px) {
        .content-grid {
            grid-template-columns: 1fr;
        }
    }
    
    @media (max-width: 768px) {
        .users-table {
            font-size: 12px;
        }
        
        .users-table th,
        .users-table td {
            padding: 10px 8px;
        }
    }
</style>

<div class="page-header">
    <h1>👥 Gerenciar Usuários</h1>
    <p>Crie e gerencie usuários do painel</p>
</div>

<?php if ($message): ?>
    <div class="alert alert-<?php echo $message_type; ?>">
        <?php echo htmlspecialchars($message); ?>
    </div>
<?php endif; ?>

<div class="content-grid">
    <div class="card">
        <h2>➕ Criar Novo Usuário</h2>
        <form method="POST">
            <div class="form-group">
                <label for="username">👤 Nome de Usuário</label>
                <input type="text" 
                       id="username" 
                       name="username" 
                       placeholder="Digite o nome de usuário (mín. 3 caracteres)"
                       autocomplete="off"
                       minlength="3"
                       required>
            </div>
            
            <div class="form-group">
                <label for="password">🔒 Senha</label>
                <input type="password" 
                       id="password" 
                       name="password" 
                       placeholder="Digite a senha (mín. 6 caracteres)"
                       autocomplete="new-password"
                       minlength="6"
                       required>
            </div>
            
            <div class="form-group">
                <label for="confirm_password">🔒 Confirmar Senha</label>
                <input type="password" 
                       id="confirm_password" 
                       name="confirm_password" 
                       placeholder="Digite novamente a senha"
                       autocomplete="new-password"
                       minlength="6"
                       required>
            </div>
            
            <button type="submit" class="submit-btn">✓ Criar Usuário</button>
        </form>
    </div>
    
    <div class="card">
        <h2>📋 Lista de Usuários</h2>
        <table class="users-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Usuário</th>
                    <th>Data de Criação</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($users)): ?>
                    <tr>
                        <td colspan="4" style="text-align: center; padding: 40px; color: #666;">
                            Nenhum usuário cadastrado.
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($users as $u): ?>
                        <tr>
                            <td>#<?php echo $u['id']; ?></td>
                            <td><?php echo htmlspecialchars($u['username']); ?></td>
                            <td><?php echo date('d/m/Y H:i', strtotime($u['created_at'])); ?></td>
                            <td>
                                <span class="status-badge <?php echo $u['is_active'] ? 'status-active' : 'status-inactive'; ?>">
                                    <?php echo $u['is_active'] ? 'Ativo' : 'Inativo'; ?>
                                </span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

</main>
</body>
</html>
