/**
* Description: 空间级联选择器
*/
<template>
  <el-cascader
    :key="cascaderKeys"
    class="yj-space-cascader"
    ref="locationCascader"
    :size="size"
    v-model="locationKeys"
    :props="locationCascaderProps"
    clearable
    filterable
    :placeholder="placeholder"
    :collapse-tags="collapse"
  />
</template>

<script>

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

export default {
  name: "FilterLocationCascader",
  props: {
   spaceType: {
     type: Object,
     default: function () {
        return {}
      }
    },
   collapse: {
     type: Boolean,
     default: false
    },
    locationKey: {
      type: String | Array,
      default: undefined
    },
    projectKey: {
      type: String | Array,
      default: undefined
    },
    size: {
      type: String,
      default: 'medium'
    },
    checkStrictly: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: undefined
    },
    module: {
      type: Array,
      default: function () {
        return ['project', 'residence', 'region', 'building', 'unit', 'floor', 'house']
      }
    },
  },
  data() {
    return {
      cascaderKeys: 'cascaderKeys',
      locationCascaderProps: {
        that: this,
        lazy: true,
        checkStrictly: this.checkStrictly,
        lazyLoad: this.lazyLoadLocation,
        multiple: this.multiple
      },
      locationKeys: [],

      moduleObjectList: [
        {
          name: '项目',
          type: 'project',
          url: 'apiFilterProjectList',
          show: false
        },
        {
          name: '小区',
          type: 'residence',
          url: 'queryAuthResidenceList',
          show: false
        },
        {
          name: '区域',
          type: 'region',
          url: 'queryAuthRegionList',
          show: false
        },
        {
          name: '楼栋',
          type: 'building',
          url: 'queryAuthBuildingList',
          show: false
        },
        {
          name: '单元',
          type: 'unit',
          url: 'queryAuthUnitList',
          show: false
        },
        {
          name: '楼层',
          type: 'floor',
          url: 'queryAuthFloorList',
          show: false
        },
        {
          name: '房屋',
          type: 'house',
          url: 'queryAuthHouseList',
          show: false
        }
      ],

      contant: {
        projectList: [],
        residenceList: [],
        regionList: [],
        buildingList: [],
        unitList: [],
        floorList: [],
        houseList: [],
      },
    }
  },

  computed: {
    nowModuleList() {
      let list = [];
      this.moduleObjectList.forEach(element => {
        if (this.module.includes(element.type)) {
          list.push(element)
        }
      });
      return list
    },
  },

  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.$nextTick(() => {
	      this.$emit('update:locationKey', locationKey)
	      this.$emit('onChange', locationKey)
      })
    },

    projectKey(val) {
      if (!this.nowModuleList.includes('project')) {
        this.reset()
      }
    }
  },

  created() {
    this.reset()
  },

  methods: {
    reset() {
      this.locationKeys = [];
      let time = new Date().getTime();
      this.cascaderKeys = 'cascader_'+time
    },

    setDropDownVisible(visible) {
      this.$refs['locationCascader'].dropDownVisible = visible;
    },

    getCheckedNodes() {
      return this.$refs['locationCascader'].getCheckedNodes()
    },

    async lazyLoadLocation(node, resolve) {
      let { level, value } = node;
      let param = {};
      let nowModule = this.nowModuleList[level];
      let list = [];

      if (nowModule) {
        if (nowModule.type === 'project') {
          param = { flag: true }
        } else if (!this.nowModuleList.includes('project') && level === 0) {
          if (typeof this.projectKey === 'undefined') {
            return
          } else if (typeof this.projectKey === 'string') {
            param = { 'projectId': this.projectKey }
          } else {
            param = { 'projectIdList': this.projectKey }
          }
        } else {
          let netModule = this.nowModuleList[level - 1];
          param[`${netModule.type}Id`] = value
        }

        if(this.spaceType){
          param = {...this.spaceType,...param}
        }

        let res = await api[nowModule.url](param);
        if (res.success) {
          res.rows.forEach((item) => {
            if (nowModule.type === 'project') {
              list.push({
                value: item.primaryKey,
                label: item.projectName,
                leaf: level >= this.nowModuleList.length - 1,
                type: 'project',
                typeCode: 'project'
              })
            } else {
              list.push({
                value: item.primaryKey,
                label: item.name,
                leaf: level >= this.nowModuleList.length - 1,
                type: item.type,
                typeCode: nowModule.type
              })
            }
          });
        }
      }
      resolve(list);
    }
  }
}
</script>

<style lang='scss' scoped>
</style>
