<template>
  <div class="part-box is-border">
    <div class="box-justify flex-start">
      <el-form inline>
        <el-form-item label="科目">
          <select-subject v-model="searchModel.subjectCode"></select-subject>
        </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-button @click="onExportSummary">导出总结表</el-button>
          </el-space>
        </el-form-item>
      </el-form>
      <div>
        <el-button @click="onRefresh">刷新</el-button>
      </div>
    </div>
    <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="subjectName" label="科目名称" min-width="120" />
      <el-table-column prop="totalCount" label="报考人数" min-width="100" />
      <el-table-column prop="absentCount" label="缺考人数" min-width="100" />
      <el-table-column prop="breachCount" label="违纪人数" min-width="100" />
      <el-table-column
        prop="realityCount"
        label="有效考试人数"
        min-width="120"
      />
      <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 { getTotalAnalysisList } from '@/api/analysis';
  import { TotalAnalysisItem, AnalysisListFilter } from '@/api/types/analysis';
  import useTable from '@/hooks/table';
  import { downloadExport } from '@/utils/download-export';

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

  const searchModel = reactive<AnalysisListFilter>({
    subjectCode: null,
  });

  const { dataList, pagination, loading, toPage, pageSizeChange } =
    useTable<TotalAnalysisItem>(getTotalAnalysisList, searchModel, true);

  async function onExport() {
    await downloadExport('exportTotalAnalysisList', searchModel);
  }

  async function onExportSummary() {
    await downloadExport('exportTotalAnalysisSummaryList', searchModel);
  }

  async function onRefresh() {
    // TODO: 刷新数据
  }
</script>
