<template>
  <div class="part-box is-border">
    <el-form inline>
      <el-form-item label="科目">
        <select-subject v-model="searchModel.code"></select-subject>
      </el-form-item>
      <el-form-item label="层次">
        <select-option v-model="searchModel.level" type="subjectLevel">
        </select-option>
      </el-form-item>
      <el-form-item label="专业类型">
        <select-option v-model="searchModel.category" type="subjectCategory">
        </select-option>
      </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
      @sort-change="handleSortChange"
    >
      <el-table-column prop="name" label="考试名称" min-width="200" sortable />
      <el-table-column
        prop="totalCount"
        label="考生总数"
        min-width="100"
        sortable
      />
      <el-table-column
        prop="scanSheetCount"
        label="已扫张数"
        min-width="100"
        sortable
      />
      <el-table-column
        prop="scanCount"
        label="已扫人数"
        min-width="100"
        sortable
      />
      <el-table-column
        prop="manualAbsentCount"
        label="人工指定缺考"
        min-width="100"
        sortable
      />
      <el-table-column label="进度" width="100">
        <template #default="scope">
          {{ getProgress(scope.row) }}
        </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 { getScanCourseList } from '@/api/scan';
  import { ScanItem, ScanCourseListFilter } from '@/api/types/scan';
  import useTable from '@/hooks/table';
  import { downloadExport } from '@/utils/download-export';

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

  const searchModel = reactive<ScanCourseListFilter>({
    code: null,
    level: '',
    category: '',
  });

  const {
    dataList,
    pagination,
    loading,
    toPage,
    pageSizeChange,
    handleSortChange,
  } = useTable<ScanItem>(getScanCourseList, searchModel, false);

  function getProgress(row: ScanItem) {
    if (!row.totalCount) return '0%';
    return `${(
      ((row.scanCount + row.manualAbsentCount) / row.totalCount) *
      100
    ).toFixed(2)}%`;
  }

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