<template>
  <div class="part-box is-border">
    <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.college"
          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>
    <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 property="college" 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 { getCollegeAnalysisList } from '@/api/analysis';
  import {
    CollegeAnalysisItem,
    CollegeAnalysisFilter,
  } from '@/api/types/analysis';
  import useTable from '@/hooks/table';

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

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

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

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