<script>

    $("#change_pass_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('change_password') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');

                $("#pass_submit_btn").text('Processing....');
                $("#pass_submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#pass_submit_btn").text('Change');
                $("#pass_submit_btn").prop('disabled', false);
                if (data.success) {
                    $(".error").text('');
                    if (data.response == false) {
                        $("#old_password_error").text(data.message)
                    } else {
                        $("#change_pass_form")[0].reset();
                        alertify.success("Password Changed Successfully!");
                    }
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#pass_submit_btn").text('Change');
                $("#pass_submit_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });

    $(document).on('submit', "#update_business_name_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('change_business_name') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');
                $("#b_submit_btn").text('Processing....');
                $("#b_submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#b_submit_btn").text('Update');
                $("#b_submit_btn").prop('disabled', false);
                $(".error").text('');

                if (data.success) {

                    alertify.success("Business Name Updated Successfully!");
                    window.location.reload();
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#b_submit_btn").text('Update');
                $("#b_submit_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $(document).find("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
</script>
