<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../styles/main.css">
    <script src="../scripts/jquery.min.js"></script>
    <script src="../scripts/helpPopup.js"></script>
    <title>Регистрация</title>
    <style>
        body {
            background-color: #EEEFF5;
        }
    </style>
</head>
<body>
    <header>
        <h1>Бюро пропусков</h1>
        <nav>
            <div class="dropdown">
                <a class="help-button" onclick="toggleDropdown('dropdownHelp-1')">Техническая поддержка</a>               
                <div id="dropdownHelp-1" class="dropdown-help">
                    <a href="tel:83477527706">8 (34775) 2-77-06</a>
                    <a href="tel:89174023516">8 (917) 402-35-16</a>
                </div>
              </div>
            <div class="dropdown">
                <a class="help-button" onclick="toggleDropdown('dropdownHelp-2')">Инструкции</a>               
                <div id="dropdownHelp-2" class="dropdown-help dropdown-manual">
                    <a href="/docs/manual.pdf" target="_blank">Текст</a>
                    <a href="https://drive.google.com/file/d/1CxrAgr2brQclZqtbbreSUU9tN-jsNTwf/view?usp=sharing" target="_blank">Видео</a>
                </div>
              </div>
            <a href="/signin">Войти</a>
            <a href="/signup">Зарегистрироваться</a>
        </nav>
    </header>
    
    <form class="login-form" id="signin-form">
        <h1>Авторизация</h1>
        <input type="text" id="email-input" placeholder="Введите email" required>
        <input type="password" id="password-input" placeholder="Введите пароль" required>
        <button style="margin-top: 15px;" type="submit" id="signin-button">Войти</button>
        <span>Нет аккаунта? <a href="/signup">Зарегистрироваться</a></span>
    </form>

    <script>
        document.addEventListener("DOMContentLoaded", function() {
            const signupForm = document.getElementById("signin-form");

            signupForm.addEventListener("submit", async function(event) {
                event.preventDefault();
                $('#signin-button').addClass('inactive');

                const email = $('#email-input').val();
                const password = $('#password-input').val();

                // Отправляем запрос на сервер для авторизации
                const response = await fetch("{{API_SERVER}}/login", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({ email, password })
                });

                if (response.status === 200) {
                    const data = await response.json();
                    const token = data.token;
                    console.log("GET TOKEN", token);

                    document.cookie = "token=" + token;

                    window.location.href = "/";
                    $('#signin-button').removeClass('inactive');
                } else {
                    $('#signin-button').removeClass('inactive');
                    alert("Ошибка авторизации");
                }
            });
        });

    </script>
    <script src="https://rawgit.com/RobinHerbots/Inputmask/5.x/dist/jquery.inputmask.js"></script>
    <script>
        $(document).ready(function(){
      
            $('#phone-input').inputmask({"mask": "+7 (999) 999-9999"});
      
        });
      </script>
</body>
</html>