<template>
  <div class="part-box is-filter">
    <el-form inline>
      <el-form-item label="科目">
        <select-subject v-model="searchModel.subject"></select-subject>
      </el-form-item>
      <el-form-item label="班级">
        <el-input
          v-model="searchModel.className"
          placeholder="请输入班级名称"
          clearable
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item>
        <el-space wrap>
          <el-button type="primary" @click="toPage(1)">查询</el-button>
          <el-button @click="exportData">导出</el-button>
        </el-space>
      </el-form-item>
    </el-form>
  </div>
  <div class="part-box">
    <el-table class="page-table" :data="dataList" :loading="loading">
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column property="className" label="班级" min-width="150" />
      <el-table-column
        property="totalStudents"
        label="报考人数"
        min-width="100"
      />
      <el-table-column
        property="validStudents"
        label="有效人数"
        min-width="100"
      />
      <el-table-column property="averageScore" label="平均分" min-width="100">
        <template #default="scope">
          {{ scope.row.averageScore?.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column property="highestScore" label="最高分" min-width="100" />
      <el-table-column property="lowestScore" label="最低分" min-width="100" />
      <el-table-column
        property="passStudents"
        label="及格人数"
        min-width="100"
      />
      <el-table-column label="及格率" min-width="100">
        <template #default="scope">
          {{ (scope.row.passRate * 100).toFixed(2) }}%
        </template>
      </el-table-column>
      <el-table-column
        property="excellentStudents"
        label="优秀人数"
        min-width="100"
      />
      <el-table-column label="优秀率" min-width="100">
        <template #default="scope">
          {{ (scope.row.excellentRate * 100).toFixed(2) }}%
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pagination.pageNumber"
      v-model:page-size="pagination.pageSize"
      :layout="pagination.layout"
      :total="pagination.total"
      @size-change="pageSizeChange"
      @current-change="toPage"
    />
  </div>
</template>

<script setup lang="ts">
  import { reactive } from 'vue';
  import { getClassAnalysisList } from '@/api/analysis';
  import { ClassAnalysisItem, ClassAnalysisFilter } from '@/api/types/analysis';
  import useTable from '@/hooks/table';

  defineOptions({
    name: 'ClassAnalysis',
  });

  const searchModel = reactive<ClassAnalysisFilter>({
    subject: null,
    className: '',
  });

  const { dataList, pagination, loading, toPage, pageSizeChange } =
    useTable<ClassAnalysisItem>(getClassAnalysisList, searchModel, false);

  function exportData() {
    // TODO: 实现导出功能
    console.log('导出班级分析数据');
  }
</script>
