<template>
  <el-select
    v-model="selected"
    class="paper-number-select"
    :placeholder="placeholder"
    filterable
    :clearable="clearable"
    :disabled="disabled"
    @change="select"
  >
    <el-option v-for="(item, index) in optionList" :key="index" :value="item">
      <span>{{ item }}</span>
    </el-option>
  </el-select>
</template>

<script>
import { conditionListPaperNumber } from "../../modules/base/api";
import { objFilterNull } from "../../plugins/utils";

export default {
  name: "paper-number-select",
  props: {
    disabled: { type: Boolean, default: false },
    placeholder: { type: String, default: "请选择" },
    value: { type: [Number, String], default: "" },
    clearable: { type: Boolean, default: true },
    semesterId: { type: String, default: "" },
    examId: { type: String, default: "" },
    courseId: { type: String, default: "" },
    printPlanId: { type: [String, Array], default: "" },
    openCollegeId: { type: String, default: "" },
  },
  data() {
    return {
      optionList: [],
      selected: "",
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.selected = val;
      },
    },
    semesterId(val, oldval) {
      if (val !== oldval) {
        this.search();
        this.$emit("input", "");
        this.$emit("change", {});
      }
    },
    examId(val, oldval) {
      if (val !== oldval) {
        this.search();
        this.$emit("input", "");
        this.$emit("change", {});
      }
    },
    printPlanId(val, oldval) {
      if (val !== oldval) {
        this.search();
        this.$emit("input", "");
        this.$emit("change", {});
      }
    },
    courseId(val, oldval) {
      if (val !== oldval) {
        this.search();
        this.$emit("input", "");
        this.$emit("change", {});
      }
    },
    openCollegeId(val, oldval) {
      if (val !== oldval) {
        this.search();
        this.$emit("input", "");
        this.$emit("change", {});
      }
    },
  },
  async created() {
    this.search();
  },
  methods: {
    async search() {
      this.optionList = [];
      if (!this.examId) return;

      let data = {
        semesterId: this.semesterId,
        examId: this.examId,
        printPlanId: this.printPlanId,
        courseId: this.courseId,
        openCollegeId: this.openCollegeId,
      };
      const res = await conditionListPaperNumber(objFilterNull(data));
      this.optionList = res;
    },
    select() {
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    },
  },
};
</script>
