<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.date) }}
              </template>
            </el-table-column>
            <el-table-column label="个人" align="center">
              <template v-slot="{ row }">
                {{ getpscore(row, i.date, i.subid) }}
              </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, default: () => [] },
    groupList: { type: Array, default: () => [] },
    list: { type: Array, default: () => [] },
    getPScore: { type: Array, default: () => [] },
    homeworkList: { type: Array, default: () => [] },
    groupscoreList: { type: Array, default: () => [] },
  },
  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, date) {
      let res = this.groupscoreList.find(f => f.date == date && f.groupid == data.groupid);
      return _.get(res, 'score', '--');
    },
    // 平时
    getpscore(data, date, subid) {
      let res = this.getPScore.find(f => f.date == date && f.studentid == data.stuid && f.subid === subid);
      return _.get(res, 'score', '--');
    },
    // 作业
    getHomework(data, lessonid) {
      let res = this.homeworkList.find(f => f.lessonid == lessonid && f.studentid == data.stuid);
      return _.get(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);
      // // 团队
      const groupscoreList = this.cleanSame(this.groupscoreList, 'groupid');
      let groupscore = groupscoreList.filter(f => f.groupid == data.groupid).reduce((p, n) => p + n['score'] * 1, 0);
      let groupLength = _.get(
        this.groupList.find(f => f.id == data.groupid),
        'students.length',
        0
      );
      let grSum = 0;
      if (groupLength > 0) grSum = groupscore / groupLength;
      // let grSum = groupscore.reduce((p, n) => p + ((n['score'] * 1) / groupLength.students.length || 0), 0);
      // if (this.getPScore.length > 0 && this.homeworkList.length > 0 && this.groupscoreList.length > 0 && data.stuname === '谷彦庆') {
      // console.group(data.stuname);
      // console.log(`个人:${psSum}`);
      // console.log(`作业:${hwSum}`);
      // let t = this.groupscoreList.filter(f => f.groupid == data.groupid);
      // console.log(t);
      // console.log(`团队:${grSum}`);
      // console.groupEnd(data.stuname);
      // }

      return _.round(hwSum + psSum + grSum, 2);
    },
    cleanSame(list, key) {
      console.log(list);
      return _.uniqBy(list, key);
    },
  },
};
</script>

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