<template>
  <div class="pbbasic-left">
    <el-tree
      highlight-current
      :data="data"
      :props="defaultProps"
      ref="tree"
      node-key="id"
      @node-click="handleNodeClick">
    </el-tree>
  </div>
</template>
<script>
export default {
  data() {
    return {
      data: [],
      defaultProps: {
        children: "children",
        label: "label",
      }
    };
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 清除el-tree的选中
    // clearTree () {
    //   console.log(this.$refs['tree'].getCurrentKey());
    //   console.log('在清空了在清空了');
    //   // this.$nextTick(function() {
    //     this.$refs['tree'].setCurrentKey(null)
    //   // })
    //   console.log(this.$refs['tree'].getCurrentKey())
    //   console.log(this.$refs['tree'].getCurrentNode())
    // },
    handleNodeClick(data) {
      // this.$refs['tree'].setCurrentKey(data.id)
      // this.$refs['tree'].setCurrentKey(null)
      this.$emit('getUsers', data)
    },
    // 获取初始数据
    getDataList () {
      // 获取所有牧场
      this.$http({
        url: this.$http.adornUrl("/management/farm/findAll"),
        method: "post"
      }).then(async ({ data }) => {
        /* 不需要猪舍和单元 */
        // 获取所有猪舍
        // let pighouseResult = await this.$http({
        //   url: this.$http.adornUrl("/management/pigsty/findAll"),
        //   method: "post",
        // })
        // let pighouseList = []
        // pighouseResult.data.all && (pighouseList = pighouseResult.data.all)
        // // 获取所有单元
        // let unitResult = await this.$http({
        //   url: this.$http.adornUrl("/management/unit/findAll"),
        //   method: "post",
        // })
        // let unitList = []
        // unitResult.data.all && (unitList = unitResult.data.all)
        // 组合牧场、猪舍、单元
        this.data = [
          {
            id: 0,
            order: 0, 
            label: '青莲集团',
            children: []
          }
        ]
        data.all.forEach(pasture => {
          let item = {
            id: pasture.id,
            label: pasture.name,
            children: [],
            order: 1
          }
          // pighouseList.forEach(pighouse => {
          //   let pitem = {
          //     id: pighouse.id,
          //     label: pighouse.number,
          //     children: [],
          //     order: 1
          //   }
          //   unitList.forEach(unit => {
          //     let uitem = {
          //       id: unit.id,
          //       label: unit.number,
          //       order: 2
          //     }
          //     if (unit.pigstyId === pighouse.id) {
          //       pitem.children.push(uitem)
          //     }
          //   })
          //   if (pighouse.pastureId === pasture.id) {
          //     item.children.push(pitem)
          //   }
          // })
          this.data[0].children.push(item)
        })
      })
    }
  },
};
</script>
<style scoped>
/* /deep/.el-tree-node__expand-icon {
  color: #000 !important;
}  */
</style>