<html lang="pt-br">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <script src="https://code.iconify.design/iconify-icon/1.0.7/iconify-icon.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <link rel="stylesheet" href="/stylesheets/login.css">
</head>

<div class="apresentation">
    <h1>Bem-vindo(a) ao</h1>
    <img src="../../images/logo-portal-do-artista.png" alt="logo" width="300px" height="40px">
</div>
<% if (locals.err) { %>
    <div>
        <%= err %>
    </div>
    <% } %>
        <div class="container">
            <h1>Login</h1>
            <form id="loginForm">
                <div class="mb-3">
                    <label for="email" class="form-label">E-mail</label>
                    <input type="email" name="email" class="form-control" id="email"
                        placeholder="Digite seu e-mail" required>
                </div>
                <div class="mb-3">
                    <label for="password" class="form-label">Senha</label>
                    <input type="password" name="password" class="form-control" id="password"
                        placeholder="Digite sua senha" minlength="8" required>
                    <a href="/auth/resetsenha">Esqueceu a senha?</a>
                </div>
                <div>
                    <button type="submit" class="btn">Entrar</button>
                </div>
            </form>
            <div class="register">
                <a href="/auth/registro">Não possui login? <b>Registre-se</b></a>
            </div>
        </div>

        <script>
            document.getElementById('loginForm').addEventListener('submit', async function (event) {
                event.preventDefault();
                const email = document.getElementById('email').value;
                const password = document.getElementById('password').value;
                try {
                    const response = await fetch('login', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ email, password })
                    });
                    const data = await response.json();
                    if (data.auth) {
                        localStorage.setItem('token', data.token);
                        alert('Login bem-sucedido!');
                        window.location.href = '/';
                    } else {
                        alert('Erro no login, verifique suas credenciais.');
                    }
                } catch (error) {
                    console.error('Erro na requisição:', error);
                }
            });
        </script>
        <%- include('../partials/vlibras.ejs') %>
</html>