<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="所属租户" prop="companyId">
        <el-select v-model="queryParams.companyId" placeholder="全部租户" clearable size="small" @change="handleQuery">
          <el-option v-for="item in companyList" :key="item.id" :label="item.companyName" :value="item.id" />
        </el-select>
      </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 label="关键词" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入关键词" clearable size="small" @keyup.enter.native="handleQuery" />
      </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="warning" plain icon="el-icon-refresh" size="mini" @click="handleRefreshCache" 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" />
      <el-table-column label="提示词标识" align="center" prop="prompt_key" />
      <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="system_role" width="100" />
      <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="dialogTitle" :visible.sync="dialogVisible" width="600px" append-to-body>
      <el-form ref="promptForm" :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="6" placeholder="请输入提示词内容" />
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-select v-model="form.modelName" placeholder="请选择模型">
            <el-option label="豆包Lite" value="doubao-lite" />
            <el-option label="豆包Pro" value="doubao-pro" />
            <el-option label="GPT-4" value="gpt-4" />
            <el-option label="Claude" value="claude" />
          </el-select>
        </el-form-item>
        <el-form-item label="系统角色" prop="systemRole">
          <el-input v-model="form.systemRole" placeholder="请输入角色描述" />
        </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>

    <!-- 详情弹窗 -->
    <el-dialog title="提示词详情" :visible.sync="detailVisible" width="600px" append-to-body>
      <el-descriptions :column="1" 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.system_role }}</el-descriptions-item>
        <el-descriptions-item label="内容">
          <div style="white-space:pre-wrap;max-height:300px;overflow:auto">{{ detail.prompt_content }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detail.create_time }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

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

export default {
  name: 'LobsterPrompt',
  data() {
    return {
      loading: false,
      showSearch: true,
      list: [],
      total: 0,
      categories: [],
      dialogVisible: false,
      dialogTitle: '',
      detailVisible: false,
      detail: {},
      form: {},
      rules: {
        promptName: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        promptContent: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      },
      queryParams: { page: 1, size: 10, category: null, search: null }
    }
  },
  created() {
    this.getCompanyList()
    this.getList()
    this.getCategories()
  },
  methods: {
    getCompanyList() {
      listAllCompanies().then(res => { this.companyList = res.data || [] })
    },
    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() },
    handleAdd() {
      this.form = { promptName: '', promptKey: '', promptCategory: '', promptContent: '', modelName: 'doubao-lite', systemRole: '', 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, systemRole: d.system_role, industryType: d.industry_type }
        this.dialogTitle = '编辑提示词'
        this.dialogVisible = true
      })
    },
    handleDetail(row) {
      getPrompt(row.id).then(res => { this.detail = res.data || {}; this.detailVisible = true })
    },
    handleDelete(row) {
      this.$confirm('确定删除该提示词吗？', '提示', { type: 'warning' }).then(() => {
        deletePrompt(row.id).then(() => { this.$message.success('删除成功'); this.getList() })
      })
    },
    submitForm() {
      this.$refs.promptForm.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() })
        }
      })
    },
    handleRefreshCache() {
      refreshPromptCache().then(() => { this.$message.success('缓存已刷新') })
    }
  }
}
</script>