<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.trim="searchModel.group"
          placeholder="请输入分组"
          clearable
          style="width: 120px"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="searchModel.marked"> 已评卷 </el-checkbox>
        <el-checkbox v-model="searchModel.noArbitration">
          不含仲裁
        </el-checkbox>
      </el-form-item>
    </el-form>
    <el-divider class="form-divider" />
    <div class="part-action">
      <el-space wrap>
        <el-button type="primary" @click="toPage(1)">查询</el-button>
        <el-button :loading="recalLoading" @click="onRecalculate"
          >重新计算</el-button
        >
        <el-button @click="onViewScoreCurve">给分曲线</el-button>
      </el-space>
    </div>

    <el-table
      class="page-table"
      :data="dataList"
      :loading="loading"
      border
      stripe
      @sort-change="handleSortChange"
    >
      <el-table-column property="group" label="分组" width="80" sortable />
      <el-table-column property="markerId" label="评卷员" min-width="120" />
      <el-table-column property="name" label="姓名" min-width="100" />
      <el-table-column
        property="completedTasks"
        label="完成任务数"
        width="120"
        sortable
      />
      <el-table-column
        property="arbitrationTasks"
        label="仲裁任务数"
        width="120"
        sortable
      />
      <el-table-column label="仲裁率" width="100" sortable>
        <template #default="scope">
          {{ (scope.row.arbitrationRate * 100).toFixed(1) }}%
        </template>
      </el-table-column>
      <el-table-column
        property="returnCount"
        label="打回次数"
        width="110"
        sortable
      />
      <el-table-column label="评卷采用率" width="120" sortable>
        <template #default="scope">
          {{ (scope.row.adoptionRate * 100).toFixed(1) }}%
        </template>
      </el-table-column>
      <el-table-column
        property="markingSpeed"
        label="评卷速度(秒)"
        width="130"
        sortable
      />
      <el-table-column
        property="averageScore"
        label="平均分"
        width="100"
        sortable
      >
        <template #default="scope">
          {{ scope.row.averageScore.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column
        property="standardDeviation"
        label="标准差"
        width="100"
        sortable
      >
        <template #default="scope">
          {{ scope.row.standardDeviation.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 { useRouter } from 'vue-router';
  import { ElMessage } from 'element-plus';
  import { getQualityMonitorList, qualityMonitorCalculate } from '@/api/mark';
  import {
    MarkQualityMonitorItem,
    MarkQualityMonitorListFilter,
  } from '@/api/types/mark';
  import useTable from '@/hooks/table';
  import useLoading from '@/hooks/loading';

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

  const router = useRouter();

  const searchModel = reactive<MarkQualityMonitorListFilter>({
    subject: null,
    group: '',
    marked: undefined,
    noArbitration: undefined,
  });

  const {
    dataList,
    pagination,
    loading,
    getList,
    toPage,
    pageSizeChange,
    handleSortChange,
  } = useTable<MarkQualityMonitorItem>(
    getQualityMonitorList,
    searchModel,
    false
  );

  // 重新计算
  const { loading: recalLoading, setLoading: setrecalLoading } = useLoading();
  async function onRecalculate() {
    try {
      setrecalLoading(true);
      await qualityMonitorCalculate({ subject: searchModel.subject });
      ElMessage.success('重新计算成功');
      getList();
    } catch (error) {
      console.error('重新计算失败:', error);
    } finally {
      setrecalLoading(false);
    }
  }

  function onViewScoreCurve() {
    // 跳转到给分曲线页面，传递当前的筛选条件
    router.push({
      path: '/quality-monitor/score-curve',
      query: {
        subject: searchModel.subject?.toString() || '',
        group: searchModel.group || '',
      },
    });
  }
</script>
