(function(){"use strict";u8.Crm.Widgets.EditWatchlistItemPopup=function(t,e){this.Widget(t,e)};u8.Crm.Widgets.EditWatchlistItemPopup.makeClass({name:"EditWatchlistItemPopup",deriveFrom:u8.Base.Widgets.Widget,events:["Close"]});u8.Crm.Widgets.EditWatchlistItemPopup.defaults={notifyOnOption:null,uid:null,callback:null,grid:null};$.extend(u8.Crm.Widgets.EditWatchlistItemPopup.prototype,{open:function(t,e,i){this.uid=t;this.callback=e;this.grid=i;u8.using({texts:["EditWatchlistItem_text"],templates:[{name:"editWatchlistItem_template",url:"~/crm/EditWatchlistItemPopup.utml.htm"}]},this._create.bind(this))},_create:function(){var t=this,e=$.extend({parent:this,position:"center",alignTo:"window",allowResize:true,allowMaximize:true,autoShow:false,modal:true,disposeOnClose:true,sizing:"fit",title:$t("TXT_EditWatchlistItem_text_1"),onCreate:function(e){t._popup=e;t._createView(t._popup.getContent())}},this.popup);this._popup=new u8.Base.Widgets.Popup("<div/>",e);this._popup.open()},_createView:function(t){var e=$("#fieldsPanel");var i=this;i.model={id:this.id,uid:this.uid,callback:this.callback,grid:this.grid,notifyOnOptions:[{key:"EveryChange",text:$t("TXT_EditWatchlistItem_text_3")},{key:"ChangeOnSpecificField",text:$t("TXT_EditWatchlistItem_text_4")}],notifyOnOptionValue:this.notifyOnOption,notificationAreaTypesValue:8,notificationAreaTypes:[{name:$t("TXT_EditWatchlistItem_text_6"),key:1},{name:$t("TXT_EditWatchlistItem_text_7"),key:2},{name:$t("TXT_EditWatchlistItem_text_8"),key:4},{name:$t("TXT_EditWatchlistItem_text_9"),key:8}],notifyOnOptionValueChange:function(t){var e=i._popup.children.get(i.id+"_fields");if(t.getValue()!="ChangeOnSpecificField"){e.element.parentElement.style.display="none"}else{e.element.parentElement.style.display="block"}},onSaveButtonClick:function(t){var e=i._popup.children.get(i.id+"_notifyOnOption").getValue();var s=i._popup.children.get(i.id+"_fields").getValue();var n=i._popup.children.get(i.id+"_notificationAreaTypes").getValue();var a=0;if(n!=null&&n.length>0){for(var o=0;o<n.length;o++){a|=n[o]}}var l=false;var r=i._popup.children.get(i.id+"_notificationAreaTypesErrorMessage");var p=i._popup.children.get(i.id+"_notifyOnOptionErrorMessage");var u=i._popup.children.get(i.id+"_fieldsErrorMessage");r.setVisible(false);p.setVisible(false);u.setVisible(false);if(a==0){r.setVisible(true);l=true}if(e==null){p.setVisible(true);l=true}if(e=="ChangeOnSpecificField"&&(s==null||s.length==0)){u.setVisible(true);l=true}if(l){return}u8.request("WatchListChannel.UpdateWatchlistItem",{request:{itemToWatchIdentifier:i.uid.uid.recordId,watchItemType:i.uid.uid.infoAreaId,notifyOn:e,creator:u8.session.identity.repId,notificationAreaType:a},selectedFields:s},function(t,e){if(e.error){}else{i._popup.close();var s="{0} {TXT_EditWatchlistItem_text_5}";s.format({0:u8.services.schema.idToLabel(i.uid.uid.infoAreaId)},function(t){u8.services.notification.info(t)});if(i.callback!=null){i.callback(i.grid,e.response.updatedWatchlistSubscriptionDetails)}}})}};u8.request("WatchListChannel.ViewAllWatchers",{request:{itemToWatchIdentifier:this.uid.uid.recordId,watchItemType:this.uid.uid.infoAreaId}},function(t,e){if(e.error){}else{if(e.response!=null&&e.response.watchlistSubscriptions!=null){for(var s=0;s<e.response.watchlistSubscriptions.length;s++){if(e.response.watchlistSubscriptions[s].createdBy==u8.session.identity.repId&&e.response.watchlistSubscriptions[s].watchedItemIdentifier==i.uid.uid.recordId&&e.response.watchlistSubscriptions[s].watchedItemType==i.uid.uid.infoAreaId){i._popup.children.get(i.id+"_notifyOnOption").setValue(e.response.watchlistSubscriptions[s].notifyOnOption);var n=new Array;if(e.response.watchlistSubscriptions[s].isDashboardNotification){n.push(1)}if(e.response.watchlistSubscriptions[s].isPopupNotification){n.push(2)}if(e.response.watchlistSubscriptions[s].isEmailNotification){n.push(4)}if(e.response.watchlistSubscriptions[s].isRecordOnlyNotification){n.push(8)}i._popup.children.get(i.id+"_notificationAreaTypes").setValue(n);e.response.watchlistSubscriptions[s].notifyOnSpecificFields.forEach(function(t){i._popup.children.get(i.id+"_fields").setValue(t.fieldDefinitionId)});return}}}}});t.addClass("u8-additemtowatchlistpopup").utml({name:"editWatchlistItem_template",data:i.model,itemOptions:{parent:this._popup}},function(){i._popup.autoSizeAndPosition().show()})}})})();