<template>
  <el-select
    v-model="selected"
    class="size-select"
    placeholder="请选择"
    @change="select"
    :style="styles"
    filterable
    remote
    :remote-method="search"
    clearable
  >
    <el-option
      v-for="item in optionList"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    >
      <span>{{ item.name }}</span>
    </el-option>
  </el-select>
</template>

<script>
import { searchExams } from "@/api/examwork-exam";

export default {
  name: "ExamSelect",
  props: {
    value: String,
    styles: { type: String },
  },
  data() {
    return {
      optionList: [],
      selected: "",
    };
  },
  async created() {
    this.search();
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.selected = val;
      },
    },
  },
  methods: {
    async search(query) {
      const res = await searchExams({
        name: query,
        pageNumber: 1,
        pageSize: 30,
      });
      // console.log(res.data);
      this.optionList = res.data.data.records;
    },
    select() {
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    },
  },
};
</script>

<style></style>
