<template>
  <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-input
          v-model.trim="searchModel.groupNumber"
          placeholder="请输入分组"
          clearable
          style="width: 120px"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="状态">
        <select-option
          v-model="searchModel.status"
          type="'arbitrationType'"
          style="width: 120px"
        >
        </select-option>
      </el-form-item>
      <el-form-item label="选做题">
        <el-select
          v-model="searchModel.unselective"
          placeholder="请选择"
          clearable
          style="width: 120px"
        >
          <el-option label="未选做" :value="true" />
        </el-select>
      </el-form-item>
      <el-form-item label="准考证号">
        <el-input
          v-model.trim="searchModel.examNumber"
          placeholder="请输入准考证号"
          clearable
          style="width: 200px"
        >
        </el-input>
      </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="groupNumber"
        label="分组序号"
        width="120"
        sortable
      />
      <el-table-column prop="examNumber" label="准考证号" width="150" />
      <el-table-column prop="secretNumber" label="密号" width="150" />
      <el-table-column prop="status" label="状态" width="100" sortable>
        <template #default="scope">
          {{ scope.row.statusText }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180"
        sortable
      />
      <el-table-column prop="updateTime" label="处理时间" width="180" sortable>
        <template #default="scope">
          {{ scope.row.updateTime || '-' }}
        </template>
      </el-table-column>
      <el-table-column prop="arbitrateName" label="处理人" width="120">
        <template #default="scope">
          {{ scope.row.arbitrateName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" fixed="right">
        <template #default="scope">
          <el-button type="primary" 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';
  import { downloadExport } from '@/utils/download-export';

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

  const searchModel = reactive<MarkArbitrationListFilter>({
    subjectCode: null,
    groupNumber: '',
    status: '',
    unselective: undefined,
    examNumber: '',
  });

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

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

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

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

  // 导出功能
  async function onExport() {
    await downloadExport('exportArbitrationList', searchModel);
  }
</script>
