<!-- 选择框 -->
<template>
  <el-select
    v-model="currentValues"
    :size="size"
    :disabled="disabled"
    :placeholder="placeholder"
    :filterable="filterable"
    :clearable="clearable"
    :multiple="multiple"
    :multiple-limit="multipleLimit"
    @change="change"
  >
    <el-option v-for="item in currentOptions" :key="item.value" :label="item.label" :value="item.value">
      <div class="nowrap" style="max-width: 300px">
        <span>{{ item.label }}</span>
        <el-tooltip v-if="item.description" effect="light" :content="item.description" placement="right">
          <span style="font-size: 12px; color: #a8a8a8; margin-left: 3px">({{ item.description }})</span>
        </el-tooltip>
      </div>
    </el-option>
  </el-select>
</template>
<script>
export default {
  name: 'CsFieldSelect',
  props: {
    value: {
      default: null
    },
    size: {
      type: String,
      default: 'mini'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: null
    },
    options: {
      type: Array,
      default: null
    },
    filterable: {
      type: Boolean,
      default: true
    },
    clearable: {
      type: Boolean,
      default: true
    },
    defaultValue: {
      default: null
    },
    multiple: {
      type: Boolean,
      default: false
    },
    multipleLimit: {
      type: Number,
      default: 0
    },
    // 返回类型 array / string
    returnType: {
      type: String,
      default: 'string'
    }
  },
  components: {},
  data() {
    return {
      currentValue: '',
      currentValues: [],
      currentOptions: []
    }
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    value: {
      handler(val, oldVal) {
        if (this.currentValue !== val && val !== oldVal) {
          this.setData()
        }
      },
      immediate: false, // 首次绑定是否执行
      deep: true // 对象内部的属性监听，也叫深度监听
    },
    currentValue: {
      handler(val, oldVal) {
        if (this.value !== val && val !== oldVal) {
          this.$emit('input', val)
        }
      },
      immediate: false, // 首次绑定是否执行
      deep: true // 对象内部的属性监听，也叫深度监听
    },
    options: {
      handler(val) {
        this.setData()
      },
      immediate: true, // 首次绑定是否执行
      deep: true // 对象内部的属性监听，也叫深度监听
    }
  },
  //方法集合
  methods: {
    change() {
      if (this.multiple) {
        this.currentValue = this.currentValues.join(',')
        if (this.returnType && this.returnType.toLowerCase() === 'string') {
          this.$emit('input', this.currentValue)
        } else {
          this.$emit('input', this.currentValues)
        }
      } else {
        this.currentValue = this.currentValues
        this.$emit('input', this.currentValue)
      }
      this.$forceUpdate()
    },

    setData() {
      let currentOptions = []
      let currentValues = []

      let value = this.value
      if (this.multiple) {
        if ((!value || value.length > 0) && this.defaultValue) {
          value = this.defaultValue
        }
      } else {
        if (!value && this.defaultValue) {
          value = this.defaultValue
        }
      }
      if (value == null) {
        value = ''
      }

      this.options.forEach(option => {
        let item = {}
        Object.keys(option).forEach(key => {
          item[key.toLowerCase()] = option[key]
        })
        if (this.multiple) {
          if (value.indexOf(item.value) > -1) {
            currentValues.push(item.value)
          }
        } else {
          if (item.value == value) {
            currentValues = [item.value]
          }
        }
        currentOptions.push(item)
      })
      this.currentOptions = currentOptions

      let currentValue = ''
      if (this.multiple) {
        currentValue = currentValues.join(',')
      } else {
        if (currentValues.length > 0) {
          currentValue = currentValues[0]
          currentValues = currentValue
        } else {
          currentValues = ''
        }
      }
      this.currentValues = currentValues
      this.currentValue = currentValue
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang="scss" scoped></style>
