<template>
  <el-select
    v-model="selected"
    :placeholder="placeholder"
    filterable
    :clearable="clearable"
    :disabled="disabled"
    @change="selectChange"
  >
    <el-option
      v-for="item in optionList"
      :key="item.code"
      :value="item.name"
      :label="item.name"
    >
    </el-option>
  </el-select>
</template>

<script>
export default {
  name: "grade-select",
  props: {
    disabled: { type: Boolean, default: false },
    placeholder: { type: String, default: "请选择年级" },
    value: { type: [Number, String], default: "" },
    clearable: { type: Boolean, default: true },
  },
  data() {
    return {
      optionList: [],
      selected: "",
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.selected = val;
      },
    },
  },
  created() {
    this.getOptions();
  },
  methods: {
    getOptions() {
      const options = [];
      for (let i = 20; i < 30; i++) {
        options.push({
          name: `${i}级`,
          code: i,
        });
      }
      this.optionList = options;
    },
    selectChange() {
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    },
  },
};
</script>
