<template>
  <el-dialog
    class="print-plan-batch-push-dialog page-dialog"
    :visible.sync="modalIsShow"
    title="推送课程"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <div class="part-box part-box-pad">
      <el-form
        ref="modalFormComp"
        :model="modalForm"
        :rules="rules"
        label-width="90px"
      >
        <el-form-item prop="thirdRelateName" label="考试名称：">
          <el-input
            v-model.trim="modalForm.thirdRelateName"
            placeholder="请输入云阅卷考试名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="examTime" label="考试时间：">
          <el-date-picker
            v-model="modalForm.examTime"
            type="datetime"
            value-format="timestamp"
            placeholder="请选择考试时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="confirm"
            >开始推送</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer"></div>
  </el-dialog>
</template>

<script>
import { printPlanSyncStmms } from "../api";

const initModalForm = {
  thirdRelateName: "",
  list: [],
  examTime: null,
};

export default {
  name: "print-plan-batch-push-dialog",
  props: {
    courses: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      rules: {
        thirdRelateName: [
          {
            required: true,
            message: "请输入云阅卷考试名称",
            trigger: "change",
          },
          {
            max: 200,
            message: "云阅卷考试名称不得超过200字符",
            trigger: "change",
          },
        ],
        examTime: [
          {
            required: true,
            message: "请选择考试时间",
            trigger: "change",
          },
        ],
      },
      loading: false,
    };
  },
  methods: {
    initData() {
      let thirdRelateName = `${this.courses[0].semesterName}-${this.courses[0].examName}`;
      if (this.courses.length === 1) {
        thirdRelateName = this.courses[0].thirdRelateName || thirdRelateName;
      }
      this.modalForm = {
        thirdRelateName,
        list: this.courses,
        examTime: Date.now(),
      };

      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    visibleChange() {
      this.initData();
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.loading) return;
      this.loading = true;
      const data = await printPlanSyncStmms(this.modalForm).catch(() => {});
      this.loading = false;

      if (!data) return;

      this.$message.success("提交成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
