<template>
  <div>
    <el-dialog
      class="modify-mark-setting"
      :visible.sync="modalIsShow"
      title="评卷设置"
      top="10px"
      width="660px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      destroy-on-close
      @open="visibleChange"
    >
      <el-form
        ref="modalFormComp"
        :model="modalForm"
        :rules="rules"
        :key="modalForm.paperNumber"
        label-width="160px"
      >
        <el-form-item prop="markMode" label="评卷模式：">
          <el-select v-model="modalForm.markMode">
            <el-option
              v-for="(val, key) in MARK_MODE_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="markStartTime" label="评卷时间：">
          <el-date-picker
            v-model="markTime"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="评卷开始日期"
            end-placeholder="评卷结束日期"
            value-format="timestamp"
            align="right"
            unlink-panels
            @change="dateChange"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="小助手原卷：">
          <el-checkbox v-model="modalForm.sheetView"></el-checkbox>
        </el-form-item> -->
        <el-form-item label="评卷是否显示客观分：">
          <el-checkbox v-model="modalForm.showObjectScore"></el-checkbox>
        </el-form-item>
        <el-form-item label="评卷提交自动定位：">
          <el-checkbox v-model="modalForm.autoScroll"></el-checkbox>
        </el-form-item>
        <el-form-item prop="passScore" label="及格分：">
          <el-input-number
            style="width: 125px"
            v-model="modalForm.passScore"
            :min="1"
            :max="100"
            :step="1"
            step-strictly
            :controls="false"
          ></el-input-number>
          <span class="ml-1">%</span>
        </el-form-item>
        <el-form-item prop="excellentScore" label="优秀分：">
          <el-input-number
            style="width: 125px"
            v-model="modalForm.excellentScore"
            :min="1"
            :max="100"
            :step="1"
            step-strictly
            :controls="false"
          ></el-input-number>
          <span class="ml-1">%</span>
        </el-form-item>
        <el-form-item prop="sheetConfig" label="原图遮盖：">
          <el-button
            class="btn-act-primary"
            type="text"
            @click="toSetSheetConfig"
            >设置</el-button
          >
          <i
            v-if="modalForm.sheetConfig.length"
            class="el-icon-success color-success ml-1"
          ></i>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" :disabled="isSubmit" @click="submit"
          >确认</el-button
        >
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>

    <!-- ModifyPaperArea -->
    <modify-paper-area
      ref="ModifyPaperArea"
      :areaConfigs="modalForm.sheetConfig"
      :paperList="paperList"
      @modified="areaModified"
    ></modify-paper-area>
  </div>
</template>

<script>
import {
  markSettingSave,
  markSettingDetail,
  examStructureFindJpg,
} from "../api";
import { MARK_MODE_TYPE } from "@/constants/enumerate";
import ModifyPaperArea from "./ModifyPaperArea.vue";
import pickerOptions from "@/constants/datePickerOptions";

const initModalForm = {
  examId: null,
  paperNumber: "",
  markMode: "",
  markStartTime: "",
  markEndTime: "",
  // sheetView: false,
  showObjectScore: false,
  autoScroll: false,
  passScore: 60,
  excellentScore: 80,
  sheetConfig: [],
};

export default {
  name: "modify-mark-setting",
  components: { ModifyPaperArea },
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      MARK_MODE_TYPE,
      modalForm: { ...initModalForm },
      paperList: [],
      rules: {
        markMode: [
          {
            required: true,
            message: "请选择评卷模式",
            trigger: "change",
          },
        ],
        markStartTime: [
          {
            required: true,
            message: "请设置评卷时间",
            trigger: "change",
          },
        ],
        passScore: [
          {
            required: true,
            message: "请设置及格分",
            trigger: "change",
          },
        ],
        excellentScore: [
          {
            required: true,
            message: "请设置优秀分",
            trigger: "change",
          },
        ],
      },
      // date-picker
      markTime: [],
      pickerOptions,
    };
  },
  methods: {
    async getPaperList() {
      this.paperList = [];
      const data = await examStructureFindJpg({
        examId: this.instance.examId,
        courseCode: this.instance.courseCode,
        paperNumber: this.instance.paperNumber,
        paperType: this.instance.paperType,
      });
      const papers = data || [];
      papers.sort((a, b) => a.index - b.index);
      this.paperList = papers.map((paper) => {
        return {
          imgUrl: paper.path,
          areas: [],
        };
      });
    },
    async initData(val) {
      await this.getPaperList();
      const res = await markSettingDetail({
        examId: this.instance.examId,
        paperNumber: this.instance.paperNumber,
      });
      this.modalForm = this.$objAssign(initModalForm, res || {});
      this.markTime = [
        this.modalForm.markStartTime || undefined,
        this.modalForm.markEndTime || undefined,
      ];
      const sheetConfig = res.sheetConfig || "[]";
      this.modalForm.sheetConfig = JSON.parse(sheetConfig);
      this.modalForm.passScore = this.modalForm.passScore || 60;
      this.modalForm.excellentScore = this.modalForm.excellentScore || 80;
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    dateChange() {
      if (this.markTime) {
        this.modalForm.markStartTime = this.markTime[0];
        this.modalForm.markEndTime = this.markTime[1];
      } else {
        this.modalForm.markStartTime = "";
        this.modalForm.markEndTime = "";
      }
    },
    areaModified(areas) {
      this.modalForm.sheetConfig = areas;
    },
    toSetSheetConfig() {
      this.$refs.ModifyPaperArea.open();
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = { ...this.modalForm };
      data.sheetConfig = JSON.stringify(data.sheetConfig);
      const res = await markSettingSave(data).catch(() => {
        this.isSubmit = false;
      });

      if (!res) return;

      this.isSubmit = false;
      this.$message.success("修改成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
