ZoneSelect.vue 5.2 KB
<template>
  <div class="component-zone-wrapper">
    <span class="condition-label" v-show="showLabel">{{ $t('asisTab.storeT') }}</span>
    <el-select
      v-if="isMulti"
      v-model="multiZoneVal"
      :filterable="filterable"
      :collapse-tags="isCollapse"
      multiple
      :placeholder="$t('pholder.shopSelect')"
      @change="multiZoneChangeHandle">
      <div
        :class="['sel-all-box', { 'selected': isZoneSelAll }]"
        @click="selAllHandle"
        >
        <span class="custom-checkbox__input">
          <span class="custom-checkbox__inner"></span>
        </span>
        <span class="pl-5">{{ $t('allPages.all') }}</span>
      </div>
      <el-option v-for="item in zoneList"
        :key="item.id"
        :label="item.name"
        :value="item.id">
        <span class="custom-checkbox__input">
          <span class="custom-checkbox__inner"></span>
        </span>
        <span class="pl-5">{{ item.name }}</span>
      </el-option>
    </el-select>
    <el-select v-else v-model="zoneVal" :filterable="filterable" :placeholder="$t('pholder.shopSelect')">
      <el-option v-for="item in zoneList"
        :key="item.id"
        :label="item.name"
        :value="item.id">
      </el-option>
    </el-select>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'ZoneSelect',
  props: {
    isMulti: {
      type: Boolean,
      default: false
    },
    filterable: {
      type: Boolean,
      default: true
    },
    isCollapse: {
      type: Boolean,
      default: true
    },
    isNeedSelectedAll: {
      type: Boolean,
      default: true
    },
    showLabel: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    ...mapGetters(['zoneList']),
    ...mapState({
      accountVal: state => state.app.accountVal,
      cacheConditions: state => state.app.cacheConditions,
      mallVal: state => state.app.mallVal,
      multiMallVal: state => state.app.multiMallVal,
      floorVal: state => state.app.floorVal
    }),
    zoneVal: {
      get() {
        return this.$store.state.app.zoneVal
      },
      set(val) {
        this.$store.commit('app/SET_FLOOR_VAL', val)
      }
    },
    multiZoneVal: {
      get() {
        let storeVal = this.$store.state.app.multiZoneVal
        this.isZoneSelAll = this.zoneList.length && storeVal.length === this.zoneList.length
        return storeVal
      },
      set(val) {
        this.$store.commit('app/SET_MULTI_FLOOR_VAL', val)
      }
    },
  },
  data() {
    return {
      isZoneSelAll: false
    }
  },
  watch: {
    mallVal(nVal, oVal) {
      if (nVal) {
        this.storeZone('update')
      }
    },
    floorVal(nVal, oVal) {
      if (nVal) {
        this.storeZone('update')
      }
    },
    multiZoneVal(nVal, oVal) {
      this.isZoneSelAll = this.zoneList.length && nVal.length === this.zoneList.length
    }
  },
  async mounted() {
    // if (!this.floorVal || !this.multiZoneVal.length) {
    this.storeZone('update')
    // }
  },
  methods: {
    async storeZone(status) {
      if (!this.zoneList.length || status === 'update') {
        await this.$store.dispatch('app/getZones', {
          accountId: this.accountVal.id,
          mallId: this.cacheConditions && this.cacheConditions['singleMall'] || this.mallVal,
          floorId: this.cacheConditions && this.cacheConditions['singleFloor'] || this.floorVal || null,
          status: 1,
          sortName: 'floor.name, zone.name'
        })
      }

      if (this.cacheConditions) {
        // 单选
        if(this.cacheConditions['singleZone']) {
          this.$store.commit('app/SET_ZONE_VAL', this.cacheConditions['singleZone'])
        }
        // 多选
        if (this.cacheConditions['multiZone']) {
          this.$store.commit('app/SET_MULTI_ZONE_VAL', this.cacheConditions['multiZone'])
        }
      } else {
        this.$store.commit('app/SET_CACHE_VAL', { key: 'singleZone', val: this.zoneVal })
        this.$store.commit('app/SET_CACHE_VAL', { key: 'multiZone', val: this.multiZoneVal })
      }
    },
    multiZoneChangeHandle() {
      this.isZoneSelAll = this.checkedAllHandle(this.multiZoneVal, this.zoneList);
    },
    checkedAllHandle(valArr, data) {
      return valArr.length === data.length
    },
    selAllHandle() {
      if(this.isZoneSelAll) {
        this.isZoneSelAll = false
        if(this.multiZoneVal.length == this.zoneList.length) {
          this.multiZoneVal = []
        }
      } else {
        this.isZoneSelAll = true
        if(this.multiZoneVal.length == this.zoneList.length) {
          this.multiZoneVal = []
        } else {
          this.multiZoneVal = []
          this.zoneList.forEach(item => {
            this.multiZoneVal.push(item.id)
          })
        }
      }
    },
    cacheOptions() {
      // if (this.isMulti) {
      //   this.$store.commit('app/SET_CACHE_VAL', { key: 'multiZone', val: this.multiZoneVal })
      // } else {
      //   this.zoneVal && this.$store.commit('app/SET_CACHE_VAL', { key: 'singleZone', val: this.zoneVal })
      // }
    },
    // zoneChangeHandle() {},
  }
}
</script>

<style scoped>
.component-zone-wrapper {
  display: inline-block;
}
.condition-label {
  padding-right: 10px;
  color: #666;
  font-size: 13px;
}
.pl-5 {
  padding-left: 5px;
}
</style>