<template>
  <div id="buildFloor">
    <span class="buildFloor">建筑楼层</span>
    <el-cascader placeholder="请选择建筑楼层" :options="options" v-model="value" filterable size="small"
                 :style="isWidth ? '' : 'width:160px;'" @change="changeCascader"></el-cascader>
  </div>
</template>
<script>
import tools from "@/utils/scan/tools"
// import { floorQuery, buildingQuery } from '@/api/scan/request'
import { buildingQuery } from '@/api/object/build'
import { floorQuery } from '@/api/object/floor'

import { mapGetters, mapActions } from "vuex"

export default {
  props: {
    isWidth: {
      type: Boolean,
      default: true
    },
    type: {
      type: String,
      default: "yes"
    }
  },
  computed: {
    ...mapGetters("layout", [
      "projectId",
      "secret",
      "userId"
    ])
  },
  data() {
    return {
      options: [],
      param: {
        ProjId: '',
        secret: ''
      },
      value: [],
      floorToMap: {}
    };
  },
  created() {
    this.param.ProjId = this.projectId
    this.param.secret = this.secret
    this.getData()
  },
  watch: {
    projectId() {
      this.options = [];
      this.param.ProjId = this.projectId
      this.param.secret = this.secret
      this.getData()
      this.changeCascader([])
    }
  },
  methods: {
    //设置默认选项
    setValue(val) {
      if (val && val.length) {
        this.value = val
      }
    },
    //获取数据
    getData() {
      let data, buildParams = {
        pageNumber: 1,
        pageSize: 1000,
        orders: "localName asc",
        projection: [
          "id",
          "localName"
        ]
      }, floorParams = {
        orders: "floorSequenceId desc",
        pageNumber: 1,
        pageSize: 1000
      }
      let promise1 = new Promise((resolve, reject) => {
        buildingQuery(buildParams, res => {
          resolve(res)
        })
      })
      let promise2 = new Promise((resolve, reject) => {
        floorQuery(floorParams, res => {
          resolve(res)
        })
      })
      Promise.all([promise1, promise2]).then(values => {
        let builData = values[0].content, floorData = values[1].content
        data = builData.map(build => {
          return {
            value: build.id,
            label: build.localName
          }
        })
        data.unshift({
          value: "noKnow",
          label: "未明确建筑"
        })
        data.forEach(build => {
          floorData.forEach(floor => {
            this.floorToMap[floor.id] = floor;
            if (build.value == floor.buildingId && floor.id && floor.localName && this.type == "yes") {
              if (build.children) {
                build.children.push({
                  value: floor.id,
                  label: floor.localName,
                  FloorSequenceID: floor.floorSequenceId
                })
              } else {
                build.children = []
                build.children.push({
                  value: 'noKnow',
                  label: "未明确楼层"
                }, {
                  value: floor.id,
                  label: floor.localName,
                  FloorSequenceID: floor.floorSequenceId
                })
              }
            }
          })
        })
        this.$emit('getFloorMap', this.floorToMap)
        this.options = data
      })
    },
    //改变item
    changeCascader(value) {
      this.$emit("change", value)
    }
  }
};
</script>
<style lang="less">
.el-cascader .el-input .el-input__inner {
  vertical-align: bottom;
}
</style>
<style lang="less" scoped>
#buildFloor {
  margin-left: 10px;
  float: left;
}

.buildFloor {
  color: #999999;
  font-size: 14px;
  margin-right: 12px;
}
</style>
