<template>

  <el-select
    v-if="options!== null && options.attrs!==null"
    ref="deSelect"
    v-model="options.value"
    :collapse-tags="showNumber"
    :clearable="!options.attrs.multiple"
    :multiple="options.attrs.multiple"
    :placeholder="options.attrs.placeholder"
    @change="changeValue"
  >
    <el-option
      v-for="item in options.attrs.datas"
      :key="item[options.attrs.key]"
      :label="item[options.attrs.label]"
      :value="item[options.attrs.value]"
    />
  </el-select>

</template>

<script>

export default {

  props: {
    element: {
      type: Object,
      default: null
    },
    inDraw: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      options: null,
      showNumber: false
    }
  },
  computed: {
    operator() {
      return this.options.attrs.multiple ? 'in' : 'eq'
    }
  },
  watch: {
    'options.attrs.multiple': function(value) {
      const sourceValue = this.options.value
      if (value) {
        !sourceValue && (this.options.value = [])
        sourceValue && !Array.isArray(sourceValue) && (this.options.value = sourceValue.split(','))
        !this.inDraw && (this.options.value = [])
      } else {
        !sourceValue && (this.options.value = null)
        sourceValue && Array.isArray(sourceValue) && (this.options.value = sourceValue[0])
        !this.inDraw && (this.options.value = null)
      }
    }
  },
  created() {
    this.options = this.element.options

    this.setCondition()
  },
  mounted() {
    this.$nextTick(() => {

    })
  },
  methods: {
    changeValue(value) {
      this.setCondition()
      this.showNumber = false
      this.$nextTick(() => {
        if (!this.$refs.deSelect.$refs.tags || !this.options.attrs.multiple) {
          return
        }
        const kids = this.$refs.deSelect.$refs.tags.children[0].children
        let contentWidth = 0
        kids.forEach(kid => {
          contentWidth += kid.offsetWidth
        })
        this.showNumber = contentWidth > (this.$refs.deSelect.$refs.tags.clientWidth * 0.9)
      })
    },

    setCondition() {
      const param = {
        component: this.element,
        value: Array.isArray(this.options.value) ? this.options.value : [this.options.value],
        operator: this.operator
      }
      this.inDraw && this.$store.commit('addViewFilter', param)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
