<template>
  <el-dialog
    ref="dialog"
    title="导入试卷"
    width="560px"
    :visible.sync="visible"
    @close="closeDialog"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-position="right"
      label-width="120px"
    >
      <el-row>
        <el-form-item label="选择文件">
          <input @change="selectFile" type="file" /> *系统支持数据包
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="试卷包解析内容">
          <el-checkbox v-model="form.processPaper">试卷</el-checkbox>
          <el-checkbox v-model="form.processAnswer">答案</el-checkbox>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="音频播放次数">
          <el-input-number
            :min="1"
            v-model.trim="form.audioPlayCount"
          ></el-input-number>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="客观题小题乱序" prop="objectiveShuffle">
          <el-radio-group class="pull_right_sm" v-model="form.objectiveShuffle">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="客观题选项乱序" prop="optionShuffle">
          <el-radio-group class="pull_right_sm" v-model="form.optionShuffle">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row class="d-flex justify-content-center">
        <el-button type="primary" @click="submitForm" :loading="loading"
          >保 存</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import MD5 from "js-md5";
import { importPaper } from "@/api/examwork-task";

export default {
  name: "PaperImportDialog",
  props: {
    examId: String,
  },
  data() {
    return {
      visible: false,
      form: {},
      rules: {},
      loading: false,
    };
  },
  watch: {
    examId: {
      immediate: true,
      handler() {
        this.form = {
          processPaper: false,
          processAnswer: false,
          objectiveShuffle: false,
          optionShuffle: false,
          audioPlayCount: 0,
          file: "",
          fileName: "",
        };
      },
    },
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    selectFile(e) {
      this.form.file = e.target.files[0];
      this.form.fileName = this.form.file?.name;
    },
    async submitForm() {
      async function blobToArray(blob) {
        return new Promise((resolve) => {
          var reader = new FileReader();
          reader.addEventListener("loadend", function () {
            // reader.result contains the contents of blob as a typed array
            resolve(reader.result);
          });
          reader.readAsArrayBuffer(blob);
        });
      }
      const ab = await blobToArray(this.form.file);
      const md5 = MD5(ab);

      let data = this.form;

      try {
        this.loading = true;
        await importPaper({ ...data, examId: this.examId, md5 });
        this.$emit("reload");
        this.$notify({ title: "导入任务已成功启动", type: "success" });
        this.closeDialog();
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style></style>
