<template>
  <div class="preview-task-paper">
    <el-dialog
      class="task-detail"
      :visible.sync="modalIsShow"
      title="预览"
      top="10vh"
      width="900px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      @open="visibleChange"
      @close="closeHandle"
    >
      <div class="part-box part-box-pad part-box-border">
        <el-form class="form-info" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="试卷编号：">
                <span>{{ rowData.paperNumber }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="课程（代码）：">
                <span>
                  {{ rowData.courseName }}（{{ rowData.courseCode }}）
                </span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="卷型：">
                <span>{{ rowData.paperType }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="命题老师：">
                <span>{{ rowData.userName }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="已曝光：">
                <span v-if="rowData.exposedPaperType">
                  卷{{ rowData.exposedPaperType }}
                </span>
                <span v-else>--</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="未曝光：">
                <span v-if="rowData.unexposedPaperType">
                  卷{{ rowData.unexposedPaperType }}
                </span>
                <span v-else>--</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <task-paper
        v-if="dataReady"
        ref="TaskPaper"
        @view-attachment="toViewAttachment"
      ></task-paper>

      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>

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

<script>
import { mapState, mapMutations } from "vuex";
import PreviewAttachment from "@/components/PreviewAttachment.vue";
import PreviewFile from "@/components/PreviewFile.vue";
import TaskPaper from "./taskApply/TaskPaper.vue";
import { taskApplyDetail } from "../api";
import { attachmentPreview } from "../../login/api";

const initExamTask = {
  id: null,
  examId: "",
  examName: "",
  examModel: "",
  category: "",
  semesterId: "",
  semesterName: "",
  courseId: "",
  courseCode: "",
  courseName: "",
  specialty: "",
  paperNumber: "",
  startTime: "",
  endTime: "",
  cardRuleId: "",
  cardRuleName: "",
  flowId: "",
  setup: null,
  userId: "",
  userName: "",
  propositionName: "",
  auditStatus: "",
  reviewStatus: "",
  source: "",
  teachingRoomName: "",
  teacherName: "",
  lecturerName: "",
  openAb: false,
};

const initTaskApply = {
  examId: "",
  examTaskId: "",
  category: "",
  paperNumber: "",
  paperType: "A",
  paperAttachmentIds: "",
  paperConfirmAttachmentIds: "",
  cardId: "",
  cardRuleId: "",
  makeMethod: "",
  remark: "",
  courseId: "",
  courseCode: "",
  courseName: "",
  drawCount: 1,
  exposedPaperType: "",
  auditContent: [],
  examTaskDetailList: [],
  openAb: false,
  // 入库申请中新建的命题任务提交信息
  examTaskContent: "",

  // 流程
  flowId: "",
  flowStatus: "",
  setup: null,
  // 工作台任务id
  flowTaskId: "",
  // 题卡状态
  status: "",
  // 考务规则
  review: false,
  includePaper: false,
  customCard: false,
  // 作废
  taskStatus: "",
  cancelRemark: "",
};

export default {
  name: "preview-task-paper",
  components: {
    PreviewAttachment,
    TaskPaper,
    PreviewFile,
  },
  props: {
    rowData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    ...mapState("exam", ["examTask", "curTaskApply", "taskStatus"]),
  },
  data() {
    return {
      isSubmit: false,
      modalIsShow: false,
      dataReady: false,
      attachmentIds: [],
      // preview file
      curFile: {
        url: "",
        type: "",
      },
    };
  },
  methods: {
    ...mapMutations("exam", [
      "setExamTask",
      "setCurTaskApply",
      "setTaskStatus",
    ]),
    visibleChange() {
      this.initData();
    },
    async initData() {
      const data = await taskApplyDetail(this.rowData.id);
      const examTask = this.$objAssign(initExamTask, {
        ...this.rowData,
        setup: data.setup,
        semesterName: data.semesterName,
        examId: data.examId,
        examName: data.examName,
        examModel: data.examModel,
      });

      const curTaskApply = this.$objAssign(initTaskApply, {
        ...data,
        examTaskId: examTask.id,
        courseId: examTask.courseId,
        courseName: examTask.courseName,
        cardRuleId: examTask.cardRuleId,
        customCard: examTask.customCard,
        paperNumber: examTask.paperNumber,
        auditContent: JSON.parse(data.auditContent || "[]"),
        includePaper: data.printContent.indexOf("PAPER") !== -1,
      });

      examTask.openAb = data.openAb;
      this.setExamTask(examTask);
      this.setCurTaskApply(curTaskApply);

      this.setTaskStatus({
        IS_APPLY: false,
        IS_PREVIEW: true,
        IS_AUDIT: false,
        IS_REBUILD: curTaskApply.category === "REBUILD",
      });
      this.dataReady = true;
    },
    closeHandle() {
      this.dataReady = false;
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async toViewAttachment(attachment) {
      if (!attachment.jpgAttachmentId) {
        if (!attachment.attachmentId) {
          this.$message.error("附件丢失！");
          return;
        }
        const data = await attachmentPreview(attachment.attachmentId);
        if (!data[0].url) {
          this.$message.error("附件丢失！");
          return;
        }
        this.curFile = {
          url: data[0].url,
          type: "application/pdf",
        };
        this.$refs.PreviewFile.open();
        return;
      }

      const datas = JSON.parse(attachment.jpgAttachmentId);
      this.attachmentIds = datas.map((item) => item.attachmentId);
      this.$refs.PreviewAttachment.open();
    },
    toViewCard(attachment) {
      this.curAttachment = { ...attachment };
      this.$refs.CardPreviewDialog.open();
    },
  },
};
</script>
