<template>
    <el-tree
    :props="treeArr"
    :load="getNodes"
    @node-click="floorClick"
    :highlight-current="true"
    lazy>
    </el-tree>
</template>

<script>
import {
  getBuildSelect, //根据项目ID获得建筑列表
  getFloor //根据建筑id获取楼层id
} from "@/api/scan/request";
export default {
  props: {
    param: {
      type: Object
    }
  },
  data() {
    return {
      treeArr: {
        label: "name", //label的名字
        children: "zones", //如果有下一级children和isLeaf存在
        isLeaf: "isChild"
      }
    };
  },
  methods: {
    //点击节点懒加载
    getNodes(node, resolve) {
      let param = {
        ProjId: this.param.ProjId,
        UserId: this.param.UserId
      };
      console.log(param)
      console.log(node);
      //第一级初始化数据，获取建筑信息
      if (node.level === 0) {
        getBuildSelect(param).then(res => {
          let data = res.data.BuildList,
            arr;
            console.log(data)
          if (data && data.length > 0) {
            arr = data.map(item => {
              return {
                name: item.BuildLocalName,
                code: item.BuildId
              };
            });
          }
          console.log(arr)
          return resolve(arr);
        });
      } else {
        //   第二级获取楼层信息
        param.BuildId = node.data.code;
        getFloor(param).then(res => {
          let resData = res.data.FloorList,
            buildData;
          if (resData && resData.length > 0) {
            buildData = resData.map(item => {
              return {
                name: item.FloorLocalName + (item.FloorMap ? '' : '(需初始化数据)'),
                code: item.FloorId,
                map: item.FloorMap,
                isChild: true //没有下一级
              };
            });
            return resolve(buildData);
          }
        });
      }
    },

    //子节点被点击
    floorClick(data) {
      if (data.isChild) {
          //派发事件
          this.$emit("change",data)
      } else {
        return false;
      }
    }
  }
};
</script>

<style lang="less" scoped>
</style>
