<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 { getteamtime } from '@/api/workforceManagement/classes';

  export default {
    model: {
      prop: 'value',
      event: 'updateVal'
    },
    props: {
      value: {
        type: [String, Number, Array],
        default: ''
      },
      init: {
        type: Boolean,
        default: true
      }
    },
    data () {
      return {
        selectName: 'classes'
      };
    },
    computed: {
      dictList () {
        return this.$store.state.selectCache[this.selectName] || [];
      },
      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 () {
        if (this.dictList.length) return;
        const res = await getteamtime({
          pageNum: 1,
          size: -1
        });

        this.$store.commit('selectCache/ADD_SELECT', {
          name: this.selectName,
          dict: res.list
        });
      }
    }
  };
</script>
