<template>
  <div class="report-class">
    <report-box
      v-for="(page, pindex) in pages"
      :key="pindex"
      title="班级成绩分析"
    >
      <div
        v-for="(chart, index) in page.charts"
        :key="`chart-${index}`"
        class="college-chart"
      >
        <v-chart :option="chart" :init-options="initOption"></v-chart>
      </div>
      <table v-if="page.tables.length" class="report-table report-table-border">
        <tr>
          <th>班级</th>
          <th>平均分</th>
          <th>最高分</th>
          <th>最低分</th>
          <th>及格数</th>
          <th>及格率(%)</th>
          <th>优秀数</th>
          <th>优秀率(%)</th>
        </tr>
        <tr v-for="(item, ind) in page.tables" :key="ind">
          <td class="td-class">
            <div>{{ item.name }}</div>
          </td>
          <td>{{ item.avgScore }}</td>
          <td>{{ item.maxScore }}</td>
          <td>{{ item.minScore }}</td>
          <td>{{ item.passCount }}</td>
          <td>{{ item.passRate }}</td>
          <td>{{ item.excellentCount }}</td>
          <td>{{ item.excellentRate }}</td>
        </tr>
      </table>
    </report-box>
  </div>
</template>

<script>
import { mapState } from "vuex";
import ReportBox from "./ReportBox.vue";
import { sectionArr, getGroupNum, sectionArrFirstPage } from "./utils";
import { getBarsOptions, initOption } from "./chart";

export default {
  name: "report-class",
  components: { ReportBox },
  data() {
    return {
      pages: [],
      initOption,
    };
  },
  computed: {
    ...mapState("report", ["classData"]),
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      const classes = this.classData.map((item) => {
        return {
          ...item,
          name: item.className,
        };
      });
      const maxChartCountPerPage = 4;
      const maxTableCountPerPage = 30;
      const groupNum = getGroupNum(classes.length, 6);
      const groupCharts = sectionArr(classes, groupNum).map((data) =>
        getBarsOptions(data)
      );
      let pages = [];
      let curPage = { charts: [], tables: [] };
      groupCharts.forEach((item) => {
        curPage.charts.push(item);
        if (curPage.charts.length === maxChartCountPerPage) {
          pages.push(curPage);
          curPage = { charts: [], tables: [] };
        }
      });
      const firstPageTableCount = Math.floor(
        (maxChartCountPerPage - curPage.charts.length) *
          (maxTableCountPerPage / maxChartCountPerPage)
      );
      const groupTables = sectionArrFirstPage(
        classes,
        maxTableCountPerPage,
        firstPageTableCount
      );
      groupTables.forEach((item) => {
        curPage.tables = item;
        pages.push(curPage);
        curPage = { charts: [], tables: [] };
      });
      this.pages = pages;
    },
  },
};
</script>
