<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Регистрация - CS2 Simulator</title>
    <link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
    {% include "_nav.html" %}

    <main class="auth-container">
        <div class="auth-card">
            <h2>Регистрация</h2>
            
            <form id="registerForm" class="auth-form">
                <div class="form-group">
                    <label for="username">Имя пользователя</label>
                    <input type="text" id="username" name="username" required minlength="3">
                    <small>Минимум 3 символа</small>
                </div>
                
                <div class="form-group">
                    <label for="password">Пароль</label>
                    <input type="password" id="password" name="password" required minlength="4">
                    <small>Минимум 4 символа</small>
                </div>
                
                <div id="errorMessage" class="error-message" style="display: none;"></div>
                
                <button type="submit" class="btn btn-primary btn-block">Зарегистрироваться</button>
            </form>
            
            <p class="auth-footer">
                Уже есть аккаунт? <a href="/login">Войдите</a>
            </p>
        </div>
    </main>

    <script>
        document.getElementById('registerForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const formData = new FormData(e.target);
            const errorDiv = document.getElementById('errorMessage');
            
            try {
                const response = await fetch('/web/api/auth/register', {
                    method: 'POST',
                    body: formData
                });
                
                const data = await response.json();
                
                if (response.ok && data.success) {
                    window.location.href = data.redirect;
                } else {
                    errorDiv.textContent = data.error || 'Ошибка регистрации';
                    errorDiv.style.display = 'block';
                }
            } catch (error) {
                errorDiv.textContent = 'Ошибка соединения с сервером';
                errorDiv.style.display = 'block';
            }
        });
    </script>
    <script src="/static/js/safemode.js"></script>
</body>
</html>