<template>
  <div class="score-archive">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" 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="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>

      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'aggregateAnalysis')"
          type="primary"
          :loading="downloading"
          icon="el-icon-document"
          @click="exportTotalAnalysis"
          >下载总量分析</el-button
        >
        <el-button
          v-if="checkPrivilege('button', 'ObjectiveCalculate')"
          type="primary"
          :loading="downloading"
          icon="el-icon-document"
          @click="batchAnalysisReport"
          >批量下载分析报告</el-button
        >
      </div>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="name" label="课程名称（代码）" min-width="260">
          <span slot-scope="scope"
            >{{ scope.row.courseName }}（{{ scope.row.courseCode }}）</span
          >
        </el-table-column>
        <el-table-column
          prop="openCollege"
          label="开课学院"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="paperNumber"
          label="试卷编号"
          width="200"
        ></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
          prop="classCount"
          label="参考班级"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="absentCount"
          label="缺考人数"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="avgScore"
          label="平均分"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="maxScore"
          label="最高分"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="minScore"
          label="最低分"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="passCount"
          label="及格人数"
          width="100"
        ></el-table-column>
        <el-table-column prop="passRate" label="及格率" width="100">
          <template slot-scope="scope">{{ scope.row.passRate || 0 }}%</template>
        </el-table-column>
        <el-table-column
          prop="excellentCount"
          label="优秀人数"
          width="100"
        ></el-table-column>
        <el-table-column prop="excellentRate" label="优秀率" width="100">
          <template slot-scope="scope"
            >{{ scope.row.excellentRate || 0 }}%</template
          >
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="100px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'ClassScoreDetail')"
              class="btn-primary"
              type="text"
              @click="toClass(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>

    <!-- ScoreClassDetail -->
    <score-class-detail
      ref="ScoreClassDetail"
      :instance="curRow"
    ></score-class-detail>
  </div>
</template>

<script>
import {
  scoreListPage,
  scoreTotalAnalysisDownload,
  scoreAllAnalysisReportDownload,
} from "../api";
import ScoreClassDetail from "../components/ScoreClassDetail.vue";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "score-manage",
  components: { ScoreClassDetail },
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        courseId: "",
        openCollegeId: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      downloading: false,
    };
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await scoreListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    toClass(row) {
      this.curRow = row;
      this.$refs.ScoreClassDetail.open();
    },
    toMarkManage() {
      this.$router.push({ name: "MarkManage" });
    },
    async exportTotalAnalysis() {
      if (this.downloading) return;
      this.downloading = true;

      const res = await downloadByApi(() => {
        return scoreTotalAnalysisDownload(this.filter);
      }).catch((e) => {
        this.$message.error(e || "下载失败，请重新尝试！");
      });
      this.downloading = false;

      if (!res) return;
      this.$message.success("下载成功！");
    },
    async batchAnalysisReport() {
      if (this.downloading) return;
      this.downloading = true;

      const res = await downloadByApi(() => {
        return scoreAllAnalysisReportDownload(this.filter);
      }).catch((e) => {
        this.$message.error(e || "下载失败，请重新尝试！");
      });
      this.downloading = false;

      if (!res) return;
      this.$message.success("下载成功！");
    },
  },
};
</script>
