<template>
  <div :class="['secp-select', { inline: inline }]">
    <el-form-item v-if="filterProps.includes('semesterId')" label="学期：">
      <semester-select
        v-model.trim="filter.semesterId"
        placeholder="学期"
        :default-select="defaultSelectSemester || defaultSelectExam"
        :clearable="!defaultSelectExam"
        @default-selected="semesterDefaultSelect"
      ></semester-select>
    </el-form-item>
    <el-form-item v-if="filterProps.includes('examId')" label="考试：">
      <exam-select
        v-model="filter.examId"
        :semester-id="filter.semesterId"
        :clearable="!defaultSelectExam"
        :default-select="defaultSelectExam"
        @change="examChange"
        @default-selected="examDefaultSelect"
      ></exam-select>
    </el-form-item>
    <el-form-item v-if="filterProps.includes('printPlanId')" label="印刷计划：">
      <print-plan-select
        v-model.trim="filter.printPlanId"
        placeholder="印刷计划"
        clearable
        :semester-id="filter.semesterId"
        :exam-id="filter.examId"
      ></print-plan-select>
    </el-form-item>
    <el-form-item
      v-if="filterProps.includes('openCollegeId')"
      label="开课学院："
    >
      <org-select
        v-model="filter.openCollegeId"
        placeholder="开课学院"
        :filter-param="{
          withoutSecondOrg: true,
        }"
      ></org-select>
    </el-form-item>
    <el-form-item
      v-if="filterProps.includes('courseId')"
      label="课程（代码）："
      label-width="110px"
    >
      <course-select
        v-model.trim="filter.courseId"
        :semester-id="filter.semesterId"
        :exam-id="filter.examId"
        :print-plan-id="filter.printPlanId"
        :open-college-id="filter.openCollegeId"
        placeholder="课程（代码）"
        clearable
      ></course-select>
    </el-form-item>
    <el-form-item v-if="filterProps.includes('paperNumber')" label="试卷编号：">
      <paper-number-select
        ref="PaperNumberSelect"
        v-model="filter.paperNumber"
        :semester-id="filter.semesterId"
        :exam-id="filter.examId"
        :print-plan-id="filter.printPlanId"
        :course-id="filter.courseId"
        :open-college-id="filter.openCollegeId"
        placeholder="试卷编号"
        clearable
      ></paper-number-select>
    </el-form-item>
  </div>
</template>

<script>
const fullProps = [
  "semesterId",
  "examId",
  "printPlanId",
  "openCollegeId",
  "courseId",
  "paperNumber",
];
export default {
  name: "secp-select",
  props: {
    value: {
      type: Object,
      default() {
        return {};
      },
    },
    inline: {
      type: Boolean,
      default: true,
    },
    defaultSelectSemester: { type: Boolean, default: false },
    defaultSelectExam: { type: Boolean, default: false },
  },
  data() {
    return {
      filter: {},
      filterProps: [],
    };
  },
  watch: {
    filter: {
      deep: true,
      handler() {
        this.emitChange();
      },
    },
  },
  created() {
    this.initData();
    this.filter = this.$objAssign(this.filter, this.value);
  },
  methods: {
    initData() {
      this.filterProps = fullProps.filter((field) =>
        Object.prototype.hasOwnProperty.call(this.value, field)
      );
      let filter = {};
      this.filterProps.forEach((field) => {
        filter[field] = "";
      });
      this.filter = filter;
    },
    getFilterData() {
      return Object.assign({}, this.value, this.filter);
    },
    emitChange() {
      const data = this.getFilterData();
      this.$emit("input", data);
      this.$emit("change", data);
    },
    semesterDefaultSelect() {
      if (this.defaultSelectExam) return;
      this.emitChange();
      this.$emit("semester-default", this.getFilterData());
    },
    examDefaultSelect() {
      this.emitChange();
      this.$emit("exam-default", this.getFilterData());
    },
    examChange(data) {
      this.$emit("exam-change", data);
    },
  },
};
</script>

<style scoped>
.secp-select.inline {
  display: inline;
}
</style>
