<template>
  <div id="leagueData">
    <el-row>
      <el-col :span="24">
        <el-table :data="list" border :span-method="objectSpanMethod">
          <el-table-column label="小组" prop="groupname" align="center"> </el-table-column>
          <el-table-column label="姓名" prop="stuname" align="center"> </el-table-column>
          <el-table-column v-for="(i, index) in lessonList" :key="index" :label="i.date" :prop="i._id" align="center">
            <el-table-column label="团队" align="center" prop="team">
              <template v-slot="{ row }">
                {{ getGroupscore(row, i.subid) }}
              </template>
            </el-table-column>
            <el-table-column label="个人" align="center">
              <template v-slot="{ row }">
                {{ getpscore(row, i._id) }}
              </template>
            </el-table-column>
            <el-table-column label="作业" align="center">
              <template v-slot="{ row }">
                {{ getHomework(row, i._id) }}
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="总分" prop="totalScore" align="center">
            <template v-slot="{ row }">{{ calcTotal(row) }}</template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash';
import { mapState, createNamespacedHelpers } from 'vuex';
export default {
  name: 'leagueData',
  props: {
    lessonList: { type: Array },
    groupList: { type: Array },
    list: { type: Array },
    getPScore: { type: Array },
    homeworkList: { type: Array },
    groupscoreList: { type: Array },
  },
  components: {},
  data: function() {
    return {};
  },
  created() {},
  computed: { ...mapState(['user', 'defaultOption']) },
  methods: {
    // 合并
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      let prop = _.get(column, 'property');
      if (prop == 'groupname' || prop == 'team') {
        let data = _.flatten(_.toPairs(_.groupBy(this.list, 'groupid'))).filter(_.isArray);
        let num = data.find(i => i.every(e => e.groupid === row.groupid));
        let head = _.head(num);
        if (_.isEqual(head, row)) {
          return {
            rowspan: num.length,
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
    // 团队
    getGroupscore(data, prop) {
      let res = this.groupscoreList.find(f => f.subid == prop && f.groupid == data.groupid);
      if (res) return res.score;
    },
    // 平时
    getpscore(data, prop) {
      let lesson = this.lessonList.find(f => f._id == prop);
      let res = this.getPScore.find(f => f.date == lesson.date && f.studentid == data.stuid);
      if (res) return res.score;
    },
    // 作业
    getHomework(data, prop) {
      let res = this.homeworkList.find(f => f.lessonid == prop && f.studentid == data.stuid);
      if (res) return res.score;
    },
    // 计算总分
    calcTotal(data) {
      // 个人
      let getPScore = this.getPScore.filter(f => f.studentid == data.stuid);
      let psSum = getPScore.reduce((p, n) => p + (n['score'] * 1 || 0), 0);
      // 作业
      let homework = this.homeworkList.filter(f => f.studentid == data.stuid);
      let hwSum = homework.reduce((p, n) => p + (n['score'] * 1 || 0), 0);
      // // 团队
      let groupscore = this.groupscoreList.filter(f => f.groupid == data.groupid);
      let groupLength = this.groupList.find(f => f.id == data.groupid);
      let grSum = groupscore.reduce((p, n) => p + ((n['score'] * 1) / groupLength.students.length || 0), 0);
      return hwSum + psSum + grSum;
    },
  },
};
</script>

<style lang="less" scoped></style>
