<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Авторизация</title>
    <link rel="stylesheet" href="../styles/main.css" />
</head>
<body>

    <section class="form right">
        <form enctype="multipart/form-data" method="post" action="/login" id="loginForm">
            <h1 style="margin-bottom: 40px;">Добро пожаловать в Аргус</h1>

            <label for="login">Логин или Email<span>*</span></label>
            <input placeholder="Введите логин или Email" name="email" type="text" required>

            <label for="password">Пароль<span>*</span></label>
            <input placeholder="Введите пароль" name="password" type="password" required>

            <button type="submit">Войти</button>
        </form>
    </section>

    <video class="animation left" autoplay muted loop>
        <source src="../img/traffic.mp4" type="video/mp4">
    </video>
    <span class="copyright left" id="copyright"><a href="https://dribbble.com/shots/15608015-Traffic" target="_blank">Видеоматериал создан Igor Kozak для 10Clouds</a></span>

    <span id="info-icon"><a href="https://dribbble.com/shots/15608015-Traffic" target="_blank">i</a></span>
    

    <script>
        document.getElementById('info-icon').addEventListener('mouseenter', function () {
            document.getElementById('copyright').style.opacity = '1';
        });
    
        document.getElementById('info-icon').addEventListener('mouseleave', function () {
            document.getElementById('copyright').style.opacity = '0';
        });
    </script>

    

    <script>
        document.addEventListener("DOMContentLoaded", function() {

            // Обработка отправки формы в формате JSON
            const loginForm = document.getElementById('loginForm');
            loginForm.addEventListener('submit', function(event) {

                event.preventDefault(); 
                const formData = new FormData(loginForm);
                const jsonData = {};

                formData.forEach((value, key) => {
                    jsonData[key] = value;
                });

                const xhr = new XMLHttpRequest();
                xhr.open('POST', '/login');
                xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');

                xhr.onload = function() {
                    if (xhr.status === 200) {
                        location.href = '/';
                    } else if (xhr.status === 401) {
                        alert('Неверный логин или пароль');
                    } else {
                        alert('Произошла ошибка при отправке данных');
                        window.location.reload();
                    }
                };

                xhr.onerror = function() {
                    // Ошибка сети
                    alert('Произошла ошибка сети');
                };

                xhr.send(JSON.stringify(jsonData));
            });
        });
    </script>
    
</body>
</html>