<template>
  <el-select v-model="newValue" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item[valueKay]"
      :label="item.label"
      :value="item[valueKay]"
    >
    </el-option>
  </el-select>
</template>

<script>
import { getList } from '@/api/dict';

export default {
  name: 'DataSelect',
  props: {
    value: {
      type: String,
      default: ''
    },
    params: {
      type: String,
      default: ''
    },
    valueKay: {
      type: String,
      default: 'value'
    }
  },

  data() {
    return {
      newValue: this.value,
      options: []
    };
  },

  watch: {
    value: {
      handler(val) {
        this.newValue = val;
      },
      immediate: true
    },
    newValue: {
      handler(val) {
        this.$emit('input', val);
      },
      immediate: true
    }
  },

  created() {
    this.loadData();
  },

  methods: {
    async loadData() {
      const { data } = await getList({
        dictCode: this.params
      });
      this.options = data.map(x => ({
        value: x.value,
        label: x.name
      }));
    }
  }
};
</script>

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