$(function () {
    $("#login-btn").click(function (event) {
        event.preventDefault();

        // Your existing login form validation code...
        $("#feedbackPrompt").hide();
        $("#feedbackPrompt").html("");

        $("#login-btn").attr("disabled", "");

        $(".email_feedback").hide();
        var email = $("input#email").val();
        if (email == "") {
            $(".email_feedback").html("Email field is required.");
            $(".email_feedback").show();
            $("input#email").focus();

            $("#login-btn").removeAttr("disabled");
            return false;
        }

        $(".password_feedback").hide();
        var password = $("input#password").val();
        if (password == "") {
            $(".password_feedback").html("Password cannot be empty.");
            $(".password_feedback").show();
            $("input#password").focus();

            $("#login-btn").removeAttr("disabled");
            return false;
        }

        // Verify reCAPTCHA
        var token = grecaptcha.getResponse();
        if (token == "") {
            // If reCAPTCHA is not checked
            $("#feedbackPrompt").html('<div class="alert alert-danger" role="alert">Please complete the reCAPTCHA.</div>');
            $("#feedbackPrompt").show();
            return false;
        }

        $(".button_spinner").show();

        $("input#email").prop("disabled", true);
        $("input#password").prop("disabled", true);

        var form_data =
            "&g-recaptcha-response=" + token + "&email=" + email + "&password=" + encodeURIComponent(password);
            
            

        $.ajax({
            url: "helpers/login",
            type: "POST",
            data: form_data,
        }).done(function (response) {
            if (response == "success") {
                window.location.href = "user/account";
            } else if (response == "admin_login") {
                window.location.href = "admin/home";
            } else if (response == "verify-account") {
                $("#feedbackPrompt").html(
                    '<div class="alert alert-success" role="alert"> A verification link has been sent to ' +
                        email +
                        ". Please check to verify.</div>"
                );
            } else {
                $("#feedbackPrompt").html(
                    '<div class="alert alert-danger" role="alert">' +
                        response +
                        "</div>"
                );
            }

            $("#feedbackPrompt").show();
            $("#login-btn").removeAttr("disabled");
            $(".button_spinner").hide();

            $("input#email").prop("disabled", false);
            $("input#password").prop("disabled", false);
        });
        return false;
    });

    // show/hide password
    const togglePassword = document.querySelector('#togglePassword');
    const password = document.querySelector('#password');

    togglePassword.addEventListener('click', function (e) {
        // toggle the type attribute
        const type = password.getAttribute('type') === 'password' ? 'text' : 'password';
        password.setAttribute('type', type);
        // toggle the eye slash icon
        this.classList.toggle('fa-eye-slash');
    });
});
