<template>
  <div class="part-box is-border">
    <!-- 统计图表区域 -->
    <div class="chart-container">
      <div class="chart-item">
        <h3>复核总进度</h3>
        <Chart :options="taskChartOptions" width="400px" height="300px" />
      </div>
      <div class="chart-item">
        <h3>科目复核进度</h3>
        <Chart :options="courseChartOptions" width="400px" height="300px" />
      </div>
    </div>
  </div>

  <div class="part-box is-border">
    <el-form inline>
      <el-form-item label="科目">
        <select-subject v-model="searchModel.subjectCode"></select-subject>
      </el-form-item>
      <el-form-item label="选做科目">
        <el-select
          v-model="searchModel.selective"
          placeholder="请选择"
          clearable
          style="width: 120px"
        >
          <el-option label="是" :value="true" />
          <el-option label="否" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="完成进度">
        <el-select
          v-model="searchModel.finished"
          placeholder="请选择"
          clearable
          style="width: 120px"
        >
          <el-option label="已完成" :value="true" />
          <el-option label="未完成" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="复核次数">
        <el-input-number
          v-model="searchModel.inspectRound"
          :min="0"
          :max="99"
          :step="1"
          :precision="0"
          :controls="false"
          step-strictly
          placeholder="请输入"
          style="width: 120px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toPage(1)">查询</el-button>
        <el-button @click="onExport">导出</el-button>
      </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="subjectName" label="科目" min-width="200" />
      <el-table-column label="选做科目" min-width="110" sortable>
        <template #default="scope">
          <el-tag :type="scope.row.selectiveText ? 'success' : 'info'">
            {{ scope.row.selectiveText || '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="totalPaperCount"
        label="试卷总量"
        min-width="110"
        sortable
      />
      <el-table-column
        prop="totalCount"
        label="任务数"
        min-width="100"
        sortable
      />
      <el-table-column
        prop="inspectedCount"
        label="已复核数"
        width="110"
        sortable
      />
      <el-table-column
        prop="leftCount"
        label="待复核数"
        min-width="110"
        sortable
      />
      <el-table-column label="完成进度" min-width="110" sortable>
        <template #default="scope">
          <el-progress
            :percentage="scope.row.percent"
            :color="scope.row.percent === 100 ? '#67c23a' : '#409eff'"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="finishCount"
        label="已复核次数"
        min-width="120"
        sortable
      />
    </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, ref, computed, onMounted } from 'vue';
  import { getReviewStatInfo, getReviewStatList } from '@/api/review';
  import {
    ReviewStatInfo,
    ReviewStatItem,
    ReviewStatListFilter,
  } from '@/api/types/review';
  import useTable from '@/hooks/table';
  import { downloadExport } from '@/utils/download-export';

  import Chart from '@/components/chart/index.vue';

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

  const searchModel = reactive<ReviewStatListFilter>({
    subjectCode: undefined,
    selective: undefined,
    finished: undefined,
    inspectRound: undefined,
  });

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

  // 统计信息
  const statInfo = ref<ReviewStatInfo>({
    markedCount: 0,
    unMarkedCount: 0,
    finishCount: 0,
    unFinishCount: 0,
  });

  // 任务进度饼状图配置
  const taskChartOptions = computed(() => ({
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)',
    },
    legend: {
      orient: 'horizontal',
      bottom: '10%',
      data: ['任务待完成', '任务已完成'],
    },
    series: [
      {
        name: '任务进度',
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['50%', '40%'],
        data: [
          {
            value: statInfo.value.unMarkedCount,
            name: '任务待完成',
            itemStyle: { color: '#5470c6' },
          },
          {
            value: statInfo.value.markedCount,
            name: '任务已完成',
            itemStyle: { color: '#ff7875' },
          },
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    ],
  }));

  // 科目进度饼状图配置
  const courseChartOptions = computed(() => ({
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)',
    },
    legend: {
      orient: 'horizontal',
      bottom: '10%',
      data: ['科目待完成', '科目已完成'],
    },
    series: [
      {
        name: '科目进度',
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['50%', '40%'],
        data: [
          {
            value: statInfo.value.unFinishCount,
            name: '科目待完成',
            itemStyle: { color: '#5470c6' },
          },
          {
            value: statInfo.value.finishCount,
            name: '科目已完成',
            itemStyle: { color: '#ff7875' },
          },
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    ],
  }));

  // 获取统计信息
  async function getStatInfo() {
    try {
      const data = await getReviewStatInfo();
      statInfo.value = data;
    } catch (error) {
      console.error('获取统计信息失败:', error);
    }
  }

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

  onMounted(() => {
    getStatInfo();
  });
</script>

<style scoped lang="scss">
  .chart-container {
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 20px;
    gap: 40px;

    .chart-item {
      text-align: center;

      h3 {
        margin-bottom: 20px;
        color: #333;
        font-size: 16px;
        font-weight: 500;
      }
    }
  }
</style>
