<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 label="状态">
        <el-select
          v-model="searchModel.status"
          placeholder="请选择状态"
          clearable
          style="width: 120px"
        >
          <el-option label="待处理" value="pending" />
          <el-option label="已处理" value="processed" />
          <el-option label="已关闭" value="closed" />
        </el-select>
      </el-form-item>
      <el-form-item label="准考证号">
        <el-input
          v-model.trim="searchModel.examCardNo"
          placeholder="请输入准考证号"
          clearable
          style="width: 150px"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="searchModel.optional"> 选做题 </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 @click="onBatchHandle">批量处理</el-button>
        <el-button @click="onExport">导出</el-button>
      </el-space>
    </div>
    <el-table
      class="page-table"
      :data="dataList"
      :loading="loading"
      border
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="subjectCode" label="科目代码" min-width="200" />
      <el-table-column prop="groupNo" label="分组序号" width="110" sortable />
      <el-table-column prop="examCardNo" label="准考证号" width="150" />
      <el-table-column label="状态" width="100" sortable>
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)" size="small">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180"
        sortable
      />
      <el-table-column prop="handleTime" label="处理时间" width="180" sortable>
        <template #default="scope">
          {{ scope.row.handleTime || '-' }}
        </template>
      </el-table-column>
      <el-table-column prop="handler" label="处理人" width="120">
        <template #default="scope">
          {{ scope.row.handler || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" fixed="right">
        <template #default="scope">
          <el-button
            type="primary"
            size="small"
            link
            @click="onHandle(scope.row)"
          >
            处理
          </el-button>
        </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 { ElMessage } from 'element-plus';
  import { getArbitrationList } from '@/api/mark';
  import {
    MarkArbitrationItem,
    MarkArbitrationListFilter,
  } from '@/api/types/mark';
  import useTable from '@/hooks/table';

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

  const searchModel = reactive<MarkArbitrationListFilter>({
    subject: null,
    group: '',
    status: '',
    optional: undefined,
    examCardNo: '',
  });

  const {
    dataList,
    pagination,
    loading,
    selectedRows,
    toPage,
    pageSizeChange,
    handleSelectionChange,
    handleSortChange,
  } = useTable<MarkArbitrationItem>(getArbitrationList, searchModel, false);

  // 获取状态类型
  function getStatusType(status: string) {
    const statusMap: Record<string, string> = {
      pending: 'warning',
      processed: 'success',
      closed: 'info',
    };
    return statusMap[status] || 'info';
  }

  // 获取状态文本
  function getStatusText(status: string) {
    const statusMap: Record<string, string> = {
      pending: '待处理',
      processed: '已处理',
      closed: '已关闭',
    };
    return statusMap[status] || status;
  }

  // 处理仲裁
  function onHandle(row: MarkArbitrationItem) {
    ElMessage.info(`处理仲裁：${row.examCardNo}`);
    // TODO: 实现处理仲裁的逻辑
  }

  function onBatchHandle() {
    if (!selectedRows.value.length) {
      ElMessage.warning('请选择数据');
      return;
    }

    ElMessage.info(
      `批量处理仲裁：${selectedRows.map((row) => row.examCardNo).join(',')}`
    );
    // TODO: 实现批量处理仲裁的逻辑
  }

  function onExport() {
    // TODO: 导出仲裁
    ElMessage.info('导出仲裁');
  }
</script>
