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

  },
  props:{
    orgId:{
      type: String,
      default(){
        return '';
      }
    },
    orgName:{
      type: String,
      default(){
        return '';
      }
    },
  },
  data(){
    return{
      orgTree: [],
      orgTreeProps: {
        children: 'children',
        label: 'orgName'
      },
    }
  },
  computed:{

  },
  created:function(){
    this.$emit('SetTitle', '组织单选');
    this.$emit('SetPageSize', 350);
    this.$emit('SetPageActions', [
      {
        text: "确定",
        theme: "primary",
        handle: () => {
          this.onOk();
        }
      },
      {
        text: "清空",
        theme: "primary",
        handle: () => {
          this.clear();
        }
      },
      {
        text: "关闭",
        theme: "primary",
        handle: () => {
          this.$emit('Close');
        }
      }
    ])
  },
  beforeMount:function(){

  },
  mounted:function(){

  },
  beforeDestroy:function(){

  },
  destroyed:function(){

  },
  methods:{
    // 加载当前组织子节点
    loadData(currentOrg, callback) {
      $store.dispatch("QueryOrg", { "orgId": currentOrg.data.orgId }).then(response => {
        if (response.statusCode == "0") {
          var childrenOrg = response.dataResult.depList;
          for (const key in childrenOrg) {
            let org = childrenOrg[key];
            if(org.checked == 0){
              org.disabled = true;
            }
          }
          callback(childrenOrg);
        } else {
          this.$Message.error(response.msg);
        }
      });
    },
    onTreeChange(currentOrg) {
      if(this.$refs["orgTree"].getCheckedKeys() && this.$refs["orgTree"].getCheckedKeys().length == 2){
        this.$refs["orgTree"].setCheckedKeys([currentOrg.orgId]);
      }
    },
    // 确定
    onOk(){
      var orgs = this.$refs["orgTree"].getCheckedNodes();
      this.$emit('Close', orgs[0]);
    },
    // 清空
    clear(){
      this.$emit('Close', []);
    }
  },
  watch:{

  },
  directives:{

  }
}
</script>
