$(document).ready(function () {

    /*
    |--------------------------------------------------------------------------
    | SAVE SETTINGS
    |--------------------------------------------------------------------------
    */

    $('#resultSettingsForm').on(
        'submit',
        function (e) {

            e.preventDefault();

            const form = $(this);

            $.ajax({

                url: 'process/save-result-settings-process.php',

                type: 'POST',

                data: form.serialize(),

                dataType: 'json',

                beforeSend: function () {

                    Swal.fire({

                        title: 'Saving Settings...',

                        text: 'Please wait',

                        allowOutsideClick: false,

                        didOpen: () => {
                            Swal.showLoading();
                        }
                    });
                },

                success: function (response) {

                    Swal.close();

                    if (response.success) {

                        toastr.success(
                            response.message
                        );

                    } else {

                        toastr.error(
                            response.message
                        );
                    }
                },

                error: function (xhr) {

                    Swal.close();

                    let errorMessage =
                        'Something went wrong!';

                    if (
                        xhr.responseJSON &&
                        xhr.responseJSON.message
                    ) {

                        errorMessage =
                            xhr.responseJSON.message;
                    }

                    toastr.error(errorMessage);
                }
            });
        }
    );
});