<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="待审核" value="pending" /><el-option label="已通过" value="approved" /><el-option label="已驳回" value="rejected" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 统计 -->
    <el-row :gutter="16" class="mb8">
      <el-col :span="4"><el-card shadow="hover"><div class="stat-card"><div class="stat-value" style="color:#E6A23C">{{ auditStats.pending || 0 }}</div><div class="stat-label">待审核</div></div></el-card></el-col>
      <el-col :span="4"><el-card shadow="hover"><div class="stat-card"><div class="stat-value" style="color:#67C23A">{{ auditStats.approved || 0 }}</div><div class="stat-label">已通过</div></div></el-card></el-col>
      <el-col :span="4"><el-card shadow="hover"><div class="stat-card"><div class="stat-value" style="color:#F56C6C">{{ auditStats.rejected || 0 }}</div><div class="stat-label">已驳回</div></div></el-card></el-col>
    </el-row>
    <el-table border v-loading="loading" :data="list">
      <el-table-column label="审核ID" align="center" prop="id" width="60" />
      <el-table-column label="实例ID" align="center" prop="instance_id" width="80" />
      <el-table-column label="插入位置" align="center" prop="insert_at" width="100" />
      <el-table-column label="节点描述" align="center" prop="node_description" show-overflow-tooltip />
      <el-table-column label="来源" align="center" prop="source" width="100" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==='pending'?'warning':scope.row.status==='approved'?'success':'danger'" size="small">{{ scope.row.status==='pending'?'待审核':scope.row.status==='approved'?'已通过':'已驳回' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time" width="160" />
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status==='pending'" size="mini" type="text" icon="el-icon-check" @click="handleApprove(scope.row)">通过</el-button>
          <el-button v-if="scope.row.status==='pending'" size="mini" type="text" icon="el-icon-close" style="color:#F56C6C" @click="handleReject(scope.row)">驳回</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.size" @pagination="getList" />
    <!-- 详情弹窗 -->
    <el-dialog title="审核详情" :visible.sync="detailVisible" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="审核ID">{{ detail.id }}</el-descriptions-item>
        <el-descriptions-item label="实例ID">{{ detail.instance_id }}</el-descriptions-item>
        <el-descriptions-item label="插入位置">{{ detail.insert_at }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ detail.status }}</el-descriptions-item>
        <el-descriptions-item label="来源">{{ detail.source }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detail.create_time }}</el-descriptions-item>
      </el-descriptions>
      <el-divider>节点JSON</el-divider>
      <div style="white-space:pre-wrap;max-height:300px;overflow:auto;background:#f5f7fa;padding:10px;font-size:12px">{{ detail.node_json }}</div>
    </el-dialog>
  </div>
</template>
<script>
import { listEventAudits, approveEvent, rejectEvent, getEventAuditDetail } from '@/api/workflow/lobster'
export default {
  name: 'LobsterEventAudit',
  data() {
    return { loading: false, showSearch: true, list: [], total: 0, auditStats: {}, detailVisible: false, detail: {},
      queryParams: { status: 'pending', page: 1, size: 10 } }
  },
  created() { this.getList() },
  methods: {
    getList() { this.loading = true; listEventAudits(this.queryParams).then(res => { let d = res.data || {}; this.list = d.list || []; this.total = d.total || 0; this.auditStats = d.stats || {}; this.loading = false }).catch(() => { this.loading = false }) },
    handleQuery() { this.queryParams.page = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleApprove(row) { this.$confirm('确定通过审核吗？通过后节点将注入工作流。', '提示', { type: 'warning' }).then(() => { approveEvent(row.id).then(res => { this.$message.success(res.data?.message || '审核通过'); this.getList() }) }) },
    handleReject(row) { this.$confirm('请输入驳回原因', '驳回审核', { confirmButtonText: '确定', cancelButtonText: '取消' }).then(({ value }) => { rejectEvent(row.id, { comment: value || '人工驳回' }).then(() => { this.$message.success('已驳回'); this.getList() }) }) },
    handleDetail(row) { getEventAuditDetail(row.id).then(res => { this.detail = res.data || {}; this.detailVisible = true }) }
  }
}
</script>
<style scoped>
.stat-card { text-align: center; padding: 10px 0; }
.stat-value { font-size: 24px; font-weight: bold; }
.stat-label { font-size: 12px; color: #909399; margin-top: 4px; }
</style>
