<template>
  <div>
    <el-row>
      <!-- style="font-weight: 700;color: #606266;font-size: 14px;" -->
      <el-col
        :span="6"
        :offset="6"
        style="font-weight: 700; color: #606266; font-size: 14px"
      >
        学年
        <el-select v-model="year" size="small">
          <el-option
            v-for="item in selectOptions1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            @click.native="selectYear"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col
        :span="6"
        style="font-weight: 700; color: #606266; font-size: 14px"
      >
        学期
        <el-select v-model="semester" size="small">
          <el-option
            v-for="item in selectOptions2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            @click.native="selectSemester"
          >
          </el-option>
        </el-select>
      </el-col>
    </el-row>
    <br /><br />
  </div>
</template>

<script>
export default {
  name: "timeSelect",
  props: {
    flag: false, //true: 实验室预约
  },
  data() {
    return {
      // 获取当前年份与学期
      year: "2021",
      semester: 0,

      selectOptions1: [
        // {
        //   value: "2021",
        //   label: "2021-2022年",
        // },
      ],
      selectOptions2: [
        {
          value: 0,
          label: "第一学期",
        },
        {
          value: 1,
          label: "第二学期",
        },
      ],
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      /*
      1.从21年开始，遍历添加当前年份【之前的年份】到数组
      2.最后判断到7月再添加一年，可预约下一个学年
      */
      let firstYear = this.year - 1;
      let year = String(new Date().getFullYear());
      // let year = "2025"; //模拟当前年份
      let month = new Date().getMonth() + 1;
      // let month = 1; //模拟当前月份
      // 默认回显的年份学期
      this.year = year;
      this.semester = month > 2 && month < 8 ? 1 : 0; //第二学期：3-7，其余为第一学期

      let i = 1;

      if (this.flag) {
        // 预约实验室
        let obj = {};
        obj.value = year;
        obj.label = obj.value + "-" + (Number(obj.value) + 1).toString() + "年";
        this.selectOptions1.push(obj);

        firstYear = year;
      } else {
        for (; i <= Number(year) - Number(firstYear); i++) {
          // {
          //     value: '2022',
          //     label: '2022-2023年'
          // }
          let obj = {};
          obj.value = (Number(firstYear) + i).toString();
          obj.label =
            obj.value + "-" + (Number(obj.value) + 1).toString() + "年";
          this.selectOptions1.push(obj);
        }
      }

      // 当前学年到7月后，可预约下一学年第一学期的实验室
      if (month > 6) {
        let obj = {};
        obj.value = (Number(firstYear) + i).toString(); //最后i++，可直接+i
        obj.label = obj.value + "-" + (Number(obj.value) + 1).toString() + "年";
        this.selectOptions1.push(obj);
      }

      // let obj = {};
      // obj.value = 0; //最后i++，可直接+i
      // obj.label = "第一学期";
      // this.selectOptions2.push(obj);
      // // 9-1【1】 3-7【2】
      // if (month > 0 && month < 9) {
      //   let obj = {};
      //   obj.value = 1; //最后i++，可直接+i
      //   obj.label = "第二学期";
      //   this.selectOptions2.push(obj);
      // }
    },
    selectYear() {
      this.$emit("selectYear");
    },
    selectSemester() {
      this.$emit("selectSemester");
    },
  },
};
</script>

<style scoped>
</style>