<template>
  <Card>
    <p slot="title">
      <Row>
        <Col span="22">
          <i class="fa fa-plus" aria-hidden="true"></i>新建考试任务</Col
        >
        <Col span="2">
          <a href="javascript:void(0)" @click="goback"
            ><i class="fa fa-reply" aria-hidden="true"></i>返回</a
          ></Col
        >
      </Row>
    </p>
    <Form
      ref="entity_form"
      :model="entity"
      :label-width="120"
      :rules="entityrule"
    >
      <FormItem label="考试识别码" prop="examCode">
        <Row>
          <Col span="17">
            <Input
              type="text"
              :readonly="true"
              v-model="entity.examCode"
              placeholder="请输入考试任务名称"
            ></Input>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="考试任务名称" prop="name">
        <Row>
          <Col span="17">
            <Input
              type="text"
              v-model="entity.name"
              placeholder="请输入考试任务名称"
            ></Input>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="考试任务类型" prop="examType">
        <Row>
          <Col span="17">
            <Select v-model="entity.examType">
              <Option
                v-for="item in examTypeList"
                :value="item.value"
                :key="item.value"
                >{{ item.name }}
              </Option>
            </Select>
          </Col>
        </Row>
      </FormItem>
      <FormItem style="display: none" label="是否开启考情监控">
        <Row>
          <Col span="17">
            <Select v-model="entity.openMonitor">
              <Option
                v-for="item in openMonitorList"
                :value="item.value"
                :key="item.value"
                >{{ item.name }}
              </Option>
            </Select>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="是否现场出分" prop="sceneScore">
        <Row>
          <Col span="17">
            <Select v-model="entity.sceneScore">
              <Option
                v-for="item in sceneScoreList"
                :value="item.value"
                :key="item.value"
                >{{ item.name }}
              </Option>
            </Select>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="是否开启人脸识别" prop="faceVerify.open">
        <Row>
          <Col span="17">
            <Select v-model="entity.faceVerify.open">
              <Option
                v-for="item in faceVerifyList"
                :value="item.value"
                :key="item.value"
                >{{ item.name }}
              </Option>
            </Select>
          </Col>
        </Row>
      </FormItem>
      <FormItem
        label="抓拍时间间隔"
        prop="faceIntervalTime"
        v-if="entity.faceVerify.open == '1'"
      >
        <Row>
          <Col span="17">
            <Input
              type="number"
              v-model="entity.faceVerify.intervalTime"
              placeholder="单位分钟"
            >
              <span slot="append">分钟</span>
            </Input>
          </Col>
        </Row>
      </FormItem>

      <FormItem
        label="是否模拟测试"
        prop="simulation"
        v-if="entity.examType != 'SOLFEGE'"
      >
        <Row>
          <Col span="17">
            <Select v-model="entity.simulation">
              <Option
                v-for="item in simulationList"
                :value="item.value"
                :key="item.value"
                >{{ item.name }}
              </Option>
            </Select>
          </Col>
        </Row>
      </FormItem>
      <FormItem
        label="登录规则"
        prop="loginRule"
        v-if="entity.examType == 'SOLFEGE'"
      >
        <Row>
          <Col span="17">
            <Select v-model="entity.loginRule">
              <Option
                v-for="item in loginRuleList"
                :value="item.value"
                :key="item.value"
                >{{ item.name }}
              </Option>
            </Select>
          </Col>
        </Row>
      </FormItem>
      <FormItem
        label="抽卷规则"
        prop="paperRule"
        v-if="entity.examType == 'SOLFEGE'"
      >
        <Row>
          <Col span="17">
            <Select v-model="entity.paperRule">
              <Option
                v-for="item in paperRuleList"
                :value="item.value"
                :key="item.value"
                >{{ item.name }}
              </Option>
            </Select>
          </Col>
        </Row>
      </FormItem>
      <FormItem
        label="主题资源包"
        prop="clientTheme"
        v-if="entity.examType == 'SOLFEGE'"
      >
        <Row>
          <Col span="17">
            <Select v-model="entity.clientTheme">
              <Option
                v-for="item in clientThemeList"
                :value="item.value"
                :key="item.value"
                >{{ item.name }}
              </Option>
            </Select>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="考点考场数据">
        <Row type="flex" :gutter="10">
          <Col span="9">
            <Input
              :readonly="true"
              type="text"
              v-model="temp.kcFileName"
              placeholder="请上传考点考场文件"
            ></Input>
          </Col>
          <Col span="2">
            <Upload
              :before-upload="beforeUploadKc"
              action="//jsonplaceholder.typicode.com/posts/"
            >
              <Button
                ><i class="fa fa-folder" aria-hidden="true"></i>选择文件
              </Button>
            </Upload>
          </Col>
          <Col span="9">
            <ButtonGroup>
              <Button @click="upload(file.kc, 'kc')" style="margin-left: 12px"
                ><i class="fa fa-cloud-upload" aria-hidden="true"></i>开始导入
              </Button>
              <Button @click="viewroom()"
                ><i class="fa fa-search" aria-hidden="true"></i>查看考场数据
              </Button>
            </ButtonGroup>
          </Col>
        </Row>
        <Row type="flex" v-if="percent.kc > 0">
          <Col span="17">
            <Progress :percent="percent.kc"></Progress>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="考务考生数据">
        <Row type="flex" :gutter="10">
          <Col span="9">
            <Input
              :readonly="true"
              type="text"
              v-model="temp.kwZipFileName"
              placeholder="请上传考点考场文件"
            ></Input>
          </Col>
          <Col span="2">
            <Upload
              :before-upload="beforeUploadStudent"
              action="//jsonplaceholder.typicode.com/posts/"
            >
              <Button
                ><i class="fa fa-folder" aria-hidden="true"></i>选择文件
              </Button>
            </Upload>
          </Col>
          <Col span="9">
            <ButtonGroup>
              <Button
                @click="upload(file.kwZip, 'kwZip')"
                style="margin-left: 12px"
                ><i class="fa fa-cloud-upload" aria-hidden="true"></i>开始导入
              </Button>
              <Button @click="viewstudent"
                ><i class="fa fa-search" aria-hidden="true"></i>查看考生数据
              </Button>
            </ButtonGroup>
          </Col>
        </Row>
        <Row type="flex" v-if="percent.kwZip > 0">
          <Col span="17">
            <Progress :percent="percent.kwZip"></Progress>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="试卷数据">
        <Row type="flex" :gutter="10">
          <Col span="9">
            <Input
              :readonly="true"
              type="text"
              v-model="temp.paperFileName"
              placeholder="请上传考点考场文件"
            ></Input>
          </Col>
          <Col span="2">
            <Upload
              :before-upload="beforeUploadPaper"
              action="//jsonplaceholder.typicode.com/posts/"
            >
              <Button
                ><i class="fa fa-folder" aria-hidden="true"></i>选择文件
              </Button>
            </Upload>
          </Col>
          <Col span="9">
            <Button
              @click="upload(file.paper, 'paper')"
              style="margin-left: 12px"
              ><i class="fa fa-cloud-upload" aria-hidden="true"></i>开始导入
            </Button>
          </Col>
        </Row>
        <Row type="flex" v-if="percent.paper > 0">
          <Col span="17">
            <Progress :percent="percent.paper"></Progress>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="考生附加信息文件" v-if="entity.examType == 'UTEST'">
        <Row type="flex" :gutter="10">
          <Col span="9">
            <Input
              :readonly="true"
              type="text"
              v-model="temp.additionalFileName"
              placeholder="请上传考生信息附加文件"
            ></Input>
          </Col>
          <Col span="2">
            <Upload
              :before-upload="beforeUploadAdditional"
              action="//jsonplaceholder.typicode.com/posts/"
            >
              <Button
                ><i class="fa fa-folder" aria-hidden="true"></i>选择文件
              </Button>
            </Upload>
          </Col>
          <Col span="9">
            <Button
              @click="upload(file.addition, 'addition')"
              style="margin-left: 12px"
              ><i class="fa fa-cloud-upload" aria-hidden="true"></i>开始导入
            </Button>
          </Col>
        </Row>
        <Row type="flex" v-if="percent.addition > 0">
          <Col span="17">
            <Progress :percent="percent.addition"></Progress>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="考情监控数据包" v-if="entity.openMonitor == 'YES'">
        <Row type="flex" :gutter="10">
          <Col span="9">
            <Input
              :readonly="true"
              type="text"
              v-model="temp.roomMonitorFileName"
              placeholder="请上传考情监控数据包文件"
            ></Input>
          </Col>
          <Col span="2">
            <Upload
              :before-upload="beforeUploadRoomMonitor"
              action="//jsonplaceholder.typicode.com/posts/"
            >
              <Button
                ><i class="fa fa-folder" aria-hidden="true"></i>选择文件
              </Button>
            </Upload>
          </Col>
          <Col span="9">
            <Button
              @click="upload(file.roomMonitor, 'roomMonitor')"
              style="margin-left: 12px"
              ><i class="fa fa-cloud-upload" aria-hidden="true"></i>开始导入
            </Button>
          </Col>
        </Row>
        <Row type="flex" v-if="percent.roomMonitor > 0">
          <Col span="17">
            <Progress :percent="percent.roomMonitor"></Progress>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="标答文件" v-if="entity.sceneScore == 'YES'">
        <Row type="flex" :gutter="10">
          <Col span="9">
            <Input
              :readonly="true"
              type="text"
              v-model="temp.sceneScoreFileName"
              placeholder="请上传标答文件"
            ></Input>
          </Col>
          <Col span="2">
            <Upload
              :before-upload="beforeUploadSceneScore"
              action="//jsonplaceholder.typicode.com/posts/"
            >
              <Button
                ><i class="fa fa-folder" aria-hidden="true"></i>选择文件
              </Button>
            </Upload>
          </Col>
          <Col span="9">
            <Button
              @click="upload(file.sceneScore, 'sceneScore')"
              style="margin-left: 12px"
              ><i class="fa fa-cloud-upload" aria-hidden="true"></i>开始导入
            </Button>
          </Col>
        </Row>
        <Row type="flex" v-if="percent.sceneScore > 0">
          <Col span="17">
            <Progress :percent="percent.sceneScore"></Progress>
          </Col>
        </Row>
      </FormItem>
      <FormItem>
        <Button type="primary" @click="submit">提交</Button>
      </FormItem>
    </Form>
  </Card>
