<!DOCTYPE html>
<html data-bs-theme="light" lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Login</title>
    <link rel="icon" type="image/png" href="/static/img/shipit.png">
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
</head>

<body>
    <div class="container"><a class="navbar-brand fw-bolder" href="/#" style="font-family: Lora, serif;font-size: 35px;" data-bs-target="/">#</a><button data-bs-toggle="collapse" data-bs-target="#navbarResponsive" class="navbar-toggler" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation" style="padding: 6px;border-radius: 0px;border-width: 0px;"><i class="fa fa-bars" style="font-size: 27px;"></i></button>

    <div class="container" style="margin-top: 10rem;">
        <div class="row">
            <div class="col-md-10 col-lg-4 mx-auto">
                <p id="responseError"></p>
                <form id="loginForm">
                    <div class="form-outline mb-4">
                        <input type="email" id="form2Example1" name="email" class="form-control" />
                        <label class="form-label" for="form2Example1">Email address</label>
                    </div>

                    <div class="form-outline mb-4">
                        <input type="password" id="form2Example2" name="password" class="form-control" />
                        <label class="form-label" for="form2Example2">Password</label>
                    </div>

                    <button type="sumbit" class="btn btn-primary btn-block mb-4">Sign in</button>
                </form>
            </div>
        </div>
    </div>

    <script src="/static/bootstrap/js/bootstrap.min.js"></script>
    <script>
        function handleSubmit(event) {
            event.preventDefault();
            const form = document.getElementById('loginForm');
            const formData = new FormData(form);
            console.log(formData);

            fetch('{{login_url}}', {
                method: 'POST',
                body: formData,
            })
            .then(response => {
                if (response.status === 200) {
                    console.log(response.json)
                    return response.json();
                } else if (response.status === 400) {
                    throw new Error('Email or password incorrect.');
                }
                else {
                    throw new Error(`HTTP Error! Status: ${response.status}`);
                }
            })
            .then(data => {
                console.log(data)
                const accessToken = data.access_token;
                var now = new Date();
                now.setDate(now.getDate() + 30);
                document.cookie = `access_token=${accessToken};expires=${now.toUTCString()};path=/`;
                window.location.href = window.location.origin;
            })
            .catch(error => {
                document.getElementById('responseError').innerHTML = error.message;
            });
        }

        const loginForm = document.getElementById('loginForm');
        loginForm.addEventListener('submit', handleSubmit);
    </script>
</body>

</html>
