<!-- 选择框 -->
<template>
  <el-select
    v-model="currentValue"
    :size="size"
    :disabled="disabled"
    :placeholder="placeholder"
    :filterable="filterable"
    :clearable="clearable"
  >
    <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,
    },
  },
  components: {},
  data() {
    return {
      currentValue: null,
      currentOptions: [],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    value: {
      handler(val) {
        this.setData();
      },
      immediate: false, // 首次绑定是否执行
      deep: true, // 对象内部的属性监听，也叫深度监听
    },
    currentValue(val) {
      this.$emit("input", val);
    },
    options: {
      handler(val) {
        this.setData();
      },
      immediate: true, // 首次绑定是否执行
      deep: true, // 对象内部的属性监听，也叫深度监听
    },
  },
  //方法集合
  methods: {
    setData() {
      let currentOptions = [];
      let currentValue = null;

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