(function(){"use strict";u8.Crm.Widgets.BulkOperations=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.BulkOperations.defaults,t));this._window=$w(e);this.commandBindings={ShowSearch:this._onShowSearch.bind(this),Update:{execute:this._onCommandUpdate.bind(this),queryState:this._onCommandUpdateQueryState.bind(this)},"Crud/Delete":{execute:this._onCommandDelete.bind(this),queryState:this._onCommandDeleteQueryState.bind(this)},DeleteNoCom:{execute:this._onCommandDeleteNoCom.bind(this),queryState:this._onCommandDeleteNoComQueryState.bind(this)}};this.settings=u8.tools.defaults(u8.services.settings.get("BulkOperations")||{},{infoAreaId:""});this._create()};u8.Crm.Widgets.BulkOperations.defaults={};u8.Crm.Widgets.BulkOperations.makeClass({name:"BulkOperations",deriveFrom:u8.Base.Widgets.Widget});$.extend(u8.Crm.Widgets.BulkOperations.prototype,{_create:function(){u8.using({types:["SearchView","BulkEditor"],texts:["BulkOperations"]},function(){this._createViewModel()}.bind(this))},_createViewModel:function(){this.viewModel={id:this.id,isDefault:this.isDefault};u8.request("SearchChannel.GetSearchableInfoAreaIds",{},function(e,t){var s=t.response;if(!s){return}this.viewModel.infoAreas=s.map(function(e){return{infoAreaId:e,infoAreaLabel:"{label} ({id})".format({label:u8.services.schema.idToLabel(e),id:e})}}).sort(function(e,t){return u8._.compare(e.infoAreaLabel,t.infoAreaLabel)});this.$(this.element).utml({url:"~/crm/BulkOperations.utml.htm",itemOptions:{parent:this},data:this.viewModel},function(){var e=this._window.$widget(this.id+"_infoAreas");var t=this.settings.infoAreaId;if(t&&s.indexOf(t)>=0){e.setValue(this.settings.infoAreaId)}else{e.setSelectedIndex(0)}}.bind(this))}.bind(this))},_deleteRecords:function(e){u8.Base.Widgets.MessageBox.showQuestion({message:"TXT_BulkOperations_1".format({recordCount:this._getRecordCount()}),buttons:"Ok,Cancel"},function(t,s){if(s.command!="Ok"){return}var i=this._search.getWidget("results");var n=new u8.Crm.ListReader({arguments:i.model.getReaderArguments()});var o=$.extend(n.arguments,{deletionType:e});u8.request("BusinessObjectsChannel.BulkDelete",{options:o},function(e,t){if(t.response&&u8.isValidRecordUid(t.response.uid)){u8.services.notification.info("TXT_BulkOperations_2",true)}})}.bind(this))},_getRecordCount:function(){var e=this._search.getWidget("results");var t=e.model.resultSet;var s=t.getTotalNumberOfRows();if(typeof s!=="undefined"){return s}s=t.getNumberOfRows();if(t.canLoadMore()){s+="+"}return s},_onShowSearch:function(){if(this._search){this._search.dispose()}var e=this._window.$widget(this.id+"_infoAreas").getValue();if(!e){return}this.settings.infoAreaId=e;u8.services.settings.set("BulkOperations",this.settings);this._search=new u8.Crm.Widgets.SearchView(this._window.$el(this.id+"_searchView"),{parent:this,name:e,showRelatedView:false,flavor:"BulkOperations",form:{useAutoSearch:false,showFilters:true},viewOptions:{list:{selectionMode:null,canEdit:false,grid:{useDefaultActionOnDblClick:false,showMenu:false,showDefaultAction:false,showLinks:false,countRows:true}}},resultHeader:{headerGroupName:"BulkOperations",headerName:"SearchResults"}})},_onCommandDelete:function(){this._deleteRecords("MarkAsDeleted")},_onCommandDeleteNoCom:function(){this._deleteRecords("Delete")},_onCommandUpdate:function(){var e=this._search.getWidget("results");u8.Crm.Widgets.BulkEditor.showAsPopup({title:"TXT_BulkOperations_5",submitButtonText:"TXT_BulkOperations_8",editorOptions:{listModel:e.model,saveProvider:new u8.Crm.Widgets.BulkOperationsSaveProvider({recordCount:this._getRecordCount(),listModel:e.model}),modelOptions:{useDynamicFieldList:true}}})},_onCommandUpdateQueryState:function(e,t,s){var i=this._search.infoAreaId;s.disabled=!u8.services.schema.getInfoArea(i).canUpdate()},_onCommandDeleteQueryState:function(e,t,s){var i=this._search.infoAreaId;s.disabled=!u8.services.schema.getInfoArea(i).canDelete()},_onCommandDeleteNoComQueryState:function(e,t,s){var i=this._search.infoAreaId;s.disabled=!u8.services.schema.getInfoArea(i).canDelete()}});u8.Crm.Widgets.BulkOperationsSaveProvider=function(e){$.extend(this,e)};$.extend(u8.Crm.Widgets.BulkOperationsSaveProvider.prototype,{save:function(e,t){var s=this,i=[];for(var n in e.values){i.push({field:n,value:e.values[n]})}u8.Base.Widgets.MessageBox.showQuestion({message:"TXT_BulkOperations_0".format({recordCount:this.recordCount}),buttons:"Yes,No"},function(e,n){if(n.command!="Yes"){return}var o=new u8.Crm.ListReader({arguments:s.listModel.getReaderArguments()});var r=$.extend(o.arguments,{changes:i});u8.request("BusinessObjectsChannel.BulkUpdate",{options:r},function(e,s){if(s.errors){return}t(s);u8.services.notification.info($t("TXT_BulkOperations_2"),true)})})}})})();