<template>
  <el-select
    v-model="selectVal"
    filterable
    style="width: 100%"
    v-bind="$attrs"
    v-on="$listeners"
    clearable
  >
    <el-option
      v-for="item in dictList"
      :key="item.id"
      :value="item.id"
      :label="item.name"
    ></el-option>
  </el-select>
</template>

<script>
  import { getGroupPage } from '@/api/material/list';
  export default {
    model: {
      prop: 'value',
      event: 'updateVal'
    },
    props: {
      value: {
        type: [String, Number, Array],
        default: ''
      },
      init: {
        type: Boolean,
        default: true
      }
    },
    data () {
      return {
        dictList: []
      };
    },
    computed: {
      selectVal: {
        set (val) {
          this.$emit(
            'selfChange',
            val,
            this.dictList.find((i) => i.id === val)
          );
          this.$emit('updateVal', val);
        },
        get () {
          return this.value;
        }
      }
    },
    created () {
      if (this.init) {
        this.getList();
      }
    },
    methods: {
      async getList (params) {
        let data = { pageNum: 1, size: -1 };
        // 如果传了参数就是获取巡点检人员数据
        if (params) {
          data = Object.assign(data, params);
        }
        const res = await getGroupPage(data);
        this.dictList = res.list;
      }
    }
  };
</script>
