<template>
  <div class="part-box is-border">
    <el-form inline>
      <el-form-item label="考点">
        <select-option v-model="searchModel.examSite" type="examSite">
        </select-option>
      </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
      @sort-change="handleSortChange"
    >
      <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 { getScanPointList } from '@/api/scan';
  import { ScanItem, ScanPointListFilter } from '@/api/types/scan';
  import useTable from '@/hooks/table';
  import { downloadExport } from '@/utils/download-export';

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

  const searchModel = reactive<ScanPointListFilter>({
    examSite: '',
  });

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

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

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