(function(){"use strict";u8.Crm.Widgets.SessionSettingsBox=function(e){$.extend(this,u8.Crm.Widgets.SessionSettingsBox.defaults,e);this._elWindow=e.elWindow||window;this.$=this._elWindow.$;this._settings=null;this._container=null;this.WidgetContainer();this.bind();this.commandBindings={apply:this._onCommandApply.bind(this),cancel:this._onCommandCancel.bind(this),toggleCheckBoxes:this._onCommandToggleCheckBoxes.bind(this),ChangeLanguage:this._onCommandChangeLanguage.bind(this)}};u8.Crm.Widgets.SessionSettingsBox.defaults={template:"~/crm/sessionSettings.utml.htm"};u8.Crm.Widgets.SessionSettingsBox.makeClass({name:"SessionSettingsBox",deriveFrom:u8.Base.Widgets.WidgetContainer,events:["Close"]});$.extend(u8.Crm.Widgets.SessionSettingsBox.prototype,{open:function(){u8.request("ApplicationChannel.GetSessionSettings",{options:{includeRoles:this.changeRoles,includePhoneProfiles:this.changePhoneProfile,includeLanguages:this.changeLanguage}},e.bind(this));function e(e,n){this._settings=n.response||{};var i=this._settings.roles&&this.changeRoles||this._settings.phoneProfiles&&this.changePhoneProfile||this._settings.languages&&this.changeLanguage;if(i){return this._showUi()}this.raise("Close",{cancel:true})}},dispose:function(){if(this._isDisposed){return}this.WidgetContainer_dispose();if(this._container){this._container.empty();this._container=null}this._elWindow=null;this.$=null},_showUi:function(e){if(!this._container){this._container=this.$("<div class='u8-login-container' />").uniqueId().css("visibility","hidden").appendTo(this._elWindow.document.body)}else{this._container.empty().css("visibility","hidden");this.children.clear()}var n=this,i=u8.resolveUrl(this.template),t=this._container.get(0);u8.using({texts:["login_text","changePhoneProfile_text"]},this._buildTemplateModel.bind(this,function(s){n._model=s;u8.Base.Templates.execute(t,{url:i,name:"sessionSettings",data:s,itemOptions:{parent:n,context:s}},function(){n._onTemplateProcessed()}.chain(e))}))},_buildTemplateModel:function(i){var t=this._settings.phoneProfiles,s=this._settings.roles,o=this._settings.languages||[],a=this._settings.currentLanguage,h={name:"",label:$t("TXT_changePhoneProfile_text_2")},l=n(a);e(o);if(s&&s.length>1){s=s.sort(u8._.cmp("label"))}if(!o.some(u8._.eq("key",l))){o.unshift({key:l,name:$t("TXT_login_text_27")})}var g={showPhoneProfiles:this.changePhoneProfile&&t&&t.length>0,phoneProfiles:[h].concat(t),phoneProfile:t&&t.find(u8._.attr("isChecked"))||h,showRoles:this.changeRoles&&s&&s.length>0,roles:s||[],headerText:$t("TXT_login_text_24"),showLanguages:this.changeLanguage&&o.length>0,languages:o,languageKey:l};g.roles.each(function(e){e.checked=e.isChecked;e.disabled=!e.canBeUnchecked});i(g)},_onTemplateProcessed:function(){var e=this;this._container.hide().css({visibility:"visible"}).fadeIn("normal",function(){var n=u8.Base.Widgets.Tools.getFirstFocusableDescendant(e);if(n){n.focus()}})},_onCommandApply:function(){var e={};if(this.changePhoneProfile){e.phoneProfile=this._elWindow.$v("comboPhoneProfile")}if(this.changeRoles){var n=this._elWindow.$widget("listRoles"),i=n&&n.items||[];e.roleIds=i.filterAndMap(u8._.attr("checked"),u8._.attr("roleId"))}this.raise("Close",e)},_onCommandCancel:function(){this.raise("Close",{cancel:true})},_onCommandToggleCheckBoxes:function(e,n){var i=e.getValue();var t=this._elWindow.$widget("listRoles");for(var s=0;s<t.items.length;s++){var o=t.items[s];if(o.disabled||o.checked==i){continue}t.toggleCheck(s)}},_onCommandChangeLanguage:function(){var e=this,n=this._model.languages.find(u8._.eq("key",this._model.languageKey)),i=n&&n.uid;u8.request("ApplicationChannel.SetCurrentLanguage",{language:i},function(n,i){u8.session.refresh({reason:"language"});e.open()})}});function e(e){e=e||[];e.forEach(function(e){e.key=n(e.uid)});return e}function n(e){return e?(e.code||"")+"/"+(e.no||0):null}})();