<template>
  <Tree
    ref="resourceTree"
    class="resourceTree"
    :data="resourceTree"
    v-bind="$attrs"
    :show-checkbox="showCheckbox"
    check-strictly
    @on-select-change="select"
    @on-check-change="select"
  ></Tree>
</template>
<script>
export default {
  props: {
    treeData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    showCheckbox: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      resourceTree: []
    };
  },
  watch: {
    treeData(val) {
      this.setTree(val);
    }
  },
  created() {
    this.setTree(this.treeData);
  },
  methods: {
    setTree(data) {
      this.resourceTree = JSON.parse(JSON.stringify(data));
    },
    select(list, data) {
      if (list.length) {
        if (this.showCheckbox) {
          this.$emit('select', list);
        } else {
          this.$emit('select', data);
        }
        
      } else {
        this.$emit('select', null);
      }
    },
    // 获取选中的树节点
    getSelectedNodes() {
      return this.$refs.resourceTree.getSelectedNodes();
    }
  }
};

</script>
<style lang="less">
.resourceTree {
  .ivu-tree-arrow-disabled + .ivu-tree-title {
    color: #c5c8ce;
    cursor: not-allowed;
    &:hover {
      background-color: #fff;
    }
  }
}
</style>
