(function(){u8.Crm.Widgets.RepPicker=function(e,t){this.Widget(e,t);this.commandBindings={AddGroup:this._onCommandGroup.bind(this),RemoveGroup:this._onCommandGroup.bind(this),AddRep:this._onCommandRep.bind(this),RemoveRep:this._onCommandRep.bind(this)};u8.using({texts:"RepUtils_text"},this._create.bind(this))};u8.Crm.Widgets.RepPicker.makeClass({name:"RepPicker",deriveFrom:u8.Base.Widgets.Widget,events:["ValueChange"]});$.extend(u8.Crm.Widgets.RepPicker.prototype,{update:function(e){this._create()},getValue:function(){return{repId:this._repId,groupId:this._groupId}},_create:function(){this._createModel(this._createView.bind(this))},_createModel:function(e){var t=this;this._model={};this._loadGroups(function(){t._model.form=t._createFormModel()}.chain(e))},_createView:function(){this.empty();var e=this.$("<div/>").appendTo(this.element);this._form=new u8.Base.Widgets.Form(e,this._model.form)},_loadGroups:function(e){var t=this;u8.services.reps.getGroups(function(e,o){var i=u8.dict(o.groupsById).values();i.push({repId:-1,name:$t("TXT_RepUtils_text_2")});t._model.groups=i;t._model.currentGroup=o.groupsById[u8.session.identity.orgGroupId]}.chain(e))},_onComboGroupsValueChange:function(e,t){this._groupId=e.getValue();this._repId=-1;var o=this._form.getWidget("comboReps");var i=false;if(this._groupId<0){o.attr({disabled:true,items:[]})}else{u8.services.reps.getGroupMembers(this._groupId,"!Group",function(e,t){var n=u8.dict(t.repsById).values();o.attr({items:n,disabled:false});var s=o.getValue();if(s!=this._repId){this._repId=typeof s=="undefined"?-1:s;this._onValueChange();i=true}}.bind(this))}if(!i){this._onValueChange()}},_onComboRepsValueChange:function(e,t){this._repId=e.getValue();this._onValueChange()},_onValueChange:function(){this.raise("ValueChange",{})},_onCommandGroup:function(e,t,o){o.repId=this._form.getWidget("comboGroups").getValue();o.stopPropagation=false},_onCommandRep:function(e,t,o){o.repId=this._form.getWidget("comboReps").getValue();o.stopPropagation=false},_createFormModel:function(){var e={context:{data:{}},widgets:{root:{type:"TablePanel",attr:{class:"u8-tablepanel-fields"},options:{columns:[{},{}],rows:[{},{},{},{},{},{}],items:[{widget:"labelGroups",row:0,column:0},{widget:"imageGroups",row:0,column:1},{widget:"comboGroups",columnSpan:2,row:1,column:0},{widget:"buttonAddGroup",row:2,column:0},{widget:"buttonRemoveGroup",css:{textAlign:"right"},row:2,column:1},{widget:"labelReps",row:3,column:0},{widget:"imageReps",row:3,column:1},{widget:"comboReps",row:4,column:0,columnSpan:2},{widget:"buttonAddRep",row:5,column:0},{widget:"buttonRemoveRep",css:{textAlign:"right"},row:5,column:1}]}},labelGroups:{type:"Label",options:{textId:"TXT_RepUtils_text_4"}},imageGroups:{type:"Image",css:{textAlign:"right"},options:{source:"u8-repgroup-s.png"}},comboGroups:{type:"ComboBox",options:{items:this._model.groups,value:this._model.currentGroup?this._model.currentGroup.repId:-1,keyProperty:"repId",textProperty:"name",onValueChange:this._onComboGroupsValueChange.bind(this)}},buttonAddGroup:{type:"Button",options:{textId:"TXT_RepUtils_text_5",command:"AddGroup"}},buttonRemoveGroup:{type:"Button",options:{textId:"TXT_RepUtils_text_6",command:"RemoveGroup"}},labelReps:{type:"Label",options:{textId:"TXT_RepUtils_text_3"}},imageReps:{type:"Image",css:{textAlign:"right"},options:{source:"u8-rep-s.png"}},comboReps:{type:"ComboBox",options:{disabled:true,keyProperty:"repId",textProperty:"name",onValueChange:this._onComboRepsValueChange.bind(this)}},buttonAddRep:{type:"Button",options:{textId:"TXT_RepUtils_text_5",command:"AddRep"}},buttonRemoveRep:{type:"Button",options:{textId:"TXT_RepUtils_text_6",command:"RemoveRep"}}},parent:this};return e}})})();