<template>
  <div class="part-box is-border">
    <el-form inline>
      <el-form-item label="科目">
        <select-subject v-model="searchModel.subjectCode"></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="onExport">导出</el-button>
        </el-space>
      </el-form-item>
    </el-form>
    <el-divider class="form-divider" />

    <el-table
      class="page-table"
      :data="dataList"
      :loading="loading"
      border
      stripe
    >
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="className" label="班级" min-width="150" />
      <el-table-column prop="totalCount" label="报考人数" min-width="100" />
      <el-table-column prop="realityCount" label="有效人数" min-width="100" />
      <el-table-column prop="avgScore" label="平均分" min-width="100">
        <template #default="scope">
          {{ scope.row.avgScore?.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column prop="maxScore" label="最高分" min-width="100" />
      <el-table-column prop="minScore" label="最低分" min-width="100" />
      <el-table-column prop="passCount" 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 prop="excellentCount" 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';
  import { downloadExport } from '@/utils/download-export';

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

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

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

  async function onExport() {
    await downloadExport('exportClassAnalysisList', searchModel);
  }
</script>
