<template>
  <div class="topic-task-modify part-box part-box-border part-box-pad">
    <el-form
      ref="ModalForm"
      :model="modalForm"
      :rules="rules"
      label-width="180px"
      style="min-width:800px;"
    >
      <el-form-item prop="examId" label="考试名称：">
        <el-select
          v-model="modalForm.examId"
          style="width: 439px;"
          placeholder="请选择"
          @change="examChange"
          clearable
          v-if="!isEdit"
        >
          <el-option
            v-for="item in exams"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
        <el-input
          v-model.trim="modalForm.examName"
          style="width: 439px;"
          readonly
          v-else
        ></el-input>
      </el-form-item>
      <el-form-item prop="courseCode" label="科目：">
        <el-select
          v-model="modalForm.courseCode"
          style="width: 439px;"
          placeholder="请选择"
          @change="courseChange"
          clearable
          v-if="!isEdit"
        >
          <el-option
            v-for="item in courses"
            :key="item.courseCode"
            :value="item.courseCode"
            :label="item.courseName"
          ></el-option>
        </el-select>
        <el-input
          v-model.trim="modalForm.courseName"
          style="width: 439px;"
          readonly
          v-else
        ></el-input>
      </el-form-item>
      <el-form-item label="考试开始时间：">
        <el-input
          v-model.trim="modalForm.beginTime"
          style="width: 439px;"
          readonly
        ></el-input>
      </el-form-item>
      <el-form-item prop="questionTeacherId" label="命题老师：">
        <el-select
          v-model="modalForm.questionTeacherId"
          style="width: 439px;"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in teachers"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :disabled="isSubmit" @click="save"
          >保存</el-button
        >
        <el-button @click="goback">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  updateTopicTask,
  topicTaskDetail,
  topicTaskExamList,
  topicTaskExamCourseList,
  topicTaskExamTeacherList
} from "../api";

export default {
  name: "topic-task-modify",
  data() {
    return {
      taskId: this.$route.params.taskId,
      modalForm: {
        examId: "",
        examName: "",
        courseCode: "",
        courseName: "",
        beginTime: "",
        questionTeacherId: ""
      },
      curExam: {},
      rules: {
        examId: [
          {
            required: true,
            message: "请选择考试",
            trigger: "change"
          }
        ],
        courseCode: [
          {
            required: true,
            message: "请选择科目",
            trigger: "change"
          }
        ],
        questionTeacherId: [
          {
            required: true,
            message: "请选择命题老师",
            trigger: "change"
          }
        ]
      },
      exams: [],
      courses: [],
      teachers: [],
      isSubmit: false
    };
  },
  computed: {
    isEdit() {
      return !!this.taskId;
    }
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      if (this.isEdit) {
        await this.getTaskDetail();
        this.getTeachers();
      } else {
        this.getExams();
      }
    },
    async getTaskDetail() {
      const data = await topicTaskDetail(this.taskId);
      this.modalForm = this.$objAssign(this.modalForm, data);
    },
    async getExams() {
      const data = await topicTaskExamList();
      this.exams = data.map(item => {
        return {
          id: item.id,
          name: item.examName,
          beginTime: item.beginTime
        };
      });
    },
    async getCourses() {
      this.courses = await topicTaskExamCourseList(this.modalForm.examId);
    },
    async getTeachers() {
      this.teachers = await topicTaskExamTeacherList(this.modalForm.courseCode);
    },
    examChange() {
      this.modalForm.courseCode = "";
      this.modalForm.questionTeacherId = "";
      this.courses = [];
      this.teachers = [];
      if (!this.modalForm.examId) {
        this.curExam = {};
        return;
      }
      this.curExam = this.exams.find(item => item.id === this.modalForm.examId);
      this.modalForm.beginTime = this.curExam.beginTime;
      this.getCourses();
    },
    courseChange() {
      this.modalForm.questionTeacherId = "";
      this.teachers = [];
      if (!this.modalForm.courseCode) return;
      this.getTeachers();
    },
    async save() {
      const valid = await this.$refs["ModalForm"].validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const datas = {
        ...this.modalForm
      };
      if (this.isEdit) {
        datas.taskId = this.taskId;
      } else {
        const curCourse = this.courses.find(
          item => item.courseCode === datas.courseCode
        );
        datas.courseName = curCourse.courseName;
      }
      const data = await updateTopicTask(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;
      this.$message.success("保存成功！");
      this.goback();
    }
  }
};
</script>