</template>

<script>
import { axios, baseUrl } from "../../libs/http";

export default {
  name: "exam_plan_add",
  data() {
    const faceIntervalTimeValidate = (rule, value, callback) => {
      if (this.entity.faceVerify.open == "0") {
        callback();
        return;
      }

      if (this.entity.faceVerify.intervalTime * 1 <= 0) {
        callback(new Error("请输入正确的抓拍时间间隔"));
      } else {
        callback();
      }
    };

    return {
      clientThemeList: [
        {
          name: "请选择",
          value: "",
        },
        {
          value: "WHYYXY",
          name: "武汉音乐学院",
        },
        {
          value: "SCYYXY",
          name: "四川音乐学院",
        },
      ],
      loginRuleList: [
        {
          name: "请选择",
          value: "",
        },
        {
          value: "ONLY_TICKET",
          name: "准考证号",
        },
        {
          value: "TICKET_IDCARD",
          name: "准考证号和身份证号",
        },
      ],
      paperRuleList: [
        {
          name: "请选择",
          value: "",
        },
        {
          value: "USE_LAST",
          name: "使用上次考试的试卷",
        },
        {
          value: "USE_NEW",
          name: "重新抽卷",
        },
      ],
      simulationList: [
        {
          name: "否",
          value: "0",
        },
        {
          name: "是",
          value: "1",
        },
      ],
      faceVerifyList: [
        {
          name: "否",
          value: "0",
        },
        {
          name: "是",
          value: "1",
        },
      ],
      openMonitorList: [
        {
          name: "否",
          value: "NO",
        },
        {
          name: "是",
          value: "YES",
        },
      ],
      sceneScoreList: [
        {
          name: "否",
          value: "NO",
        },
        {
          name: "是",
          value: "YES",
        },
      ],
      file: {
        kc: null,
        kwZip: null,
        paper: null,
        addition: null,
        roomMonitor: null,
        sceneScore: null,
      },
      percent: {
        kc: 0,
        kwZip: 0,
        paper: 0,
        addition: 0,
        roomMonitor: 0,
        sceneScore: 0,
      },
      temp: {
        kcFileName: "",
        kwZipFileName: "",
        paperFileName: "",
        additionalFileName: "",
        roomMonitorFileName: "",
        sceneScoreFileName: "",
      },
      examTypeList: [
        {
          name: "请选择",
          value: "",
        },
        {
          name: "通用机考",
          value: "NORMAL",
        },
        {
          name: "视唱机考",
          value: "SOLFEGE",
        },
        {
          name: "优特机考",
          value: "UTEST",
        },
      ],
      entity: {
        id: "",
        name: "",
        examCode: "",
        examType: "",
        kcFileId: "",
        kwZipFileId: "",
        paperFileId: "",
        additionalFileId: "",
        roomMonitorFileId: "",
        simulation: "",
        paperRule: "",
        loginRule: "",
        clientTheme: "",
        openMonitor: "NO",
        sceneScore: "",
        faceVerify: {
          open: "",
          intervalTime: 5,
        },
      },
      entityrule: {
        name: [
          {
            required: true,
            message: "任务名称不能为空",
            trigger: "blur",
          },
          {
            type: "string",
            max: 25,
            message: "任务名称必须在25个字符以内",
            trigger: "blur",
          },
        ],
        examCode: [
          {
            required: true,
            message: "考试识别码不能够为空",
            trigger: "blur",
          },
        ],
        examType: [
          {
            required: true,
            message: "任务类型不能够为空",
            trigger: "change",
          },
        ],
        loginRule: [
          {
            required: true,
            message: "登录规则不能够为空",
            trigger: "change",
          },
        ],
        paperRule: [
          {
            required: true,
            message: "抽卷规则不能够为空",
            trigger: "change",
          },
        ],
        clientTheme: [
          {
            required: true,
            message: "主题资源包不能够为空",
            trigger: "change",
          },
        ],
        simulation: [
          {
            required: true,
            message: "请选择是否模拟测试",
            trigger: "change",
          },
        ],
        ceneScore: [
          {
            required: true,
            message: "请选择是否开启现场出分",
            trigger: "change",
          },
        ],
        "faceVerify.open": [
          {
            required: true,
            message: "请选择是否开启人脸识别",
            trigger: "change",
          },
        ],
        faceIntervalTime: [
          {
            required: true,
            validator: faceIntervalTimeValidate,
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    goback() {
      this.$router.push({ name: "exam_plan_index" });
    },
    reset() {
      this.initData();
    },
    initData() {
      this.file = {
        kc: null,
        kwZip: null,
        paper: null,
        addition: null,
        roomMonitor: null,
        sceneScore: null,
      };
      this.percent = {
        kc: 0,
        kwZip: 0,
        paper: 0,
        addition: 0,
        roomMonitor: 0,
        sceneScore: 0,
      };
      this.temp = {
        kcFileName: "",
        kwZipFileName: "",
        paperFileName: "",
        additionalFileName: "",
        roomMonitorFileName: "",
        sceneScoreFileName: "",
      };
      this.entity = {
        id: "",
        name: "",
        examCode: "",
        examType: "",
        kcFileId: "",
        kwZipFileId: "",
        paperFileId: "",
        additionalFileId: "",
        roomMonitorFileId: "",
        sceneScoreFileId: "",
        simulation: "",
        openMonitor: "NO",
        sceneScore: "",
        faceVerify: {
          open: "",
          intervalTime: 5,
        },
      };
      this.createExamCode();
    },
    viewroom() {
      if (!this.entity.examType) {
        this.$Notice.error({
          title: "系统提示",
          desc: "<p>请先选择任务类型</p>",
        });
        return;
      }
      if (this.entity.examType == "SOLFEGE") {
        this.$Notice.error({
          title: "系统提示",
          desc: "<p>不支持视唱类机考查看考场数据</p>",
        });
        return;
      }
      if (!this.entity.kcFileId) {
        this.$Notice.error({
          title: "系统提示",
          desc: "<p>请导入考场文件</p>",
        });
        return;
      }
      let p = {};
      p.examCode = this.entity.examCode;
      p.attachmentId = this.entity.kcFileId;
      p.last = "exam_plan_add";
      this.$router.push({ name: "exam_plan_room_view", query: p });
    },
    viewstudent() {
      if (!this.entity.examType) {
        this.$Notice.error({
          title: "系统提示",
          desc: "<p>请先选择任务类型</p>",
        });
        return;
      }
      if (!this.entity.kwZipFileId) {
        this.$Notice.error({
          title: "系统提示",
          desc: "<p>请导入考务考生文件</p>",
        });
        return;
      }
      let p = {};
      p.examCode = this.entity.examCode;
      p.attachmentId = this.entity.kwZipFileId;
      p.examType = this.entity.examType;
      p.last = "exam_plan_add";
      this.$router.push({ name: "exam_plan_student_view", query: p });
    },
    upload(file, type) {
      if (!this.entity.examType) {
        this.$Notice.error({
          title: "系统提示",
          desc: "<p>请先选择任务类型</p>",
        });
        return;
      }
      if (file == undefined || file == null) {
        this.$Notice.error({
          title: "系统提示",
          desc: "<p>请先选择文件</p>",
        });
        return;
      }
      let fd = new FormData();
      fd.append("file", file);
      let xhr = new XMLHttpRequest();
      xhr.upload.addEventListener(
        "progress",
        (evt) => {
          this.uploadProgress(evt, type);
        },
        false
      );
      xhr.addEventListener(
        "load",
        (evt) => {
          this.uploadComplete(evt, type);
        },
        false
      );
      xhr.addEventListener(
        "error",
        (evt) => {
          this.uploadFailed(evt, type);
        },
        false
      );
      if (type == "kc") {
        xhr.open(
          "POST",
          baseUrl +
            "/system/kw/file/upload/" +
            this.entity.examCode +
            "/" +
            this.entity.examType +
            "/kc"
        ); //修改成自己的接口
      }
      if (type == "kwZip") {
        xhr.open(
          "POST",
          baseUrl +
            "/system/kw/file/upload/" +
            this.entity.examCode +
            "/" +
            this.entity.examType +
            "/student"
        ); //修改成自己的接口
      }
      if (type == "paper") {
        xhr.open(
          "POST",
          baseUrl +
            "/system/kw/file/upload/" +
            this.entity.examCode +
            "/" +
            this.entity.examType +
            "/paper"
        ); //修改成自己的接口
      }
      if (type == "addition") {
        xhr.open(
          "POST",
          baseUrl +
            "/system/kw/file/upload/" +
            this.entity.examCode +
            "/" +
            this.entity.examType +
            "/studentAdditional"
        ); //修改成自己的接口
      }
      if (type == "roomMonitor") {
        xhr.open(
          "POST",
          baseUrl +
            "/system/kw/file/upload/" +
            this.entity.examCode +
            "/" +
            this.entity.examType +
            "/roomMonitor"
        ); //修改成自己的接口
      }
      if (type == "sceneScore") {
        xhr.open(
          "POST",
          baseUrl +
            "/system/kw/file/upload/" +
            this.entity.examCode +
            "/" +
            this.entity.examType +
            "/sceneScore"
        ); //修改成自己的接口
      }
      xhr.setRequestHeader(
        "authorization-token",
        this.$store.state.user.accessToken
      );
      xhr.setRequestHeader("fileName", encodeURIComponent(file.name));
      xhr.setRequestHeader("schoolId", this.$store.state.user.schoolId);
      xhr.send(fd);
      this.$store.commit("showloading");
    },
    uploadProgress(evt, type) {
      if (evt.lengthComputable) {
        let percentComplete = Math.round((evt.loaded * 100) / evt.total);
        this.percent[type] = percentComplete;
      }
    },
    uploadComplete(evt, type) {
      this.$store.commit("closeloading");
      let statusCode = evt.target.status;
      let title = "";
      let successMsg = "";
      if (type == "kc") {
        title = "考场文件导入失败";
        successMsg = "考场文件导入成功";
      }
      if (type == "kwZip") {
        title = "考务考生文件导入失败";
        successMsg = "考务考生文件导入成功";
      }
      if (type == "paper") {
        title = "试卷文件导入失败";
        successMsg = "试卷文件导入成功";
      }
      if (type == "addition") {
        title = "考生附加信息文件导入失败";
        successMsg = "考生附加信息文件导入成功";
      }
      if (type == "roomMonitor") {
        title = "考情监控数据包导入失败";
        successMsg = "考情监控数据包导入成功";
      }
      if (type == "sceneScore") {
        title = "标答文件导入失败";
        successMsg = "标答文件导入成功";
      }
      if (statusCode == 200) {
        let data = JSON.parse(evt.target.responseText);
        if (data.code != 0) {
          this.percent[type] = 0;
          this.$Notice.error({
            title: title,
            desc: "<p>" + data.message + "</p>",
          });
        } else {
          if (type == "kc") {
            this.entity.kcFileId = data.data.id;
          }
          if (type == "kwZip") {
            this.entity.kwZipFileId = data.data.id;
          }
          if (type == "paper") {
            this.entity.paperFileId = data.data.id;
          }
          if (type == "addition") {
            this.entity.additionalFileId = data.data.id;
          }
          if (type == "roomMonitor") {
            this.entity.roomMonitorFileId = data.data.id;
          }
          if (type == "sceneScore") {
            this.entity.sceneScoreFileId = data.data.id;
          }
          this.$Notice.success({
            title: "导入成功",
            desc: "<p>" + successMsg + "</p>",
          });
        }
      } else {
        this.$Notice.error({
          title: title,
          desc: "<p>" + evt + "</p>",
        });
      }
    },
    uploadFailed(evt, type) {
      this.$store.commit("closeloading");
      let title = "";
      if (type == "kc") {
        title = "考场文件导入失败";
      }
      if (type == "kwZip") {
        title = "考务考生文件导入失败";
      }
      if (type == "paper") {
        title = "试卷文件导入失败";
      }
      if (type == "addition") {
        title = "考生附加信息文件导入失败";
      }
      if (type == "roomMonitor") {
        title = "考情监控数据包导入失败";
      }
      if (type == "sceneScore") {
        title = "标答文件导入失败";
      }
      this.percent[type] = 0;
      this.$Notice.error({
        title: "系统提示",
        desc: "<p>" + title + "</p>",
      });
    },
    submit() {
      this.$refs["entity_form"].validate((valid) => {
        if (valid) {
          if (!this.entity.kcFileId) {
            this.$Notice.error({
              title: "表单验证失败",
              desc: "<p>请导入考点考场数据</p>",
            });
            return;
          }
          if (!this.entity.kwZipFileId) {
            this.$Notice.error({
              title: "表单验证失败",
              desc: "<p>请导入考务考生数据</p>",
            });
            return;
          }
          if (!this.entity.paperFileId) {
            this.$Notice.error({
              title: "表单验证失败",
              desc: "<p>请导入试卷数据</p>",
            });
            return;
          }
          if (this.entity.sceneScore && this.entity.sceneScore == "YES") {
            if (!this.entity.sceneScoreFileId) {
              this.$Notice.error({
                title: "表单验证失败",
                desc: "<p>请导入标答文件</p>",
              });
              return;
            }
          }
          if (this.entity.examType == "UTEST") {
            if (!this.entity.additionalFileId) {
              this.$Notice.error({
                title: "表单验证失败",
                desc: "<p>请导入考生附加信息表</p>",
              });
              return;
            }
          }
          //提交表单
          let entityData = { ...this.entity };
          entityData["faceVerify.open"] = this.entity.faceVerify.open;
          entityData["faceVerify.intervalTime"] =
            this.entity.faceVerify.intervalTime;
          delete entityData.faceVerify;

          axios
            .post("/system/examPlan/addExamPlan", entityData)
            .then((response) => {
              let data = response.data;
              if (data.code == 0) {
                this.$Notice.success({
                  title: "系统提示",
                  desc: "<p>考试任务创建成功</p>",
                });
                this.$store.commit("closePage", "exam_plan_add");
                this.$store.commit("removeTag", "exam_plan_add");
                this.initData();
                this.$router.push({
                  name: "exam_plan_index",
                  query: { submit: true },
                });
              }
            });
        }
      });
    },
    beforeUploadKc(file) {
      if (file) {
        let fileSize = 0;
        if (file.size > 1024 * 1024)
          fileSize =
            (Math.round((file.size * 100) / (1024 * 1024)) / 100).toString() +
            "MB";
        else
          fileSize =
            (Math.round((file.size * 100) / 1024) / 100).toString() + "KB";
        this.temp.kcFileName = file.name + "(" + fileSize + ")";
        this.file.kc = file;
        this.percent.kc = 0;
      }
      return false;
    },
    beforeUploadStudent(file) {
      if (file) {
        let fileSize = 0;
        if (file.size > 1024 * 1024)
          fileSize =
            (Math.round((file.size * 100) / (1024 * 1024)) / 100).toString() +
            "MB";
        else
          fileSize =
            (Math.round((file.size * 100) / 1024) / 100).toString() + "KB";
        this.temp.kwZipFileName = file.name + "(" + fileSize + ")";
        this.file.kwZip = file;
        this.percent.kwZip = 0;
      }
      return false;
    },
    beforeUploadPaper(file) {
      if (file) {
        let fileSize = 0;
        if (file.size > 1024 * 1024)
          fileSize =
            (Math.round((file.size * 100) / (1024 * 1024)) / 100).toString() +
            "MB";
        else
          fileSize =
            (Math.round((file.size * 100) / 1024) / 100).toString() + "KB";
        this.temp.paperFileName = file.name + "(" + fileSize + ")";
        this.file.paper = file;
        this.percent.paper = 0;
      }
      return false;
    },

    beforeUploadAdditional(file) {
      if (file) {
        let fileSize = 0;
        if (file.size > 1024 * 1024)
          fileSize =
            (Math.round((file.size * 100) / (1024 * 1024)) / 100).toString() +
            "MB";
        else
          fileSize =
            (Math.round((file.size * 100) / 1024) / 100).toString() + "KB";
        this.temp.additionalFileName = file.name + "(" + fileSize + ")";
        this.file.addition = file;
        this.percent.addition = 0;
      }
      return false;
    },
    beforeUploadRoomMonitor(file) {
      if (file) {
        let fileSize = 0;
        if (file.size > 1024 * 1024)
          fileSize =
            (Math.round((file.size * 100) / (1024 * 1024)) / 100).toString() +
            "MB";
        else
          fileSize =
            (Math.round((file.size * 100) / 1024) / 100).toString() + "KB";
        this.temp.roomMonitorFileName = file.name + "(" + fileSize + ")";
        this.file.roomMonitor = file;
        this.percent.roomMonitor = 0;
      }
      return false;
    },
    beforeUploadSceneScore(file) {
      if (file) {
        let fileSize = 0;
        if (file.size > 1024 * 1024)
          fileSize =
            (Math.round((file.size * 100) / (1024 * 1024)) / 100).toString() +
            "MB";
        else
          fileSize =
            (Math.round((file.size * 100) / 1024) / 100).toString() + "KB";
        this.temp.sceneScoreFileName = file.name + "(" + fileSize + ")";
        this.file.sceneScore = file;
        this.percent.sceneScore = 0;
      }
      return false;
    },
    createExamCode() {
      if (!this.entity.id && !this.entity.examCode) {
        axios.post("/system/examPlan/createExamCode", {}).then((response) => {
          let data = response.data.data;
          if (data) {
            this.entity.examCode = data;
          }
        });
      }
    },
  },
  beforeRouteLeave(to, from, next) {
    if (
      to.name != "exam_plan_room_view" &&
      to.name != "exam_plan_student_view"
    ) {
      if (to.query.submit) {
        next();
      } else {
        this.$Modal.confirm({
          title: "系统提示",
          content:
            "您尚未保存已经填写的考试任务数据，离开当前页面这些数据会丢失，确定要离开当前页面吗？",
          onOk() {
            next();
          },
        });
      }
    } else {
      next();
    }
  },
  activated() {
    let _r = this.$route.query._r;
    if (!_r) {
      this.reset();
    }
  },
};
</script>
