<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>
      <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="prompt_name" show-overflow-tooltip />
      <el-table-column label="标识" align="center" prop="prompt_key" width="120" />
      <el-table-column label="分类" align="center" prop="prompt_category" width="100" />
      <el-table-column label="模型" align="center" prop="model_name" width="100" />
      <el-table-column label="行业" align="center" prop="industry_type" 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="create_time" 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="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.prompt_name }}</el-descriptions-item>
        <el-descriptions-item label="标识">{{ detail.prompt_key }}</el-descriptions-item>
        <el-descriptions-item label="分类">{{ detail.prompt_category }}</el-descriptions-item>
        <el-descriptions-item label="模型">{{ detail.model_name }}</el-descriptions-item>
        <el-descriptions-item label="行业">{{ detail.industry_type }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ detail.enabled===1?'启用':'禁用' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detail.create_time }}</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.prompt_content }}</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-select v-model="form.modelName" placeholder="请选择模型">
            <el-option label="豆包Lite" value="doubao-lite" />
            <el-option label="豆包Pro" value="doubao-pro" />
          </el-select>
        </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 })
    },
    handleAdd() {
      this.form = { promptName: '', promptKey: '', promptCategory: '', promptContent: '', modelName: 'doubao-lite', industryType: '' }
      this.dialogTitle = '新增模板'
      this.dialogVisible = true
    },
    handleEdit(row) {
      getPrompt(row.id).then(res => {
        let d = res.data || {}
        this.form = { id: d.id, promptName: d.prompt_name, promptKey: d.prompt_key, promptCategory: d.prompt_category, promptContent: d.prompt_content, modelName: d.model_name, industryType: d.industry_type }
        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>