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

<script>
import { searchCourses } from "@/api/examwork-course";

export default {
  name: "CourseSelect",
  props: {
    value: String,
    examId: 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 searchCourses({
        examId: this.examId,
        courseName: query,
        pageNumber: 1,
        pageSize: 30,
      });
      this.optionList = res?.data.data.records.records;
    },
    select() {
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    },
  },
};
</script>

<style></style>
