<template>
  <div class="mark-setting">
    <div class="part-box part-box-filter">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <secp-select
            v-model="filter"
            defaultSelectExam
            @exam-default="search"
            @exam-change="examChange"
          ></secp-select>
        </template>
        <el-form-item label="状态">
          <el-select
            v-model="filter.groupStatus"
            style="width: 120px"
            placeholder="状态"
            clearable
          >
            <el-option :value="1" label="已提交"></el-option>
            <el-option :value="0" label="未提交"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="box-justify">
        <div>
          <upload-button
            v-if="checkPrivilege('button', 'CardJsonImport')"
            btn-content="导入数据包"
            btn-type="primary"
            btn-icon="el-icon-upload2"
            upload-url="/api/admin/mark/setting/card_json/import"
            :upload-data="{ examId: filter.examId }"
            :format="['zip']"
            style="margin: 0"
            @valid-error="validError"
            @upload-success="uploadJsonSuccess"
          >
          </upload-button>
        </div>
        <div>
          <el-button
            v-if="checkPrivilege('button', 'BatchMarkConfig')"
            type="primary"
            icon="el-icon-setting"
            :disabled="!multipleSelection.length"
            @click="toBatchModifySetting"
          >
            评卷设置
          </el-button>
          <el-button
            v-if="checkPrivilege('button', 'SubjectiveStructImport')"
            type="primary"
            icon="el-icon-upload2"
            :disabled="!isModel4"
            @click="toImportSubjective"
            >主观题导入</el-button
          >
          <el-button
            v-if="checkPrivilege('button', 'SubjectiveStructImport')"
            type="success"
            icon="el-icon-s-order"
            :disabled="!isModel4"
            @click="toDataTask('SUBJECTIVE_STRUCT_IMPORT')"
            >主观题导入结果查询</el-button
          >
          <el-button
            v-if="checkPrivilege('button', 'ObjectiveStructImport')"
            type="primary"
            icon="el-icon-upload2"
            :disabled="!isModel4"
            @click="toImportObjective"
            >客观题导入</el-button
          >
          <el-button
            v-if="checkPrivilege('button', 'ObjectiveStructImport')"
            type="success"
            icon="el-icon-s-order"
            :disabled="!isModel4"
            @click="toDataTask('OBJECTIVE_STRUCT_IMPORT')"
            >客观题导入结果查询</el-button
          >
        </div>
      </div>
    </div>

    <div class="part-box part-box-pad">
      <el-table
        ref="TableList"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          fixed="left"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程（代码）" min-width="260">
          <template slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </template>
        </el-table-column>
        <el-table-column
          prop="openCollege"
          label="开课学院"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="paperNumber"
          label="试卷编号"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="paperType"
          label="卷型"
          width="80"
        ></el-table-column>
        <el-table-column prop="questionStatus" label="试卷结构" width="100">
          <span slot-scope="scope">{{
            scope.row.questionStatus ? "已提交" : "未提交"
          }}</span>
        </el-table-column>
        <el-table-column prop="groupStatus" label="评卷任务" width="100">
          <span slot-scope="scope">{{
            scope.row.groupStatus ? "已提交" : "未提交"
          }}</span>
        </el-table-column>
        <el-table-column prop="markMode" label="评卷模式" width="120">
          <template slot-scope="scope">
            {{ scope.row.markMode | markModeTypeFilter }}
          </template>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'MarkParamSetting')"
              class="btn-primary"
              type="text"
              @click="toSetParams(scope.row)"
              >评卷参数设置</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'MarkConfig')"
              class="btn-primary"
              type="text"
              @click="toModifySetting(scope.row)"
              >评卷设置</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ModifyMarkSetting -->
    <modify-mark-setting
      ref="ModifyMarkSetting"
      :instance="curRow"
      :multiple-inst="multipleInst"
      @modified="getList"
    ></modify-mark-setting>
    <!-- ModifyMarkParams -->
    <modify-mark-params
      ref="ModifyMarkParams"
      :instance="curRow"
      @modified="getList"
    ></modify-mark-params>
    <!-- 主观题导入 -->
    <import-file
      v-if="checkPrivilege('button', 'SubjectiveStructImport')"
      ref="ImportSubjectiveFile"
      title="主观题导入"
      upload-url="/api/admin/mark/setting/subjective_struct/import"
      :upload-data="{ examId: filter.examId }"
      :download-handle="() => downloadTemplate('subjectiveStruct')"
      download-filename="主观题导入模板.xlsx"
      :format="['xls', 'xlsx']"
      @upload-success="uploadSuccess"
    >
    </import-file>
    <!-- 客观题导入 -->
    <import-file
      v-if="checkPrivilege('button', 'ObjectiveStructImport')"
      ref="ImportObjectiveFile"
      title="客观题导入"
      upload-url="/api/admin/mark/setting/objective_struct/import"
      :upload-data="{ examId: filter.examId }"
      :download-handle="() => downloadTemplate('objectiveStruct')"
      download-filename="客观题导入模板.xlsx"
      :format="['xls', 'xlsx']"
      @upload-success="uploadSuccess"
    >
    </import-file>
    <!-- data-task-dialog -->
    <data-task-dialog
      v-if="
        checkPrivilege('button', 'SubjectiveStructImport') ||
        checkPrivilege('button', 'ObjectiveStructImport')
      "
      ref="DataTaskDialog"
      :task-type="questionType"
    ></data-task-dialog>
  </div>
</template>

<script>
import { markSettingListPage } from "../api";
import ModifyMarkSetting from "../components/ModifyMarkSetting.vue";
import ModifyMarkParams from "../components/markParam/ModifyMarkParams.vue";
import ImportFile from "@/components/ImportFile.vue";
import templateDownload from "@/mixins/templateDownload";
import UploadButton from "@/components/UploadButton.vue";

export default {
  name: "mark-setting",
  components: { ModifyMarkSetting, ModifyMarkParams, ImportFile, UploadButton },
  mixins: [templateDownload],
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        openCollegeId: "",
        courseId: "",
        paperNumber: "",
        groupStatus: null,
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      curExam: {},
      multipleSelection: [],
      questionType: "",
      multipleInst: [],
    };
  },
  computed: {
    isModel4() {
      return this.curExam.examModel === "MODEL4";
    },
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      if (datas.groupStatus !== null && datas.groupStatus !== "")
        datas.groupStatus = !!datas.groupStatus;

      const data = await markSettingListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    examChange(val) {
      this.curExam = val;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    toSetParams(row) {
      this.curRow = row;
      this.$refs.ModifyMarkParams.open();
    },
    toModifySetting(row) {
      this.curRow = row;
      this.multipleInst = [];
      this.$refs.ModifyMarkSetting.open();
    },
    toBatchModifySetting() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择数据");
        return;
      }
      this.curRow = {};
      this.multipleInst = [...this.multipleSelection];
      this.$refs.ModifyMarkSetting.open();
    },
    toImportSubjective() {
      this.$refs.ImportSubjectiveFile.open();
    },
    toImportObjective() {
      this.$refs.ImportObjectiveFile.open();
    },
    uploadSuccess() {
      this.$message.success("文件上传成功，后台正在导入！");
      this.getList();
    },
    toDataTask(type) {
      this.questionType = type;
      this.$refs.DataTaskDialog.open();
    },
    // upload
    validError(errorData) {
      this.$message.error(errorData.message);
    },
    uploadJsonSuccess() {
      this.$message.success("上传成功！");
      this.getList();
    },
  },
};
</script>
