<template>
  <div>
      <div style="height: 280px;overflow-y: auto;overflow-x: hidden">
        <el-tree :data="orgTree" :load="loadData" lazy ref="orgTree" :props="orgTreeProps" node-key="orgId" @node-click="onTreeChange"
          :show-checkbox="true" :check-strictly="true"></el-tree>
      </div>
  </div>
</template>
<script>
export default{
  name: 'AssignOrgDimension',
  components:{

  },
  props:{
    objectId:{
      type:String,
      default(){
        return '';
      }
    },
    dimensionWdCode:{
      type:String,
      default(){
        return '';
      }
    }
  },
  data(){
    return{
      orgTree: [],
      orgTreeProps: {
        children: 'children',
        label: 'orgName'
      },
      param: {
        objectId: this.objectId,
        dimensionWdCode: this.dimensionWdCode,
        parentId: ""
      },
      checkedOrgs: [],
      parentOrgIds: []
    }
  },
  computed:{

  },
  created:function(){

  },
  beforeMount:function(){

  },
  mounted:function(){

  },
  beforeDestroy:function(){

  },
  destroyed:function(){

  },
  methods:{
    // 加载当前组织子节点
    loadData(currentOrg, callback) {
      this.param.parentId = currentOrg.data.orgId;
      $store.dispatch("QueryOrgDimensionTree", this.param).then(response => {
        if (response.statusCode == "0") {
          var childrenOrg = response.dataResult.datas;
          let defaultValue = response.dataResult.defaultValue;
          this.checkedOrgs = [];
          this.$refs["orgTree"].getCheckedNodes().forEach(org => {
            this.checkedOrgs.push(org.orgId);
          });
          this.checkedOrgs = this.checkedOrgs.concat(defaultValue);
          this.$refs["orgTree"].setCheckedKeys(this.checkedOrgs);

          for (const key in childrenOrg) {
            let org = childrenOrg[key];
            if(org.checked === 0){
              org.disabled = true;
            }
          }
          callback(childrenOrg);
        } else {
          this.$Message.error(response.msg);
        }
      });
    },

    onOk() {
      //添加或修改角色权限信息
      // this.checkedOrgs = this.checkedOrgfilter(this.$refs["orgTree"].getCheckedNodes());
      this.checkedOrgs = this.$refs["orgTree"].getCheckedNodes();
      this.parentOrgIds = [];
      this.checkedOrgs.forEach(org => {
      //   let parentOrgId = this.findParentOrgId(org.parentId);
      //   if(parentOrgId == null){
      //     // 没找到父Id
      //     if(!this.contains(this.parentOrgIds, org.orgId)){
            this.parentOrgIds.push(org.orgId);
      //     }
      //   }
      });

      let param ={
        dimensionWdCode: this.dimensionWdCode,
        objectId: this.objectId,
        dimensionValueList: this.parentOrgIds
      };

      $store.dispatch("UpdateOrgDimension", param).then(response => {
        if (response.statusCode == "0") {
          this.$Message.success({
            content: this.$t("module.dimension.warning.updateSuccessfully"),
            duration: 3
          });
        }else{
          this.$Message.warning({
            content: response.msg,
            duration: 3
          });
        }
      });
    },
    // 级联查找父组织ID
    // findParentOrgId(parentId){
    //   for (const index in this.checkedOrgs) {
    //     const org = this.checkedOrgs[index];
    //     if(org.orgId == parentId){
    //       let id = this.findParentOrgId(org.parentId);
    //       if(id == null){
    //         // 没找到父组织返回当前组织ID
    //         return org.orgId;
    //       }else{
    //         // 返回父组织ID
    //         return id;
    //       }
    //     }
    //   }
    //   // 没找到父组织返回null
    //   return null;
    // },
    // 判断数组中是否存在对象
    // contains(arr, obj) {
    //   for (const key in arr) {
    //     if (arr[key] === obj) {  
    //       return true;  
    //     }
    //   }
    //   return false;
    // },
    // checkedOrgfilter(checkedNodes){
    //   let checkedOrgs = [];
    //   for (const key in checkedNodes) {
    //     const node = checkedNodes[key];
    //     if(node.checked == 1){
    //       checkedOrgs.push(node);
    //     }
    //   }
    //   return checkedOrgs;
    // }
  },
  watch:{

  },
  directives:{

  }
}
</script>
