
/**
* Description: 空间【权限】级联选择器
*/

<template>
  <div class="filter-space-auth-tree">
    <span
      v-if="showLabel"
      class="normal-text title"
    >{{label}}</span>
    <el-cascader
      :class="['space-auth-tree-cascader']"
      :popper-class="popperClass"
      v-if="showCascader"
      ref="locationCascader"
      :size="size"
      style="width: 250px"
      v-model="locationKeys"
      :props="locationCascaderProps"
      :collapse-tags="collapseTags"
      clearable
      filterable
      :placeholder="placeholder"
    />
  </div>
</template>

<script>

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

import { SPACE_ASSET_TYPE } from '../globalConstants'

export default {
  name: "FilterSpaceAuthTree",
  props: {
    showLabel: {
      type: Boolean,
      default: true
    },
    label: {
      type: String,
      default: '位置'
    },
    locationKey: {
      type: String | Array,
      default: undefined
    },
    size: {
      type: String,
      default: 'medium'
    },
    maxLevel: {
      type: Number,
      default: 6
    },
    checkStrictly: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: undefined
    },
    popperClass: {
      type: String,
      default: ''
    },
    collapseTags: {
      type: Boolean,
      default: false
    },
  },
  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

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

      if (level === 0) {
        res = await api.apiFilterProjectList({ flag: true })
      } else if (level === 1) {
        res = await api.apiFilterResidenceList({
          projectIdList: [value]
        })
      } else if (level === 2) {
        res = await api.apiFilterGroupsAndBuildings({
          residenceIdList: [value]
        })
      } else if (level === 3) {
        // 如果父级是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 === 4) {
        // 如果父级是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 === 5) {
        // 如果父级是查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 === 6 && 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 || item.projectName,
            type: item.type,
            leaf: level === this.maxLevel || type == SPACE_ASSET_TYPE.FLOOR.value
          }
        }))
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.filter-space-auth-tree {
    display: flex;
    align-items: center;

    .title {
      white-space: nowrap; /*强制span不换行*/
      display: inline-block; /*将span当做块级元素对待*/
      margin: 0px 10px;
      font-size: 14px;
      color: #606266;
    }

    .space-auth-tree-cascader{
      flex: 1;
    }
  }
</style>
<style lang="scss">

.filterSpaceAuthTree1 .el-cascader-menu:nth-child(-n+1) .el-checkbox {
  display: none;
}
.filterSpaceAuthTree2 .el-cascader-menu:nth-child(-n+2) .el-checkbox {
  display: none;
}
.filterSpaceAuthTree3 .el-cascader-menu:nth-child(-n+3) .el-checkbox {
  display: none;
}
.filterSpaceAuthTree4 .el-cascader-menu:nth-child(-n+4) .el-checkbox {
  display: none;
}
.filterSpaceAuthTree5 .el-cascader-menu:nth-child(-n+5) .el-checkbox {
  display: none;
}
.filterSpaceAuthTree6 .el-cascader-menu:nth-child(-n+6) .el-checkbox {
  display: none;
}
.filterSpaceAuthTree7 .el-cascader-menu:nth-child(-n+7) .el-checkbox {
  display: none;
}
.filterSpaceAuthTree8 .el-cascader-menu:nth-child(-n+8) .el-checkbox {
  display: none;
}
</style>
