<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="70px" @submit.native.prevent>
      <el-form-item label="学年" prop="classYear">
        <el-date-picker v-model="classYear" type="year" @change="handleQuery" value-format="yyyy"
                        placeholder="选择年度"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
        <!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
        <el-button
          class="ml20"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-form-item>
    </el-form>
    <el-table :data="classList" :span-method="arraySpanMethod" border :row-style="{ height: '30px' }"
              v-loading="loading" :cell-style="{ padding: '0' }" style="font-size: 14px" stripe>
      <el-table-column prop="gradeName" label="班级" width="240" align="center"></el-table-column>
      <el-table-column prop="className" label="班级" align="center"></el-table-column>
      <el-table-column prop="num" label="人数" align="center"></el-table-column>
      <el-table-column prop="boyNum" label="男生" align="center"></el-table-column>
      <el-table-column prop="girlNum" label="女生" align="center"></el-table-column>
      <el-table-column prop="picNum" label="有照片数" align="center"></el-table-column>
      <el-table-column prop="noPicNum" label="无照片" align="center"></el-table-column>
    </el-table>
  </div>
</template>
<script>
import {
  getClassList,
} from '@/api/smartSchool/logisticsManage/affairStatistics'

import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "BudgetApproval",
  data() {
    return {
      queryForm: {
        gradeValue: '',
        gradeName: '',
        classValue: '',
        className: '',
        num: '',
        boyNum: '',
        girlNum: '',
        picNum: '',
        noPicNum: '',
      },
      // 遮罩层
      loading: false,
      total: 0,
      // 信息列表
      classList: [],
      classYear: '2023',
    }
  },
  created() {
    this.getNowYear();
    this.getList();

  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true;
      getClassList(this.classYear).then(response => {
        this.loading = false;
        this.classList = response.rows;
        this.total = response.total;
      }).catch(() => {
        this.loading = false;
      })
    },

    // 搜索按钮
    handleQuery() {
      this.getList();
    },
    // 重置按钮
    resetQuery() {
      this.getNowYear();
      this.getList();
    },
    // 导出按钮
    handleExport() {
      this.throttle(() => {
        this.download('/statistics/exportClass/' + this.classYear, {
          ...this.queryForm
        }, `班级信息统计${new Date().getTime()}.xlsx`)
      })
    },
    //获得当前年份
    getNowYear() {
      this.classYear = new Date().getFullYear() + '';
    },
    //  合并单元格
    arraySpanMethod({ rowIndex, columnIndex}) {
      const spanArr = this.getSpanArr(this.classList);
      if (columnIndex == 0) {
        const _row = spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
    },
    // 合并相邻行之前存在相同项目的数据
    getSpanArr(data) {
      // data就是我们从后台拿到的数据
      let spanArr = [];
      let pos = 0;

      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          //设置table表格行号、设置合并参数，以便相同参数合并
          spanArr.push(1);
          pos = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].gradeName === data[i - 1].gradeName) {
            spanArr[pos] += 1;
            spanArr.push(0);
          } else {
            spanArr.push(1);
            pos = i;
          }
        }
      }
      return spanArr;
    },

  }
}
</script>

<style lang="scss" scoped>
::v-deep .inline-table .cell {
  padding: 0;
}
</style>
