<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    {{ include('partial_embed_styles.html.twig') }}
</head>

<body>
<form action="" method="post" id="client-login" class="embed-shell embed-stack">
    <h1 class="embed-title">{{ 'Login to Client Area'|trans }}</h1>
    <div class="embed-field">
        <label class="embed-sr-only" for="embed-login-email">{{ 'Email'|trans }}</label>
        <input id="embed-login-email" class="embed-input" type="email" name="email" value="" required="required" placeholder="{{ 'Your email'|trans }}">
    </div>
    <div class="embed-field">
        <label class="embed-sr-only" for="embed-login-password">{{ 'Password'|trans }}</label>
        <input id="embed-login-password" class="embed-input" type="password" name="password" value="" required="required" placeholder="{{ 'Your password'|trans }}">
    </div>
    <button class="embed-button" type="submit">{{ 'Login'|trans }}</button>
</form>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        document.getElementById('client-login').addEventListener('submit', function(event) {
            event.preventDefault();

            const formData = new FormData(this);
            const formDataEncoded = new URLSearchParams(formData).toString();

            fetch("{{ 'client/login'|api_url(role='guest') }}", {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: formDataEncoded
            })
            .then(response => response.json())
            .then(json => {
                if(json.error) {
                    alert(json.error.message);
                } else {
                    parent.window.location = "{{ '/'|url }}";
                }
            })
            .catch(error => console.error('Error:', error));
        });
    });
</script>
</body>
</html>
