<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-magic-stick" size="mini" @click="goToGenerate">AI生成</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="60" />
      <el-table-column label="模板名称" align="center" prop="template_name" show-overflow-tooltip />
      <el-table-column label="模板编码" align="center" prop="template_code" width="140" />
      <el-table-column label="行业" align="center" prop="industry_type" width="80" />
      <el-table-column label="描述" align="center" prop="description" show-overflow-tooltip min-width="160" />
      <el-table-column label="状态" align="center" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status===1" type="success" size="small">已发布</el-tag>
          <el-tag v-else-if="scope.row.status===0" type="info" size="small">草稿</el-tag>
          <el-tag v-else type="warning" size="small">已停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="版本" align="center" prop="version" width="60" />
      <el-table-column label="创建时间" align="center" prop="create_time" width="160" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEditCanvas(scope.row)">画布编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-video-play" @click="handleStart(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" />
  </div>
</template>

<script>
import { listWorkflowTemplates } from '@/api/workflow/lobster'

export default {
  name: 'LobsterTemplate',
  data() {
    return {
      loading: false,
      showSearch: true,
      list: [],
      total: 0,
      queryParams: { page: 1, size: 10 }
    }
  },
  created() { this.getList() },
  methods: {
    getList() {
      this.loading = true
      listWorkflowTemplates().then(res => {
        this.list = res.data || []
        this.total = this.list.length
      }).finally(() => { this.loading = false })
    },
    handleEditCanvas(row) {
      this.$router.push({ path: '/lobster/production-workflow/canvas', query: { workflowId: row.id } })
    },
    handleStart(row) {
      this.$router.push({ path: '/lobster/production-workflow/instance', query: { templateId: row.id, templateName: row.template_name } })
    },
    goToGenerate() {
      this.$router.push('/lobster/workflow-generate')
    }
  }
}
</script>
