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

<script>
import { roomQuery } from "../../modules/print/api";

export default {
  name: "room-select",
  props: {
    disabled: { type: Boolean, default: false },
    placeholder: { type: String, default: "请选择" },
    value: { type: [Number, String], default: "" },
    clearable: { type: Boolean, default: true },
    printPlanId: { type: [String, Array], default: "" },
    examId: { type: String, default: "" },
  },
  data() {
    return {
      optionList: [],
      selected: "",
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.selected = val;
      },
    },
    printPlanId(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", {});
      }
    },
  },
  async created() {
    this.search();
  },
  methods: {
    async search(query) {
      const res = await roomQuery({
        param: query,
        printPlanId: this.printPlanId,
        examId: this.examId,
      });
      this.optionList = res;
    },
    select() {
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    },
  },
};
</script>
