<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-select v-model="queryParams.processName" placeholder="全部" clearable style="width: 180px;">
            <el-option v-for="def in definitionList" :key="def.id" :label="def.name" :value="def.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="归属用户">
          <el-input v-model="queryParams.applicantName" placeholder="用户名或姓名" clearable style="width: 160px;" />
        </el-form-item>
        <el-form-item label="当前节点">
          <el-select v-model="queryParams.currentNodeName" placeholder="全部" clearable style="width: 180px;">
            <el-option v-for="node in currentNodeOptions" :key="node" :label="node" :value="node" />
          </el-select>
        </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="业务编号">
          <template #default="{ row }">
            {{ row.bizValue || row.instanceNo || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="applicantName" label="发起人" width="120">
          <template #default="{ row }">
            {{ row.applicantName || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="instanceStatusTagType(row.status)">{{ instanceStatusText(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="currentNodeName" label="当前节点">
          <template #default="{ row }">
            {{ row.currentNodeName || row.currentNode || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="发起时间" />
        <el-table-column prop="endTime" label="结束时间" />
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleDetail(row)">查看详情</el-button>
          </template>
        </el-table-column>
      </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"
      />

      <InstanceDetail v-model="detailVisible" :instance-id="selectedInstanceId" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { listInstance } from '@/api/flow/instance'
import { listEnabled } from '@/api/flow/definition'
import InstanceDetail from '@/views/flow/execute/InstanceDetail.vue'
import { instanceStatusText, instanceStatusTagType } from '@/utils/flow'
import type { FlowInstance, FlowDefinition } from '@/types/flow'

const loading = ref(false)
const tableData = ref<FlowInstance[]>([])
const total = ref(0)
const detailVisible = ref(false)
const selectedInstanceId = ref<number>(0)
const definitionList = ref<FlowDefinition[]>([])
const currentNodeOptions = ref<string[]>([])

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  processName: undefined as string | undefined,
  applicantName: undefined as string | undefined,
  currentNodeName: undefined as string | undefined
})

function handleDetail(row: FlowInstance) {
  selectedInstanceId.value = row.id
  detailVisible.value = true
}

function handleQuery() { queryParams.pageNum = 1; loadData() }
function resetQuery() {
  queryParams.processName = undefined
  queryParams.applicantName = undefined
  queryParams.currentNodeName = undefined
  queryParams.pageNum = 1
  loadData()
}

async function loadData() {
  loading.value = true
  try {
    const res = await listInstance(queryParams)
    tableData.value = res.list
    total.value = res.total
  } finally { loading.value = false }
}

async function loadDefinitions() {
  try { definitionList.value = await listEnabled() } catch { /* ignore */ }
}

onMounted(() => { loadDefinitions(); 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>
