<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-select
          v-model="searchModel.intervalType"
          placeholder="请选择"
          clearable
          style="width: 150px"
        >
          <el-option label="5分间隔" value="5" />
          <el-option label="10分间隔" value="10" />
          <el-option label="20分间隔" value="20" />
        </el-select>
      </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="segment" label="分数段" min-width="120" />
      <el-table-column property="students" label="人数" min-width="100" />
      <el-table-column label="频率" min-width="100">
        <template #default="scope"> {{ scope.row.rate.toFixed(2) }}% </template>
      </el-table-column>
      <el-table-column label="分布图" min-width="200">
        <template #default="scope">
          <el-progress
            :percentage="scope.row.rate"
            :color="getSegmentColor(scope.row.rate)"
            :show-text="false"
          />
        </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 { getSegmentAnalysisList } from '@/api/analysis';
  import { ScoreSegmentItem, ScoreSegmentFilter } from '@/api/types/analysis';
  import useTable from '@/hooks/table';

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

  const searchModel = reactive<ScoreSegmentFilter>({
    subject: null,
    intervalType: '10',
  });

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

  function getSegmentColor(rate: number) {
    if (rate < 5) return '#f56c6c';
    if (rate < 15) return '#e6a23c';
    if (rate < 25) return '#409eff';
    return '#67c23a';
  }

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