<template>
  <el-cascader placeholder="全部" :options="LocationList" v-model="value" filterable size="small" @change="changeCascader"
               :change-on-select="!isFloor"
               :props="props" clearable class="location"></el-cascader>
</template>
<script>
import {
  // dynamicLocationList,
  // buildingQuery, //数据中心查询建筑
  // floorQuery, //数据中型查询楼层
  queryDictionaryHead, //数据中心查询分区类型
  zoneQuery, //数据中心查询所有空间
} from "@/api/scan/request";
import { floorQuery } from '@/api/object/floor'

import { buildingQuery } from '@/api/object/build'

import { mapGetters, mapActions } from "vuex";

export default {
  data() {
    const that = this;
    return {
      LocationList: [],
      value: [],
      timer: null,
      props: {
        lazy: this.isFloor ? false : true,
        lazyLoad(node, resolve) {
          console.log(node)
          if (node.level == 1) {
            if (node.value == 'unknowBuild') {
              resolve()
            } else {
              let pa = {
                filters: `buildingId='${node.value}'`
              }
              floorQuery(pa, res => {
                const nodes = res.content.map(t => {
                  t.value = t.id;
                  t.label = t.localName;
                  t.leaf = that.isSpace;
                  return t;
                })
                nodes.unshift({ value: 'unknowFloor', label: '未明确楼层', leaf: true })
                resolve(nodes);
              })
            }
          } else if (node.level == 2) {
            if (that.isSpace) {
              resolve();
            } else {
              let pa = {
                Filters: "parentId='Space'"
              }
              queryDictionaryHead(pa, res => {
                const nodes = res.Content.map(t => {
                  if (t.Code == 'Ispace') {
                    return undefined;
                  }
                  t.value = t.Code;
                  t.label = t.Name;
                  return t;
                }).filter(item => item);
                resolve(nodes);
              })
            }
          } else if (node.level == 3) {
            let pa = {
              PageSize: 1000,
              ZoneType: node.path[2],
              Filters: `BuildingId='${node.path[0]}';FloorId='${node.path[1]}'`
            };
            zoneQuery(pa, res => {
              const nodes = res.Content.map(t => {
                t.value = t.RoomID;
                t.label = t.RoomLocalName;
                t.leaf = true;
                return t;
              })
              resolve(nodes);
            })
          } else {
            resolve();
          }
        }
      }
    };
  },
  created() {
    this.init();
  },
  props: {
    Related: {},
    typeName: {},
    isFloor: {
      default: false
    },
    isSpace: {
      default: false
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  methods: {
    init() {
      // let param = { Related: this.Related, TypeName: this.typeName };
      // dynamicLocationList(param, res => {
      //   this.LocationList = res.Content[0].List;
      //   //添加未明确对象
      //   this.LocationList = this.delNullItems(this.LocationList)
      //   this.LocationList.map(item => {
      //     if (this.isFloor) {
      //       delete item.List;
      //     }
      //     if (item.List) {
      //       item.List.map(child => {
      //         if (child.List) {
      //           child.List.unshift({ Name: '未明确业务空间' })
      //         }
      //       })
      //       item.List.unshift({ Name: '未明确楼层' })
      //     }
      //   })
      //   this.LocationList.unshift({ Name: '未明确建筑' })
      // });
      let pa = {}
      buildingQuery(pa, res => {
        this.LocationList = res.content.map(item => {
          item.value = item.id;
          item.label = item.localName;
          return item;
        });
        this.LocationList.unshift({ value: 'unknowBuild', label: '未明确建筑', leaf: true })
      })
    },
    delNullItems(arr) {
      let tempArr = JSON.parse(JSON.stringify(arr))
      let options = tempArr.map(item => {
        if (item.Name == 'null') return undefined
        if (item.List && item.List.length) {
          item.List = this.delNullItems(item.List)
        }
        return item
      }).filter(d => d)
      return options
    },
    changeCascader(val) {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        let tempVal = JSON.parse(JSON.stringify(val));
        if (tempVal.length && tempVal[tempVal.length - 1].indexOf('未明确') > -1) {
          tempVal.splice(tempVal.length - 1, 1, '')
        }
        this.$emit("change", tempVal);
      }, 800);
    }
  },
  watch: {
    projectId(n, o) {
      this.init();
    }
  }
};
</script>
<style lang="less" scoped>
.location {
  width: 100%;

  /deep/ .el-input__inner {
    vertical-align: baseline;
  }
}
</style>

