<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"
      :label="item.name"
      :value="item.id"
    ></el-option>
  </el-select>
</template>

<script>
  import { getFactoryarea } from '@/api/factoryModel';
  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('updateVal', val);
          this.$emit(
            'selfChange',
            val,
            this.dictList.find((i) => i.id === val)
          );
        },
        get () {
          return this.value;
        }
      }
    },
    created () {
      if (this.init) {
        this.getList();
      }
    },
    methods: {
      async getList () {
        const res = await getFactoryarea({
          pageNum: -1,
          size: -1,
          type: 2
        });
        this.dictList = res.list;
      }
    }
  };
</script>
