<template>
  <div class="report-college">
    <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 class="table 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>{{ item.name }}</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-college",
  components: { ReportBox },
  data() {
    return {
      pages: [],
      initOption,
    };
  },
  computed: {
    ...mapState("report", ["college"]),
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      const college = this.college.map((item) => {
        return {
          ...item,
          name: item.college,
        };
      });
      const maxChartCountPerPage = 4;
      const maxTableCountPerPage = 30;
      const groupNum = getGroupNum(college.length, 6);
      const collegeCharts = sectionArr(college, groupNum).map((data) =>
        getBarsOptions(data)
      );
      let pages = [];
      let curPage = { charts: [], tables: [] };
      collegeCharts.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)
      );
      console.log(firstPageTableCount);
      const collegeTables = sectionArrFirstPage(
        college,
        maxTableCountPerPage,
        firstPageTableCount
      );
      collegeTables.forEach((item) => {
        curPage.tables = item;
        pages.push(curPage);
        curPage = { charts: [], tables: [] };
      });
      this.pages = pages;
      console.log(pages);
    },
  },
};
</script>
