<template>
  <div class="app-container">
    <evaluation-title
      :title="`${evaluationName}${mxGlobal.EvalTitleSeperator}${mxGlobal.TailingClassName(className)}${studentName}数据统计`"
      :navBackButton="true"
    ></evaluation-title>
    <el-table
      v-loading="loading"
      :data="dataList"
      :row-class-name="rowClassName"
      :span-method="spanMethod"
      style="width: 100%"
    >
      <el-table-column label="序号" type="index" align="center" width="100" />
      <el-table-column label="姓名" prop="customerName" align="center" />
      <el-table-column label="名次" prop="ranking" align="center" />
      <el-table-column label="分数" prop="score" align="center" />
      <el-table-column label="得分率" prop="scoreRate" align="center" />
      <el-table-column label="选择题正确率" align="center">
        <template slot-scope="scope">
          <span
            >{{ dataList[scope.$index].chooseCount }}/{{
              dataList[scope.$index].chooseTotal
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="主观题正确率" align="center">
        <template slot-scope="scope">
          <span
            >{{ dataList[scope.$index].subjectiveCount }}/{{
              dataList[scope.$index].subjectiveTotal
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-view"
            @click="detail(scope.$index)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getClassExaminees } from "@/api/webApi/front";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      queryParams: {},
      evaluationName: "",
      className: "",
      studentName: "",
      customerCode: "",
      loading: false,
      dataList: [],
      // 置顶用户数据
      matchingEnable: false,
      matchingCode: "",
      matchingName: "",
      mathcingDoFake: true,
    };
  },
  computed: {
    ...mapGetters(["isFrontStudent", "currentUser"]),
  },
  created() {
    this.queryParams = this.$route.query;
    this.evaluationName = this.queryParams.evaluationName || "";
    // 可能从学生管理跳转过来会携带studentName & customerCode
    this.className = this.queryParams.className || "";
    this.studentName = this.queryParams.studentName || "";
    this.customerCode = this.queryParams.customerCode || "";
    delete this.queryParams.evaluationName;
    delete this.queryParams.studentName;
    delete this.queryParams.customerCode;
    delete this.queryParams.className;
    this.initMatchingData();
    this.handleQuery();
  },
  methods: {
    initMatchingData() {
      // 初始化置顶数据条件
      // 学生身份进入此页, 或者老师身份选中学生进入时
      this.matchingEnable = this.isFrontStudent || this.customerCode;
      this.matchingCode =
        (this.isFrontStudent ? this.currentUser.code : this.customerCode) || "";
      this.matchingName =
        (this.isFrontStudent ? this.currentUser.nickName : this.studentName) ||
        "";
    },
    popupMatchingRowByNeed() {
      if (!this.matchingEnable) return;
      var matchingIdx = this.dataList.findIndex(this.isRowMatching);
      var row = matchingIdx > -1 ? this.dataList[matchingIdx] : null;
      if (!row && this.mathcingDoFake) {
        row = {
          customerCode: this.matchingCode,
          customerName: this.matchingName,
          ranking: "该生无记录",
          isFakeRow: true,
        };
      }
      if (matchingIdx > -1) this.dataList.splice(matchingIdx, 1);
      if (row) this.dataList = [row, ...this.dataList];
    },
    rowClassName({ row, rowIndex }) {
      if (this.matchingEnable && this.isRowMatching(row))
        return "highlight-row";
      return "";
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      if (!row.isFakeRow) return [1, 1];
      if (columnIndex < 2) return [1, 1];
      if (columnIndex == 2) return [1, 6];
      return [0, 0];
    },
    isRowMatching(row) {
      return row.customerCode == this.matchingCode;
    },
    detail(idx) {
      const row = this.dataList[idx];
      const nextParams = {
        evaluationName: this.evaluationName,
        examineeId: row.examineeId || "",
        studentName: row.customerName,
      };
      this.$router.push({
        path: "/idc/EvaluationDatabase/statistics",
        query: nextParams,
      });
    },
    handleQuery() {
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true;
      getClassExaminees(this.queryParams).then((rep) => {
        console.log("getClassExaminees rep =", rep);
        if (rep.code == 200 || rep.code == 0) {
          this.dataList = rep.data;
          this.popupMatchingRowByNeed();
        } else {
          this.msgError(rep.msg || "getClassExaminees请求异常");
        }
        this.loading = false;
      });
    },
  },
};
</script>

<style scoped>
</style>
