<template>
  <el-table :data="resultData" border style="width: 100%">
    <el-table-column prop="studentCode" label="学号" align="center"></el-table-column>
    <el-table-column prop="studentName" label="姓名" align="center"></el-table-column>
    <template v-if="types == 1">
      <el-table-column prop="answerTimes" label="答题次数" align="center"></el-table-column>
      <el-table-column prop="consumingDuration" label="答题耗时" align="center"><template slot-scope="scoped">{{
        setDuration(scoped.row.consumingDuration)
      }}</template></el-table-column>
      <el-table-column prop="correctAnswerTimes" label="答对次数" align="center"></el-table-column>
      <el-table-column prop="participationRate" label="参与度" sortable align="center"><template slot-scope="scoped">{{
        scoped.row.participationRate }}%</template></el-table-column>
      <el-table-column prop="correctRate" label="正确率" sortable align="center"><template slot-scope="scoped">{{
        scoped.row.correctRate }}%</template></el-table-column>
      <el-table-column prop="answerCorrectRate" label="已答正确率" sortable align="center"><template slot-scope="scoped">{{
        scoped.row.answerCorrectRate }}%</template></el-table-column>
      <el-table-column v-for="(item, index) in optionsList" :key="index" :label="'Q' + (index + 1)"
        align="center"><template slot-scope="scoped">
          <span :class="scoped.row['isRight' + index] ? '' : 'red'">{{
            scoped.row["answer" + index]
          }}</span>
        </template>
      </el-table-column>
    </template>
    <template v-if="types == 2">
      <el-table-column prop="answerTimes" label="累计答题次数" align="center"></el-table-column>
      <el-table-column prop="correctAnswerTimes" label="累计答对次数" align="center"></el-table-column>
      <el-table-column prop="participationRate" label="总参与度" align="center"><template slot-scope="scoped">{{
        scoped.row.participationRate }}%</template></el-table-column>

      <el-table-column prop="participationRateRank" label="总参与度班名" align="center"></el-table-column>
      <el-table-column prop="correctRate" label="总正确率" align="center"><template slot-scope="scoped">{{
        scoped.row.correctRate }}%</template></el-table-column>
      <el-table-column prop="correctRateRank" label="总正确率班名" sortable align="center"></el-table-column>
      <el-table-column prop="answerCorrectRate" label="已答正确率" align="center"><template slot-scope="scoped">{{
        scoped.row.answerCorrectRate }}%</template></el-table-column>
      <el-table-column label="查看折线图" align="center">
        <template slot-scope="scoped"><el-button icon="el-icon-right" type="primary" circle></el-button></template>
      </el-table-column>
    </template>
    <template v-if="types == 3">
      <el-table-column v-for="(item, index) in phaseOption" :key="index" :label="item" align="center">
        <el-table-column align="center" :label="index == 0 ? '总课时数' : '课时数'" :prop="'periodCount' + item">
        </el-table-column>
        <el-table-column align="center" :label="index == 0 ? '总出题数' : '出题数'" :prop="'questionNum' + item">
        </el-table-column>
        <el-table-column align="center" :label="index == 0 ? '总参与度' : '参与度'" :prop="'participationRate' + item"><template
            slot-scope="scoped">{{ scoped.row["participationRate" + item] }}%</template>
        </el-table-column>
        <el-table-column align="center" :label="index == 0 ? '总正确率' : '正确率'" :prop="'correctRate' + item"><template
            slot-scope="scoped">{{ scoped.row["correctRate" + item] }}%</template>
        </el-table-column>
      </el-table-column>
    </template>
  </el-table>
</template>
<script>
export default {
  props: {
    tableData: Array,
    types: Number,
  },
  data() {
    return {
      optionsList: [],
      phaseOption: [], //问答补充数据
    };
  },
  computed: {
    resultData: function () {
      let resultData = []
      if (this.tableData.length) {
        let optionsList = [];
        let subjectName = [];
        let rank = {}
        resultData = this.tableData.map((item) => {
          let params = {};

          if (this.types == 1) {
            const detail = item.detail ? JSON.parse(item.detail) : [];
            if (detail.length > optionsList.length) {
              optionsList = [...detail];
            }
            detail.map((items, index) => {
              params["isRight" + index] = items.isRight;
              params["answer" + index] =
                items.answer == 1
                  ? "✓"
                  : items.answer == 2
                    ? "✗"
                    : items.answer;
            });
          } else if (this.types == 3) {
            item.dataList.map((items, index) => {
              if (!subjectName.includes(items.subjectName)) {
                subjectName.push(items.subjectName);
              }
              params["answerCorrectRate" + items.subjectName] =
                items.answerCorrectRate;
              params["correctRate" + items.subjectName] = items.correctRate;
              params["participationRate" + items.subjectName] =
                items.participationRate;
              params["periodCount" + items.subjectName] = items.periodCount;
              params["questionNum" + items.subjectName] = items.questionNum;
            });
          }
          if (this.types != 3) {
            let participationRateArr = []
            let correctRateArr = []
            this.tableData.map(item => {
              participationRateArr.push(item.participationRate)
              correctRateArr.push(item.correctRate)
            })
            participationRateArr = [...new Set(participationRateArr)]
            participationRateArr = participationRateArr.sort((a, b) => {
              return b - a
            })
            correctRateArr = [...new Set(correctRateArr)]
            correctRateArr = correctRateArr.sort((a, b) => {
              return b - a
            })
            let participationRateRank = participationRateArr.findIndex(value => {
              return item.participationRate == value
            })
            let correctRateRank = correctRateArr.findIndex(value => {
              return item.correctRate == value
            })
            rank = {
              participationRateRank: participationRateRank + 1,
              correctRateRank: correctRateRank + 1
            }
          }
          return {
            ...item,
            ...params,
            ...rank
          };
        });
        this.phaseOption = [...subjectName];
        this.optionsList = [...optionsList];
      } else {
        resultData = []
        this.optionsList = []
      }
      return resultData
    }
  },
  methods: {
    setDuration(times) {
      if (times) {
        let m = parseInt(times / 1000 / 60);
        let s = parseInt((times / 1000) % 60);
        let ms = times;
        let aTime;
        if (times == 0) {
          aTime = `0`;
        } else {
          if (m == 0 && s == 0) {
            aTime = `${ms}毫秒`;
          } else if (m == 0 && s != 0) {
            aTime = `${s}秒`;
          } else if (m != 0 && s != 0) {
            aTime = `${m}分${s}秒`;
          }
        }
        return aTime;
      }
    },
  }
};
</script>
<style>
.red {
  color: #f30;
}
</style>
