<script setup lang="ts">
import { toZhAuditNode } from "../../../utils/status";

defineProps<{
  rows: any[];
  canApprove: boolean;
  canReject: boolean;
  canReadMaterial: boolean;
  submittingTaskId: number | null;
  getCurrentUserId: () => number | null;
  meetingNameFormatter: (row: any, column?: any, cellValue?: unknown) => string;
  auditNodeFormatter: (row: any, column: any, value: unknown) => string;
  assigneeNameFormatter: (row: any, column?: any, cellValue?: unknown) => string;
  statusFormatter: (row: any, column: any, value: unknown) => string;
  resolveFlowNodes: (row: any) => any[];
  flowNodeTagType: (node: any, row: any) => "success" | "warning" | "info";
}>();

const emit = defineEmits<{
  selectionChange: [items: any[]];
  submitAudit: [taskId: number];
  openAuditProgress: [row: any];
  openMaterial: [row: any];
}>();
</script>

<template>
  <el-table :data="rows" class="mt-md" empty-text="暂无数据" @selection-change="emit('selectionChange', $event)">
    <el-table-column type="selection" width="48" />
    <el-table-column prop="meetingId" label="会议名称" min-width="280" :formatter="meetingNameFormatter" />
    <el-table-column prop="node" label="审核节点" :formatter="auditNodeFormatter" />
    <el-table-column prop="assigneeUserId" label="处理" width="140" :formatter="assigneeNameFormatter" />
    <el-table-column label="SLA" width="130">
      <template #default="{ row }">
        <el-tag v-if="Number(row.timeoutLevel || 0) >= 3" type="danger">
          超时24h+
        </el-tag>
        <el-tag v-else-if="Number(row.timeoutLevel || 0) >= 2" type="warning">
          超时12h+
        </el-tag>
        <el-tag v-else-if="Number(row.timeoutLevel || 0) >= 1" type="info">
          超时4h+
        </el-tag>
        <el-tag v-else type="success">
          正常
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="status" label="任务状态" :formatter="statusFormatter" />
    <el-table-column label="流转流程" min-width="260">
      <template #default="{ row }">
        <el-space wrap>
          <el-tag
            v-for="node in resolveFlowNodes(row)"
            :key="`${row.id}-${node.nodeCode || node.nodeName}`"
            :type="flowNodeTagType(node, row)"
          >
            {{ toZhAuditNode(node.nodeCode || node.nodeName) }}
          </el-tag>
        </el-space>
      </template>
    </el-table-column>
    <el-table-column prop="opinion" label="意见" />
    <el-table-column label="操作" width="360">
      <template #default="{ row }">
        <el-button
          v-if="(canApprove || canReject) && row.assigneeUserId === getCurrentUserId() && String(row.status || '').toUpperCase() === 'PENDING'"
          size="small"
          type="primary"
          :loading="submittingTaskId === row.id"
          @click="emit('submitAudit', row.id)"
        >
          提交审核
        </el-button>
        <el-button
          size="small"
          @click="emit('openAuditProgress', row)"
        >
          审核进度
        </el-button>
        <el-button
          v-if="canReadMaterial"
          size="small"
          @click="emit('openMaterial', row)"
        >
          查看资料
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
