$(document).ready(function() {
    var advanced,
        advancedPanel = {
                fields:{
                    recordings: 'select#recordings',
                    region : '#softphoneCheck_modal input[name="determine_region"]'
                },
                buttons: {
                    softphoneCheck: '#softphoneCheck',
                    clear_Cache: '#clear_cache',
                    // recording_mp3: '#mp3',
                    // recording_stereo: '#stereo',
                    // recordings: "input[name=recordings]",
                    saveWssPort: $('#save_softphone_port'),
                    download_log: "input[data-bind='download_log']"
                },
                softphoneModal: {
                    self: '#softphoneCheck_modal',
                    miniHttpsServ: '#softphoneCheck_modal #miniHttpsServ',
                    TLSminiServ: '#softphoneCheck_modal #TLSminiServ',
                    configSertificate: '#softphoneCheck_modal #configSertificate',
                    externalWSSPort: '#externalWSSPort',
                    notSupportedVersion: '#not_supported_version',
                    CurrentAstVersion: '#CurrentAstVersion'
                },
                help:{
                    softphoneCheckHelp: '#AdvancedPanel span[id="check-help"]',
                    clearCacheHelp: '#AdvancedPanel span[id="clear_cache-help"]',
                    timeoutHelp: '#AdvancedPanel span[id="timeout-help"]',
                    recordingHelp: '#AdvancedPanel span[id="recording-help"]',
                    linksHelp: '#AdvancedPanel span[id="links-help"]',
                    regionHelp: '#AdvancedPanel span[id="region-help"]'
                },

            init: function () {
                advanced = this;
                translate(this.help);
                this.bindUIActions();
                if ($(advanced.fields.recordings).val() == 'mp3' || $(advanced.fields.recordings).val() == 'stereo') {
                    advanced.hideYesButton();
                }
            },
            /**
             * обработчики кнопок
             */
            bindUIActions: function () {
//advanced_1
                $(advanced.buttons.softphoneCheck).on('click',function () {
                    let checkbtn = this;
                    if(!$(checkbtn).hasClass('disabled')) {
                        $(checkbtn).addClass('disabled');
                        var promise = $.ajax({
                            url: "ajax.php",
                            type: "POST",
                            data: {
                                command: 'getSoftphoneData',
                                module: window.modulename
                            }
                        });
                        promise.then(function (data) {
                            $(checkbtn).removeClass('disabled');
                            if(data['ASTVERSION']) {
                                $(advanced.softphoneModal.self).modal('show');
                                if (data['miniHttpServer']) {
                                    $(advanced.softphoneModal.miniHttpsServ + ' .fa-check-circle').removeClass(' hidden');
                                } else {
                                    $(advanced.softphoneModal.miniHttpsServ + ' .fa-exclamation-triangle').removeClass(' hidden');
                                }
                                if (data['TLSminiServ']) {
                                    $(advanced.softphoneModal.TLSminiServ + ' .fa-check-circle').removeClass(' hidden');
                                } else {
                                    $(advanced.softphoneModal.TLSminiServ + ' .fa-exclamation-triangle').removeClass(' hidden');
                                }
                                if (data['configSertificate']) {
                                    $(advanced.softphoneModal.configSertificate + '. fa-check-circle').removeClass(' hidden');
                                } else {
                                    $(advanced.softphoneModal.configSertificate + ' .fa-exclamation-triangle').removeClass(' hidden');
                                }
                                if (data['externalWssPort'].length) {
                                    $(advanced.softphoneModal.externalWSSPort).val(data['externalWssPort']);
                                }
                                if ($(advanced.softphoneModal.externalWSSPort).val() !== undefined && $(advanced.softphoneModal.externalWSSPort).val().length === 0) {
                                    warnInvalid($(advanced.softphoneModal.externalWSSPort));
                                }
                            }else{
                                $(advanced.softphoneModal.notSupportedVersion).modal('show');
                            }
                        }, function (err) {
                            console.error(err);
                        });
                    }

                });
                $(advanced.softphoneModal.externalWSSPort).on('change',function () {
                    if ($(advanced.softphoneModal.externalWSSPort).val().length === 0 || $(advanced.softphoneModal.externalWSSPort).val() ==="") {
                        warnInvalid($(advanced.softphoneModal.externalWSSPort));
                    }else{
                        $.ajax({
                            url: "ajax.php",
                            type: "POST",
                            data: {
                                command: 'submit',
                                module: 'iub24',
                                wssport: $('#externalWSSPort').val()
                            },
                            success: function () {
                                $('#externalWSSPortSave .fa-check-circle').removeClass('hidden');
                            }
                        });
                    }
                });
                $(advanced.softphoneModal.externalWSSPort).on('focus',function () {
                    $('#externalWSSPortSave .fa-check-circle').addClass('hidden');
                });
//END_advanced_1

                $(advanced.fields.recordings).on('change',function() {
                    if ($(this).val() == 'mp3' || $(this).val() == 'stereo') {
                        advanced.hideYesButton();
                    } else {
                        advanced.showYesButton();
                    }
                });
                $(advanced.buttons.clear_Cache).on('click',function () {
                    let btn = this;
                    $(btn).prop('disabled',true);
                   advanced.clearCache().then(function () {
                       fpbxToast('Complete', 'Cache Clear', 'success');
                       $(btn).prop('disabled',false);
                   });
                });

                $(document.body).on('click', advanced.buttons.download_log, function() {
                    var fileLink = $('select[name="log-files"]').find(':selected').attr("data-link");
                    window.location.href = "ajax.php?module=iub24&command=get_record&record=" + fileLink + "&zip=y";
                });
            },
            showYesButton: function() {
                $('label[for="links_yes"]').show();
            },
            hideYesButton: function() {
                $('label[for="links_yes"]').hide();
                $('input[id="links_yes"]').prop("checked",false);
                $('input[id="links_no"]').prop("checked",true);
                $('input[name="links"]').trigger('change');
            },
            /**
             * Очистка кеша ASTDB
             */
            clearCache: function() {
                let promise = $.ajax({
                    url: "ajax.php",
                    type: "POST",
                    data: {
                        command: 'clearCache',
                        module: window.modulename
                    },
                });
                return promise;
            },
    };

    advancedPanel.init();

});