"use strict";

// Class definition
var Setting = function () {
    const form = document.getElementById('form-setting');
  
    
    const handleCheckboxes=()=>{
        const motamedEl=form.querySelector("#Motamed_Active");
        const moadiEl=form.querySelector("#Moadi_Active");
        moadiEl.addEventListener("change",function(e){
            if (this.checked) {
                $("#Motamed_Active").prop('checked', false);
            } 
            else {
                $("#Motamed_Active").attr('checked',true);
            }
        });
        motamedEl.addEventListener("change",function(e){
            if (this.checked) {
                $("#Moadi_Active").prop('checked', false);
            } 
            else {
                $("#Moadi_Active").attr('checked',true);
            }
        });
        
    }
    const handleSubmit=(e)=>{
        
        
        // Define variables
        const submitButton = form.querySelector('#button_submit');

        let message='این فیلد ضروریست';
        let validatorMoadi = FormValidation.formValidation(
            form,
            {
                fields: {
                    "Moadi.WebServiceAddress": {
                        validators: {
                            notEmpty: {
                                message
                            }
                        }
                    },
                    'Moadi.MemoryId': {
                        validators: {
                            notEmpty: {
                                message
                            }
                        }
                    },
                    'Moadi.PrivateKey': {
                        validators: {
                            notEmpty: {
                                message
                            }
                        }
                    }
                },
                plugins: {
                    trigger: new FormValidation.plugins.Trigger(),
                    bootstrap: new FormValidation.plugins.Bootstrap5({
                        rowSelector: '.fv-row',
                        eleInvalidClass: '',
                        eleValidClass: ''
                    })
                }
            }
        );  
        let validatorMotamed = FormValidation.formValidation(
            form,
            {
                fields: {

                    "Motamed.WebServiceAddress": {
                        validators: {
                            notEmpty: {
                                message
                            }
                        }
                    },
                    "Motamed.PrivateKey": {
                        validators: {
                            notEmpty: {
                                message
                            }
                        }
                    },
                    "Motamed.UserName": {
                        validators: {
                            notEmpty: {
                                message
                            }
                        }
                    },
                    "Motamed.Password": {
                        validators: {
                            notEmpty: {
                                message
                            }
                        }
                    }
                },
                plugins: {
                    trigger: new FormValidation.plugins.Trigger(),
                    bootstrap: new FormValidation.plugins.Bootstrap5({
                        rowSelector: '.fv-row',
                        eleInvalidClass: '',
                        eleValidClass: ''
                    })
                }
            }
        );
        
        // Handle submit button
        submitButton.addEventListener('click', (e,t) => {
           e.preventDefault();
            // Validate form before submit
           
            if($("#Motamed_Active").prop('checked')==false && $("#Moadi_Active").prop('checked')==false)
            {
                toastr.warning('یکی از متدهای ارسال به سامانه باید فعال باشد');
                return;
            }
            let validator=validatorMoadi;
            if($("#Motamed_Active").prop('checked'))
                validator=validatorMotamed;
                
            if (validator) {
                validator.validate().then(function (status) {
                    if (status == 'Valid') {
                        submitButton.setAttribute('data-kt-indicator', 'on');

                        // Disable submit button whilst loading
                        submitButton.disabled = true;
                        $(form).submit();
                        // setTimeout(function () {
                        //     submitButton.removeAttribute('data-kt-indicator');
                        //
                        //     Swal.fire({
                        //         text: "Form has been successfully submitted!",
                        //         icon: "success",
                        //         buttonsStyling: false,
                        //         confirmButtonText: "Ok, got it!",
                        //         customClass: {
                        //             confirmButton: "btn btn-primary"
                        //         }
                        //     }).then(function (result) {
                        //         if (result.isConfirmed) {
                        //             // Enable submit button after loading
                        //             submitButton.disabled = false;
                        //
                        //             // Redirect to customers list page
                        //             // window.location = form.getAttribute("data-kt-redirect");
                        //         }
                        //     });
                        // }, 2000);
                    } else {
                        Swal.fire({
                            text: "Sorry, looks like there are some errors detected, please try again.",
                            icon: "error",
                            buttonsStyling: false,
                            confirmButtonText: "Ok, got it!",
                            customClass: {
                                confirmButton: "btn btn-primary"
                            }
                        });
                    }
                });
            }
        })
    }
    // Public methods
    return {
        init: function () {
            // Init forms
            

            // Handle forms
            handleCheckboxes();
            handleSubmit();
        }
    };
}();
// On document ready
KTUtil.onDOMContentLoaded(function () {
    Setting.init();
});
