<template>
  <div class="mark-param-upload-answer">
    <div class="box-justify part-box part-box-pad">
      <div>
        <p class="tips-info">1.主观题标答请上传PDF文档；</p>
        <p class="tips-info">
          2.主观题标答文档上传后，在评卷界面小助手里可以打开进行查看，作为评卷参考；
        </p>
        <p class="tips-info">3.支持重复提交，以最后一次提交标答文件为准。</p>
      </div>
      <div>
        <mark-status field="answerFile"></mark-status>
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-form ref="modalFormComp" :model="infos" label-width="50px">
        <div class="part-box">
          <h3 class="mb-2">
            卷型{{ markParamInfos.basicPaperInfo.paperType }}
          </h3>
          <el-form-item prop="file" label="标答：">
            <select-file
              :format="fileFormat"
              :disabled="loading"
              @file-change="fileChange"
            ></select-file>
            <p v-if="infos.errorMsg" class="tips-info tips-error">
              {{ infos.errorMsg }}
            </p>
          </el-form-item>
          <el-form-item v-if="answerFileUrl">
            <span
              class="cont-link"
              title="点击查看已上传标答文件"
              @click="toViewAnswer"
            >
              <i class="el-icon-document mr-1"></i>{{ answerFileName }}
            </span>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="text-center">
      <el-button type="primary" :disabled="loading" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { examStructureUploadAnswer } from "../../api";
import SelectFile from "@/components/SelectFile.vue";
import MarkStatus from "./MarkStatus.vue";

export default {
  name: "mark-param-upload-answer",
  components: { SelectFile, MarkStatus },
  data() {
    return {
      modalIsShow: false,
      loading: false,
      infos: { file: null, md5: null, errorMsg: null },
      fileFormat: ["pdf"],
    };
  },
  computed: {
    ...mapState("markParam", ["markParamInfos"]),
    answerFileName() {
      return `${this.markParamInfos.basicPaperInfo.courseName}-标答文件`;
    },
    answerFileUrl() {
      const { paperType, paperAnswer } = this.markParamInfos.basicPaperInfo;
      if (!paperAnswer) return;
      const paperAnswerInfo = JSON.parse(paperAnswer);
      const paper = paperAnswerInfo.find(
        (item) => item.paperType === paperType
      );
      return paper ? paper.answerUrl : "";
    },
  },
  methods: {
    ...mapMutations("markParam", ["setMarkParamInfos"]),
    toViewAnswer() {
      window.open(this.answerFileUrl);
    },
    fileChange(data) {
      if (data.errorMsg) {
        this.infos.file = null;
        this.infos.md5 = null;
        this.infos.errorMsg = data.errorMsg;
      } else {
        this.infos.file = data.file;
        this.infos.md5 = data.md5;
        this.infos.errorMsg = null;
      }
    },
    async submit() {
      if (!this.infos.file) {
        this.$message.error("请选择标答文件");
        return;
      }

      if (this.loading) return;
      this.loading = true;

      let formData = new FormData();
      formData.append("id", this.markParamInfos.basicPaperInfo.id);
      formData.append(`file`, this.infos.file);
      formData.append(`md5`, this.infos.md5);
      const data = await examStructureUploadAnswer(formData).catch(() => {});
      this.loading = false;
      if (!data) return;

      this.$message.success("上传成功！");
      this.$emit("confirm");
    },
    cancel() {
      this.$emit("cancel");
    },
  },
};
</script>
