<template>
  <el-dialog
    class="push-set-dialog"
    :visible.sync="modalIsShow"
    title="推送云阅卷设置"
    top="10vh"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form label-width="150px">
      <el-form-item label="是否推送客观分：">
        <el-radio-group v-model="modalForm.objectiveScorePush">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否推送违纪考生：">
        <el-radio-group v-model="modalForm.examStudentBreachPush">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { pushCloudMarkExam } from "@/api/examwork-exam";

const initModalForm = {
  examId: null,
  objectiveScorePush: false,
  examStudentBreachPush: false,
};

export default {
  name: "PushSetDialog",
  props: {
    exam: {
      type: Object,
      default() {
        return;
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = {
        examId: this.exam.id,
        objectiveScorePush: !!this.exam.objectiveScorePush,
        examStudentBreachPush: !!this.exam.examStudentBreachPush,
      };
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      if (this.exam.cloudMarkPushDataStatus === "FINISH") {
        const confirm = await this.$confirm(
          `当前批次已经推送过，确定要重新推送吗?`,
          "提示",
          {
            type: "warning",
          }
        ).catch(() => {});
        if (confirm !== "confirm") return;
      }

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

      if (!data) return;

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