<template>
  <div class="mark-param-subjective-answer">
    <div class="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>
    <div class="part-box part-box-pad">
      <el-form ref="modalFormComp" :model="infos" label-width="50px">
        <div class="part-box">
          <h3 class="mb-2">卷型{{ basicInfo.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>

    <!-- PreviewFile -->
    <preview-file ref="PreviewFile" :data="curFile"></preview-file>
  </div>
</template>

<script>
import { mapState } from "vuex";
import {
  markSubjectiveQuestionList,
  markSubjectiveQuestionUpload,
} from "../../api";
import SelectFile from "@/components/SelectFile.vue";
import PreviewFile from "@/components/PreviewFile.vue";

export default {
  name: "mark-param-subjective-answer",
  components: { SelectFile, PreviewFile },
  data() {
    return {
      modalIsShow: false,
      loading: false,
      infos: { file: null, md5: null, errorMsg: null },
      fileFormat: ["pdf"],
      answerFileUrl: "",
      // preview file
      curFile: {
        url: "",
        type: "",
      },
    };
  },
  computed: {
    ...mapState("markParam", ["basicInfo"]),
    answerFileName() {
      return `${this.basicInfo.courseName}-标答文件`;
    },
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      const params = {
        examId: this.basicInfo.examId,
        paperNumber: this.basicInfo.paperNumber,
      };
      const res = await markSubjectiveQuestionList(params);
      this.answerFileUrl = res || "";
    },
    toViewAnswer() {
      // window.open(this.answerFileUrl);
      this.curFile = {
        url: this.answerFileUrl,
        type: "application/pdf",
      };
      this.$refs.PreviewFile.open();
    },
    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("examId", this.basicInfo.examId);
      formData.append("paperNumber", this.basicInfo.paperNumber);
      formData.append(`file`, this.infos.file);
      formData.append(`md5`, this.infos.md5);
      const data = await markSubjectiveQuestionUpload(formData).catch(() => {});
      this.loading = false;
      if (!data) return;

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