<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="模板名称" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入模板名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="场景分类" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择分类" clearable size="small">
          <el-option v-for="cat in categories" :key="cat" :label="cat" :value="cat" />
        </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-plus" size="mini" @click="handleAdd" v-hasPermi="['workflow:lobster:edit']">新增模板</el-button>
      </el-col>
      <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="promptName" show-overflow-tooltip />
      <el-table-column label="标识" align="center" prop="promptKey" width="120" />
      <el-table-column label="分类" align="center" prop="promptCategory" width="100" />
      <el-table-column label="模型" align="center" prop="modelName" width="100" />
      <el-table-column label="行业" align="center" prop="industryType" width="80" />
      <el-table-column label="状态" align="center" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enabled===1" type="success" size="small">启用</el-tag>
          <el-tag v-else type="info" size="small">禁用</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="240">
        <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="handleEdit(scope.row)" v-hasPermi="['workflow:lobster:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['workflow:lobster:edit']">删除</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="模板名称">{{ detail.promptName }}</el-descriptions-item>
        <el-descriptions-item label="标识">{{ detail.promptKey }}</el-descriptions-item>
        <el-descriptions-item label="分类">{{ detail.promptCategory }}</el-descriptions-item>
        <el-descriptions-item label="模型">{{ detail.modelName }}</el-descriptions-item>
        <el-descriptions-item label="行业">{{ detail.industryType }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ detail.enabled===1?'启用':'禁用' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detail.createTime }}</el-descriptions-item>
      </el-descriptions>
      <el-divider>模板内容</el-divider>
      <div style="white-space:pre-wrap;max-height:300px;overflow:auto;background:#f5f7fa;padding:10px;font-size:12px">{{ detail.promptContent }}</div>
      <div slot="footer">
        <el-button @click="detailVisible=false">关闭</el-button>
        <el-button type="primary" @click="detailVisible=false; handleEditCanvas(detail)">进入画布编辑</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" append-to-body>
      <el-form ref="templateForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="模板名称" prop="promptName">
          <el-input v-model="form.promptName" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="模板标识" prop="promptKey">
          <el-input v-model="form.promptKey" placeholder="请输入标识key" />
        </el-form-item>
        <el-form-item label="分类" prop="promptCategory">
          <el-select v-model="form.promptCategory" placeholder="请选择分类">
            <el-option v-for="cat in categories" :key="cat" :label="cat" :value="cat" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板内容" prop="promptContent">
          <el-input v-model="form.promptContent" type="textarea" :rows="4" placeholder="请输入模板内容" />
        </el-form-item>
        <el-form-item label="模型">
          <el-input v-model="form.modelName" placeholder="请输入模型名称，如 doubao-lite" />
        </el-form-item>
        <el-form-item label="行业类型">
          <el-input v-model="form.industryType" placeholder="请输入行业类型" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPrompts, getPrompt, addPrompt, updatePrompt, deletePrompt, getPromptCategories } from '@/api/workflow/lobster'

export default {
  name: 'LobsterTemplate',
  data() {
    return {
      loading: false,
      showSearch: true,
      list: [],
      total: 0,
      categories: [],
      detailVisible: false,
      detail: {},
      dialogVisible: false,
      dialogTitle: '',
      form: {},
      rules: {
        promptName: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        promptContent: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      },
      queryParams: { page: 1, size: 10, search: null, category: null }
    }
  },
  created() {
    this.getList()
    this.getCategories()
  },
  methods: {
    getList() {
      this.loading = true
      listPrompts(this.queryParams).then(res => {
        let data = res.data || {}
        this.list = data.list || []
        this.total = data.total || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    getCategories() {
      getPromptCategories().then(res => { this.categories = res.data || [] })
    },
    handleQuery() { this.queryParams.page = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleDetail(row) {
      getPrompt(row.id).then(res => { this.detail = res.data || {}; this.detailVisible = true })
    },
    handleEditCanvas(row) {
      this.$router.push({ path: '/lobster/production-workflow/canvas', query: { templateId: row.id || row.promptKey } })
    },
    goToGenerate() {
      this.$router.push('/lobster/workflow-generate')
    },
    handleAdd() {
      this.form = { promptName: '', promptKey: '', promptCategory: '', promptContent: '', modelName: '', industryType: '' }
      this.dialogTitle = '新增模板'
      this.dialogVisible = true
    },
    handleEdit(row) {
      getPrompt(row.id).then(res => {
        const data = res.data || {}
        this.form = {
          id: data.id,
          promptName: data.promptName || '',
          promptKey: data.promptKey || '',
          promptCategory: data.promptCategory || '',
          promptContent: data.promptContent || '',
          modelName: data.modelName || '',
          industryType: data.industryType || ''
        }
        this.dialogTitle = '编辑模板'
        this.dialogVisible = true
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除该模板吗？', '提示', { type: 'warning' }).then(() => {
        deletePrompt(row.id).then(() => { this.$message.success('删除成功'); this.getList() })
      })
    },
    submitForm() {
      this.$refs.templateForm.validate(valid => {
        if (!valid) return
        if (this.form.id) {
          updatePrompt(this.form.id, this.form).then(() => { this.$message.success('更新成功'); this.dialogVisible = false; this.getList() })
        } else {
          addPrompt(this.form).then(() => { this.$message.success('新增成功'); this.dialogVisible = false; this.getList() })
        }
      })
    }
  }
}
</script>
