(function(){u8.Crm.SerialEntryRelatedViewModel=function(e){this.Model($.extend({},e));if(!this.link){var t=u8.Base.Widgets.Tools.closest(this.parent,"SerialEntry");$assert(t,"Failed to create serial entry related view! SerialEntry does not exist.");this.link=t.model.link}var i=this.get("relatedHeaderCaption");if(i){this._createRelatedCaptionText(i)}this.bind("PropertyChange",this._onPropertyChange.bind(this))};u8.Crm.SerialEntryRelatedViewModel.makeClass({name:"SerialEntryRelatedViewModel",deriveFrom:u8.Base.Model});$.extend(u8.Crm.SerialEntryRelatedViewModel.prototype,{_onPropertyChange:function(e,t){if(t.propertyName=="uids"){var i=this,r=t.value,a=u8.Base.Widgets.Tools.closest(this.parent,"RelatedView");if(!r||!a){return}var s=i.get("detailsInfoAreaId");var n=r.find(function(e){return e&&e.infoAreaId==s});if(n){this.set("selectedCardIndex",0);this.set("detailsUid",n)}else{this.set("selectedCardIndex",1);var d=i.get("detailsAltInfoAreaId");this.set("detailsAltUid",r.find(function(e){return e&&e.infoAreaId==d}))}this.set("relatedItems",[]);r.push(this.link);var l=this.get("relatedViewQuery");if(l){this._createRelatedItems(l,r)}}},_createRelatedCaptionText:function(e){var t=this;u8.services.tableCaptions.read($.extend({uid:this.link},e),function(e,i){if(!i.tableCaption){return}t.set("relatedHeaderCaptionText",i.tableCaption.getText())})},_createRelatedItems:function(e,t){var i=this,r=new u8.Crm.QueryCommand(e);r.load({},a);function a(e,i){if(!i.error){u8.services.queries.setParameters({query:e,uids:t},s)}}function s(e,t){if(!t.error){r.execute({},n)}}function n(e,t){var r=t.resultSet;if(!r){return}var a=r.getUidsAt(null,0).removeDuplicates(u8.equalsRecordUid);i.set("relatedUids",a);i.set("selectedCardIndexForRelatedItems",a.length>0?0:1)}}})})();