{
    template:'${tmpl}',
    data:function(){
        return {
            multipleSelection: [],
            isSingleSelect:false,
            ldms:[],
            orgs:[],
            dialogFormVisible:false,
            formMetaData:[],
            currentPath:null,
            positonChange:false,
            orgidchageto:null,
            refwhereorg:{isPosition:true},
            crow:null,
            selCompanyKey:null,
        }
    },
    mounted:function(){
        $("#treelist").css("min-height",(this.$root.outheight-60)+"px");
        $("#empdiv").css("min-height",(this.$root.outheight-60)+"px");
    },
    created:function(){
      var cmpid=this.$router.history.current.query.compid;
      this.selCompanyKey=cmpid;
    },
    methods:{
      beforesave(model){
        var currentData=this.$refs.orgtree.currentData;
        console.log(JSON.stringify(currentData));
        if(!currentData || !currentData.isPosition){
          this.$alert("增加人员前，请先选择岗位节点","提示");
          return false;
        }else{
          if(model){
            model.org_id=currentData.id;
          }
          return true;
        }
      },
       async saveclick(fm,prop){
        var self=this;
        var fm=this.$refs.orgform.getFormModel();
        this.$refs.orgform.overBtnLoading(prop);
        var currentData=self.$refs.orgtree.currentData;
        var currentNode=self.$refs.orgtree.currentNode;
        var parentdata=currentNode.parent.data;
        this.$refs.orgform.validate(async function(v){
          if(v){
            if(self.$refs.orgtree.isEdit){
              if(currentData.code=="root"){
                fm.orgpath=currentData.orgpath+fm.name;
              }else{
                 fm.orgpath=parentdata.orgpath+"/"+fm.name;
                 fm.org_id=parentdata.id;
              }
              fm.comkey=self.selCompanyKey;
              var porgs=await self.$root.postReq("/web/auth/orgCtl/update",fm);
              if(porgs.status==0 && porgs.data){
                  self.formatroles(porgs.data);
                  self.$refs.orgtree.updateKeyChildren(parentdata.id,porgs.data);
                  self.$refs.orgtree.hidedlg();
              }

            }else{
              if(currentData.code.indexOf("root")>=0){
                fm.orgpath=currentData.orgpath+fm.name;
              }else{
                fm.orgpath=currentData.orgpath+"/"+fm.name;
              }
              fm.org_id=currentData.id;
              fm.comkey=self.selCompanyKey;
              var porgs=await self.$root.postReq("/web/auth/orgCtl/create",fm);
              if(porgs.status==0 && porgs.data){
                self.formatroles(porgs.data);
                self.$refs.orgtree.updateKeyChildren(currentData.id,porgs.data);
                self.$refs.orgtree.hidedlg();
              }else{
                alert("保存失败");
              }

            }
   
          }
        });
       },
        async remove(node,data,cbk){
          var orgid=data.id;
          var currentNode=node;
          var parentdata=currentNode.parent.data;
          /*按照id去删除组织*/
          if(data.orgs && data.orgs.length>0){
            this.$alert("不能删除含有字元素的节点", '提示', {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
            return;
          }
          var rmdata=await this.$root.postReq("/web/auth/orgCtl/delete",{id:orgid});
          if(rmdata.status==0 && rmdata.data){
            this.formatroles(rmdata.data);
            this.$refs.orgtree.updateKeyChildren(parentdata.id,rmdata.data);
            this.$refs.orgtree.hidedlg();
          }else{
            alert("删除失败");
          }
        },
        formatroles(orgs){
          for(var org of orgs){
            if(org.roles && org.roles.length>0){
              org["Roles"]=[];
              org.roles.forEach((role)=>{
                org["Roles"].push(role.id);
              });
            }
            if(org.orgs && org.orgs.length>0){
              this.formatroles(org.orgs);
            }else{
              continue;
            }
          }
        },
        async fetchtreedata(node, resolve,expandkeys){
          if (node.level === 0) {
            var orgroot=await this.$root.postReq("/web/auth/orgCtl/initOrgs",{comkey:this.selCompanyKey});
            this.formatroles([orgroot.data]);
            expandkeys.push(orgroot.data.id);
            this.$refs.userGrid.linkInInitQuery("opath","/");
            return resolve([orgroot.data]);
          }else{
            console.log(node.level,node.id);
            console.log("dddddddddddddddddd");
            if(node.data.orgs && node.data.orgs.length){
              this.formatroles(node.data.orgs);
              console.log(JSON.stringify(node.data.orgs));
              resolve(node.data.orgs);
            }else{
              var idorg=await this.$root.postReq("/web/auth/orgCtl/findOrgById",{id:node.data.id});
              if(idorg.status==0 && idorg.data){
                this.formatroles(idorg.data);
                console.log(JSON.stringify(idorg.data));
                return resolve(idorg.data);
              }else{
                return resolve([]);
              }
       
            }
            return 
          }
        },
        async switchchange(srcprop,toprop,fm,ctlvisable){
          /*当前节点是否有子节点，如果有提示不能打开，设置为岗位*/
          var currentData=this.$refs.orgtree.currentData;
          var isedit=this.$refs.orgtree.isEdit;
          if(srcprop=="isPosition"){ 
            if(isedit && fm["isPosition"] && currentData.orgs && currentData.orgs.length>0){
              this.$alert("具有子部门的节点不能设置为岗位","提示");
              setTimeout(()=>{
                fm.isPosition=false;
                fm.isMain=false;
              },500);
              return false;
            }
            var toprops=toprop.split(",");
            if(fm[srcprop]){
              toprops.forEach((p)=>{
                ctlvisable[p]=true;
              });
              
            }else{
              toprops.forEach((p)=>{
                ctlvisable[p]=false;
              });
            }

          }else{
            var ismaindata=await this.$root.postReq("/web/auth/orgCtl/checkMainPosition",{org_id:currentData.id});
            if(ismaindata.status<0){
              this.$alert("当前节点下已经设置了负责人岗位，不能重复设置","提示");
              setTimeout(()=>{
                fm.isMain=false;
              },500);
            }else{
              setTimeout(()=>{
                fm.isMain=true;
              },500);
            }
          }
        },
        onsel(node,data){
          this.$refs.userGrid.linkInInitQuery("opath",data.orgpath);
          this.currentPath=data.orgpath;
          this.$refs.orgtree.isEdit=false;
        },
        loadforminfo(forminfo){
          console.log("forminfoforminfoforminfoforminfo");
          this.formMetaData=forminfo;
          var currentData=this.$refs.orgtree.currentData;
          this.currentPath=currentData.orgpath;
          setTimeout(()=>{
            if(!this.$refs.orgtree.isEdit){
              var fm=this.$refs.orgform.getFormModel();
              fm.id="";
              fm.code="";
              fm.name="";
              fm.isPosition=false;
              fm.isMain=false;
              fm.Roles=[];
              this.$refs.orgform.ctlVisable["Roles"]=false;
              this.$refs.orgform.ctlVisable["isMain"]=false;
            }else{
              this.$refs.orgform.setInitData(currentData);
              this.$refs.orgform.ctlVisable["Roles"]=currentData.isPosition;
              this.$refs.orgform.ctlVisable["isMain"]=currentData.isPosition;
            }
          },10);
        },
        initheight(lh,rh){
            $(lh).css("min-height",(this.$root.outheight-60)+"px");

        },
        async initdata(orgs){
            
            orgs.push();
            this.orgs=orgs;
        },
        validmethod:function (rule,value,callback) { 
            console.log("code validdate");
            var self=this;
            var fm=this.currentFrm.getFormModel();

            return callback("ddddd");
        },
        validateformmethod(rule,value,cbk){
            if(rule.field=="mobile"){
              if(/^1[23456789]\d{9}$/.test(value) == false){
                  return cbk("请检查号码格式!");
              }
            }
            return cbk();
          },
        onColFormater(row,column,cellvalue,index){
            if(column.property=="isEnabled"){
              if(row["isEnabled"]){
                return "正常";
              }else{
                return "停用";
              }
            }
            if(column.property=="isAdmin"){
              if(row["isAdmin"]){
                return "是";
              }else{
                return "否";
              }
            }
            if(column.property=="Roles"){
              var tmp=[];
              var ids=[];
              row["Roles"].forEach(s=>{
                tmp.push(s.name);
                ids.push(s.id);
              });
              row.roles=ids;
              return tmp.join(",");
            }
            return cellvalue;
          },
        handleSelectionChange:function(val){
            this.multipleSelection = val;
            console.log(val);
          },
         async changeok(){
            if(this.orgidchageto){
              var u=await this.$root.postReq("/web/auth/orgCtl/changePos",{orgid:this.orgidchageto,uid:this.crow.id});
              if(u.status==0){
                this.positonChange=false;
                this.orgidchageto=null;
                this.crow=null;
                this.$refs.userGrid.linkInInitQuery("opath",this.currentPath);
              }
            }
          },
        onRowClick(row,key,e){
            console.log("................////////////////////////////");
            if(key=="stopUser"){
              this.modelName="user";
              var url="/web/auth/userCtl/update";
              row.isEnabled=!row.isEnabled;
              this.$root.postReq(url,row).then(rd=>{
                console.log("................////////////////////////////");
                if(rd.status==0){
                  this.$refs.userGrid.fetchData();
                }
              });
            }
            if(key=="datapriv"){
              this.$router.push({path:"/datapriv",query:{
               uid:row.id,
               nickname:row.nickName,
             }});
            }
            if(key=="positionChange"){
              this.crow=row;
              this.positonChange=true;
              
            }
          }
    },
    computed: Vuex.mapState({
        headStyle:function(){
          return {"background-color":"white"};
        }
      }),
    vname:"gsb-comorgs"
}
