// Login js
$(document).on("click", "#admin_login_btn", function (e) {
    $("#admin_login_form").validate({
        rules: {
            email: {
                required: true,
            },
            password: {
                required: true,
            },
        },
        messages: {
            email: {
                required: "Please enter the email address.",
            },
            password: {
                required: "Please enter the password.",
            },
        },
        submitHandler: function (form) {
            e.preventDefault();
            let base_url = url_path;
            var formData = new FormData(form);
            $.ajaxSetup({
                headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
                        "content"
                    ),
                },
            });
            $.ajax({
                url: base_url + "/check_login",
                type: "POST",
                data: formData,
                beforeSend: function () {
                    $("#admin_login_btn").text("Please wait...");
                    $("#admin_login_btn").attr("disabled", true);
                },
                processData: false,
                contentType: false,
                success: function (response) {
                    if (response.status == 200) {
                        $("#admin_login_btn").prop("disabled", false);
                        $("#admin_login_btn").text("Login");
                        window.location.href = base_url + "/dashboard";
                    }
                    if (response.status == 401) {
                        toastr.error(response.message);
                        form.reset();
                        $("#admin_login_btn").prop("disabled", false);
                        $("#admin_login_btn").text("Sign In");
                    }
                },
            });
        },
    });
});

// forgot password js
$(document).on("click", "#forgot_password_btn", function (e) {
    let base_url = url_path;
    $("#forgot_pass_form").validate({
        ignore: [],
        debug: false,
        rules: {
            email: {
                required: true,
            },
        },
        messages: {
            email: {
                required: "Please Enter email",
            },
        },
        submitHandler: function (form) {
            var formData = new FormData(form);
            e.preventDefault(),
                $.ajaxSetup({
                    headers: {
                        "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
                            "content"
                        ),
                    },
                });
            $.ajax({
                url: base_url + "/send_otp",
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (response) {
                    console.log(response);
                    if (response.status == 200) {
                        form.reset();
                        toastr.success("Otp send it your mail id please check");
                        setTimeout(function () {
                            //                                toastr.info('Please check your email to reset your password. The link is valid for 5 minutes.');
                            window.location.href = base_url + "/otp";
                        }, 1000);
                    } else if (response.status == 404) {
                        toastr.error("This email id is not exits");
                    } else {
                        toastr.error("Something went wrong");
                    }
                },
            });
        },
    });
});

// otp varification
$(document).on("click", "#otp_verify_button", function (e) {
    e.preventDefault();

    // Get base URL
    let base_url = url_path;

    // Get admin ID
    var id = $("#admin_otp_id").val();

    // Get OTP by concatenating values of all OTP input fields
    var otp = $(".otp")
        .map(function () {
            return this.value;
        })
        .get()
        .join("");

    // Send AJAX request for OTP verification
    $.ajax({
        url: base_url + "/otp_verify",
        type: "POST",
        data: {
            id: id,
            otp: otp,
            _token: $('meta[name="csrf-token"]').attr("content"),
        },
        success: function (response) {
            if (response.status == 200) {
                // Display success message
                toastr.success("Otp Verify Successfully");
                // Redirect to the dashboard after a delay
                setTimeout(function () {
                    window.location.href = base_url + "/password_reset";
                }, 1000);
            } else if (response.status == 401) {
                // Display error message for invalid OTP or expired
                toastr.error(response.message);
            } else {
                // Display error message for other issues
                toastr.error("Something went wrong");
            }
        },
    });
});

$(document).on("input", ".otp", function () {
    this.value = this.value.replace(/[^0-9]/g, "");

    if (this.value.length >= this.maxLength) {
        $(this).next(".otp").focus();
    }
});

// Reset Password
$(document).on("click", "#password_reset", function (e) {
    $("#password_reset_form").validate({
        rules: {
            password: {
                required: true,
                minlength: 8,
            },
            confirm_password: {
                required: true,
                equalTo: "#password",
            },
        },
        messages: {
            password: {
                required: "Please enter a password.",
                minlength: "The password field must be at least 8 characters.",
            },
            confirm_password: {
                required: "Please Confirm Your Password",
                equalTo: "Your Password Do Not Match",
            },
        },
        submitHandler: function (form) {
            e.preventDefault();
            let base_url = url_path;
            var formData = new FormData(form);

            $.ajaxSetup({
                headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
                        "content"
                    ),
                },
            });
            $.ajax({
                url: base_url + "/password_update",
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (response) {
                    if (response.status_code == 200) {
                        $("#password_reset").prop("disabled", false);
                        $("#password_reset").text("Login");
                        toastr.success(response.message);
                        setTimeout(function () {
                            window.location.href = base_url + "/";
                        }, 1000);
                    }
                    if (response.status_code == 401) {
                        toastr.error(response.message);
                        form.reset();
                        $("#password_reset").prop("disabled", false);
                        $("#password_reset").text("Sign In");
                    }
                },
            });
        },
    });
});

$("#passwordToggle").click(function () {
    var passwordInput = $("#password");
    var eyeIcon = $("#passwordToggle");

    if (passwordInput.attr("type") === "password") {
        passwordInput.attr("type", "text");
        eyeIcon.removeClass("fa-eye-slash").addClass("fa-eye");
    } else {
        passwordInput.attr("type", "password");
        eyeIcon.removeClass("fa-eye").addClass("fa-eye-slash");
    }
});
