<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="工作流名称" prop="workflowName">
        <el-input v-model="queryParams.workflowName" placeholder="请输入工作流名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="运行中" value="running" />
          <el-option label="已暂停" value="paused" />
          <el-option label="已完成" value="completed" />
          <el-option label="已终止" value="terminated" />
        </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">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-edit" size="mini" @click="handleDesign" v-hasPermi="['workflow:lobster:edit']">设计工作流</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="list">
      <el-table-column label="实例ID" align="center" prop="id" width="80" />
      <el-table-column label="工作流名称" align="center" prop="workflowName" />
      <el-table-column label="联系人" align="center" prop="contactId" width="100" />
      <el-table-column label="当前节点" align="center" prop="currentNodeName" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==='running'?'success':scope.row.status==='paused'?'warning':scope.row.status==='completed'?'success':'info'" size="small">{{ scope.row.status==='running'?'运行中':scope.row.status==='paused'?'已暂停':scope.row.status==='completed'?'已完成':'已终止' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleOpenDesign(scope.row)">设计</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @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.workflowId }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ detail.contactId }}</el-descriptions-item>
        <el-descriptions-item label="当前节点">{{ detail.currentNodeName }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ detail.status }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detail.createTime }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listInstances, getInstance } from '@/api/workflow/lobster'

export default {
  name: 'LobsterCanvas',
  data() {
    return {
      loading: false,
      showSearch: true,
      list: [],
      total: 0,
      detailVisible: false,
      detail: {},
      queryParams: { pageNum: 1, pageSize: 10, workflowName: null, status: null }
    }
  },
  created() { this.getList() },
  methods: {
    getList() {
      this.loading = true
      listInstances(this.queryParams).then(res => {
        let data = res.data || {}
        this.list = data.list || res.rows || []
        this.total = data.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleDetail(row) {
      getInstance(row.id).then(res => {
        this.detail = res.data || row
        this.detailVisible = true
      })
    },
    handleOpenDesign(row) {
      this.$message.info('请前往企业管理 > AI工作流进行画布设计')
    },
    handleDesign() {
      this.$message.info('请前往企业管理 > AI工作流创建新工作流')
    }
  }
}
</script>