<template>
  <div class="score-check">
    <div class="part-box part-box-filter part-box-flex">
      <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"
          ></secp-select>
        </template>
        <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="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'ObjectiveCalculate')"
          type="primary"
          :loading="loading"
          icon="el-icon-document-checked"
          :disabled="!multipleSelection.length"
          @click="toObjectiveCalculate"
          >客观题统分</el-button
        >
      </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="studentCount" label="参考人数" width="100">
        </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', 'Detail')"
              class="btn-primary"
              type="text"
              @click="toDetail(scope.row)"
              >查看详情</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'PackageDetail')"
              class="btn-primary"
              type="text"
              @click="toViewSign(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 class="tips-markedness">
        <p>
          可以点击查看详情按钮进行成绩检查，成绩检查完成后可以在评卷管理菜单结束评卷
          <el-button
            type="text"
            class="btn-primary btn-link ml-2"
            @click="toMarkManage"
            >点击跳转评卷管理</el-button
          >
        </p>
      </div>
    </div>

    <!-- ScoreCheckDetail -->
    <score-check-detail
      ref="ScoreCheckDetail"
      :instance="curRow"
    ></score-check-detail>
    <!-- ScoreCheckSign -->
    <score-check-sign
      ref="ScoreCheckSign"
      :instance="curRow"
    ></score-check-sign>
  </div>
</template>

<script>
import { scoreCheckListPage, objectiveScoreBatchCalculate } from "../api";
import ScoreCheckDetail from "../components/ScoreCheckDetail.vue";
import ScoreCheckSign from "../components/ScoreCheckSign.vue";

export default {
  name: "score-check",
  components: { ScoreCheckDetail, ScoreCheckSign },
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        openCollegeId: "",
        courseId: "",
        paperNumber: "",
        progressStatus: null,
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      multipleSelection: [],
      downloading: false,
      loading: false,
      // img view
      curImage: {},
      curImageIndex: 0,
      imageList: [],
    };
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

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

      const data = await scoreCheckListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    async toObjectiveCalculate() {
      if (this.loading) return;
      this.loading = true;

      const res = await objectiveScoreBatchCalculate({
        ids: this.multipleSelection.join(),
      }).catch(() => {});
      this.loading = false;

      if (!res) return;
      this.$message.success("操作成功！");
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => item.markPaperId);
    },
    toDetail(row) {
      this.curRow = row;
      this.$refs.ScoreCheckDetail.open();
    },
    toViewSign(row) {
      this.curRow = row;
      this.$refs.ScoreCheckSign.open();
    },
    toMarkManage() {
      this.$router.push({ name: "MarkManage" });
    },
  },
};
</script>
