<!-- 班级学年选择框 -->
<template>
  <span>
    <cs-field-select
      :options="yearOptions"
      v-model="yearValue"
      :disabled="disabled"
      :clearable="false"
      :size="size"
    ></cs-field-select>
    <cs-field-select
      style="margin-left:5px;"
      :options="classOptions"
      v-model="classValue"
      :disabled="disabled"
      :clearable="false"
      :size="size"
    ></cs-field-select>
  </span>
</template>
<script>
import Request from "../../../../lib/utils/request";

export default {
  name: "CsFilterClass",
  props: {
    value: {
      default: null
    },
    tenantId: {
      type: Number,
      default: null
    },
    size: {
      type: String,
      default: "mini"
    },
    disabled: {
      type: Boolean,
      default: false
    },
    isDefaultAll: {
      type: Boolean,
      default: true
    }
  },
  components: {},
  data() {
    return {
      yearOptions: [],
      allClassOptions: [],
      classOptions: [],

      yearValue: 0,
      classValue: 0,

      isFirstLoad: true
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    yearValue(val) {
      if (this.isFirstLoad) {
        this.isFirstLoad = false;
        return;
      }
      this.setClassId();
    },
    classValue(val) {
      this.$emit("input", val);
    }
  },
  //方法集合
  methods: {
    /* 根据年级编号设置班级选项 */
    setClassId() {
      this.classOptions.splice(0, this.classOptions.length);
      this.allClassOptions.forEach(classOption => {
        if (classOption.YearId == this.yearValue) {
          this.classOptions.push(classOption);
        }
      });

      this.classOptions.unshift({ value: 0, label: "未分班" });
      this.classOptions.unshift({
        value: this.yearValue * -1,
        label: "全部"
      });

      this.classValue = this.yearValue * -1;
    },

    /* 构造数据 */
    initData() {
      this.classOptions.splice(0, this.classOptions.length);

      // 得到学年
      if (this.value > 0) {
        for (let i = 0; i < this.allClassOptions.length; i++) {
          const classOption = this.allClassOptions[i];
          if (classOption.Value == this.value) {
            this.yearValue = classOption.YearId;
            break;
          }
        }
        this.classValue = this.value;
      } else if (this.value < 0) {
        this.yearValue = this.value * -1;
      }

      if (this.isDefaultAll && !this.value) {
        this.classValue = this.yearValue * -1;
      }

      this.allClassOptions.forEach(classOption => {
        if (classOption.YearId == this.yearValue) {
          this.classOptions.push(classOption);
        }
      });

      this.classOptions.unshift({ value: 0, label: "未分班" });
      this.classOptions.unshift({
        value: this.yearValue * -1,
        label: "全部"
      });
    },

    /* 初始化请求 */
    init() {
      Request({
        url: "/api/Class/{0}".format(this.tenantId),
        method: "get"
      }).then(res => {
        this.yearValue = res.CurrentYearId;
        res.AcademicYearOptions.forEach(academicYearOption => {
          academicYearOption.Value = Number(academicYearOption.Value);
        });
        res.ClassOptions.forEach(classOption => {
          classOption.Value = Number(classOption.Value);
        });
        this.yearOptions = res.AcademicYearOptions;
        this.allClassOptions = res.ClassOptions;
        this.initData();
      });
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.init();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scoped>
</style>