<template>

  <el-select
    v-model="currentValue"
    :multiple="multi"
    :multiple-limit="1"
    :remote-method="fetchData"
    filterable
    remote
    reserve-keyword
    clearable
    automatic-dropdown
    placeholder="选择或搜索题库"
    class="filter-item"
    @change="handlerChange"
  >
    <el-option
      v-for="item in dataList"
      :key="item.repoId"
      :label="item.title"
      :value="item.repoId"
    />
  </el-select>

</template>

<script>

import { listRepo } from '@/api/training/elearn/repo'

export default {
  name: 'RepoSelect',
  props: {
    multi: {
      type: Boolean,
      default: false
    },
    value: String | Array,
    excludes: Array
  },
  data() {
    return {
      // 下拉选项值
      dataList: [],
      currentValue: []
    }
  },

  watch: {
    // 检测查询变化
    value: {
      handler() {
        this.currentValue = this.value
      }
    }
  },
  created() {
    this.currentValue = this.value
    this.fetchData()
  },
  methods: {
    fetchData(q) {
      listRepo({ pageNum: 1, pageSize: 1000,  title: q, excludes: this.excludes }).then(res => {
        this.dataList = res.rows;
      })
    },
    handlerChange(e) {
      const obj = this.dataList.find((item) => {
        return item.repoId == e
      })

      this.$emit('change', obj)
      this.$emit('input', e)
    }
  }
}
</script>
