<template>
  <div class="location-selector">
    <span
      v-if="showLabel"
      class="normal-text title"
    >{{label}}</span>
    <el-cascader
      v-if="showCascader"
      ref="locationCascader"
      :size="size"
      v-model="locationKeys"
      :props="locationCascaderProps"
      clearable
      filterable
      :placeholder="placeholder"
    />
  </div>
</template>

<script>

import * as api from './api/space.js'

import { SPACE_ASSET_TYPE } from '../globalConstants'

export default {
  name: "LocationSelector",
  props: {
    showLabel: {
      type: Boolean,
      default: true
    },
    label: {
      type: String,
      default: '位置'
    },
    locationKey: {
      type: String | Array,
      default: undefined
    },
    projectKeys: {
      type: Array,
      default: () => []
    },
    size: {
      type: String,
      default: 'medium'
    },
    maxLevel: {
      type: Number,
      default: 5
    },
    checkStrictly: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      showCascader: true,
      locationCascaderProps: {
        lazy: true,
        checkStrictly: this.checkStrictly,
        lazyLoad: this.lazyLoadLocation,
        multiple: this.multiple
      },
      refresh: false,
      locationKeys: []
    }
  },

  watch: {
    locationKeys(val) {
      let locationKey
      if (this.multiple) {
        locationKey = val.map(item => {
          return item.length > 0 ? item[item.length - 1] : undefined
        });
      } else {
        locationKey = val && val.length > 0 ? val[val.length - 1] : undefined
      }
      this.$emit('update:locationKey', locationKey)
      this.$emit('onChange', locationKey)
    }
  },
  methods: {
    reset() {
      this.locationKeys = [];
      this.showCascader = false;
      setTimeout(() => {
        this.showCascader = true;
      }, 10)
    },
    setDropDownVisible(visible) {
      this.$refs['locationCascader'].dropDownVisible = visible;
    },
    getCheckedNodes() {
      return this.$refs['locationCascader'].getCheckedNodes()
    },
    async lazyLoadLocation(node, resolve) {
      const { level, value, data } = node;
      const type = data ? data.type : undefined
      let res
      console.log(level, value, data)

      if (level > this.maxLevel) {
        return resolve()
      }

      if (level === 0) {
        let projectKeys = [];
        if (this.projectKeys.length) {
          projectKeys = this.projectKeys
        } else {
          projectKeys = [localStorage.getItem('projectKey')]
        }
        res = await api.apiFilterResidenceList({
          projectIdList: projectKeys
        })
      } else if (level === 1) {
        res = await api.apiFilterGroupsAndBuildings({
          residenceIdList: [value]
        })
      } else if (level === 2) {
        // 如果父级是group则调查building接口，如果父级是building, 则查unit接口
        if (type == SPACE_ASSET_TYPE.GROUP.value) {
          res = await api.apiFilterBuildingList({
            groupIdList: [value]
          })
        } else if (type == SPACE_ASSET_TYPE.BUILDING.value) {
          res = await api.apiFilterUnitList({
            buildingIdList: [value]
          })
        }
      } else if (level === 3) {
        // 如果父级是building, 查unit接口，如果父级是查unit, 则查floor接口
        if (type == SPACE_ASSET_TYPE.BUILDING.value) {
          res = await api.apiFilterUnitList({
            buildingIdList: [value]
          })
        } else if (type == SPACE_ASSET_TYPE.UNIT.value) {
          res = await api.apiFilterFloorList({
            unitIdList: [value]
          })
        }
      } else if (level === 4) {
        // 如果父级是查unit, 则查floor接口，如果父级是查floor, 则查house接口
        if (type == SPACE_ASSET_TYPE.UNIT.value) {
          res = await api.apiFilterFloorList({
            unitIdList: [value]
          })
        } else if (type == SPACE_ASSET_TYPE.FLOOR.value) {
          res = await api.apiFilterHouseList({
            floorIdList: [value]
          })
        }
      } else if (level === 5 && type == SPACE_ASSET_TYPE.FLOOR.value) {
        // 如果父级是查floor, 则查house接口
        res = await api.apiFilterHouseList({
          floorIdList: [value]
        })
      } else {
        return resolve()
      }
      if (res) {
        resolve(res.rows.map(item => {
          return {
            value: item.primaryKey,
            label: item.name,
            type: item.type,
            leaf: level === this.maxLevel || type == SPACE_ASSET_TYPE.FLOOR.value
          }
        }))
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.location-selector {
    display: flex;
    align-items: center;
    .title {
      white-space: nowrap; /*强制span不换行*/
      display: inline-block; /*将span当做块级元素对待*/
      margin: 0px 10px;
      font-size: 14px;
      color: #606266;
    }
  }
</style>
