<template>
  <div class="pbbasic-left">
    <el-tree
      :data="data"
      :props="defaultProps"
      @node-click="handleNodeClick">
    </el-tree>
  </div>
</template>
<script>
export default {
  data() {
    return {
      data: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 2",
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
              children: [
                {
                  label: "三级 3-1-1",
                },
              ],
            },
            {
              label: "二级 3-2",
              children: [
                {
                  label: "三级 3-2-1",
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
      this.$emit('getPigs', 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 = []
        data.all.forEach(pasture => {
          let item = {
            id: pasture.id,
            label: pasture.name,
            children: [],
            order: 0
          }
          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.farmId === pasture.id) {
              item.children.push(pitem)
            }
          })
          this.data.push(item)
        })
        console.log(this.data);
      })
    }
  },
};
</script>
<style scoped>
/deep/.el-tree-node__expand-icon {
  color: #000 !important;
} 
</style>