$(document).ready(function () {
    var errorMessage = $('#errorMessage');

    $('#login-form').submit(function (event) {
        event.preventDefault();

        var formData = {
            email: $('#email').val(),
            password: $('#password').val()
        };

        $.ajax({
            type: 'POST',
            url: '/login',
            contentType: 'application/json',
            data: JSON.stringify(formData),
            success: function (response) {
                if (response.logged) {
                    window.location.href = '/dashboard_notes_management';
                } else {
                    errorMessage.text('An error occurred while processing your request.');
                }
            },
            error: function (error) {
                errorMessage.text('An error occurred while processing your request.');
                displayError('An error occurred while processing your request.');
            }
        });
    });
});


function displayError(errorMessage) {
    const errorContainer = $('#error-container');
    errorContainer.empty();

    const errorElement = $('<div>').addClass('p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-50 dark:bg-gray-800 dark:text-red-400').attr('role', 'alert').html(`
    <span class="font-medium">Hey!</span> ${errorMessage}
  `);

    errorContainer.append(errorElement);
}