<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>我的已办</span>
        </div>
      </template>

      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="流程名称">
          <el-input v-model="queryParams.processName" placeholder="请输入流程名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="tableData" border>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="definitionName" label="流程名称" />
        <el-table-column label="处理节点" show-overflow-tooltip>
          <template #default="{ row }">
            {{ row.subNodeName ? row.nodeName + ' / ' + row.subNodeName : row.nodeName }}
          </template>
        </el-table-column>
        <el-table-column prop="action" label="操作结果" width="100">
          <template #default="{ row }">
            <el-tag :type="actionTagType(row.action)">
              {{ actionText(row.action) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="comment" label="审批意见" show-overflow-tooltip />
        <el-table-column prop="endTime" label="处理时间" />
      </el-table>

      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        class="pagination"
        @current-change="loadData"
        @size-change="loadData"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { listHandled } from '@/api/flow/task'
import type { FlowTask } from '@/types/flow'
import { ElMessage } from 'element-plus'

const loading = ref(false)
const tableData = ref<FlowTask[]>([])
const total = ref(0)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  processName: undefined as string | undefined
})

function actionText(action?: string) {
  const key = (action || '').toUpperCase()
  const map: Record<string, string> = {
    PASS: '通过',
    REJECT: '拒绝',
    RETURN: '回退',
    ROLLBACK: '回退',
    TRANSFER: '转办',
    ADD_SIGN: '加签'
  }
  return map[key] || action || '-'
}

function actionTagType(action?: string) {
  const key = (action || '').toUpperCase()
  const map: Record<string, any> = {
    PASS: 'success',
    REJECT: 'danger',
    RETURN: 'warning',
    ROLLBACK: 'warning',
    TRANSFER: 'info',
    ADD_SIGN: 'info'
  }
  return map[key] || 'info'
}

async function loadData() {
  loading.value = true
  try {
    const res = await listHandled(queryParams)
    tableData.value = res.list
    total.value = res.total
  } catch {
    ElMessage.error('加载已办任务失败')
  } finally {
    loading.value = false
  }
}

function handleQuery() {
  queryParams.pageNum = 1
  loadData()
}

function resetQuery() {
  queryParams.processName = undefined
  queryParams.pageNum = 1
  loadData()
}

onMounted(loadData)
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
.search-form {
  margin-bottom: 20px;
}
</style>
