<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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 渠道概览 -->
    <el-row :gutter="16" class="mb8">
      <el-col :span="4">
        <el-card shadow="hover">
          <div class="stat-card">
            <div class="stat-value">{{ channelInfo.qw || 0 }}</div>
            <div class="stat-label">企微渠道</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover">
          <div class="stat-card">
            <div class="stat-value" style="color:#67C23A">{{ channelInfo.wx || 0 }}</div>
            <div class="stat-label">个微渠道</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover">
          <div class="stat-card">
            <div class="stat-value" style="color:#E6A23C">{{ channelInfo.sms || 0 }}</div>
            <div class="stat-label">短信渠道</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="4">
        <el-card shadow="hover">
          <div class="stat-card">
            <div class="stat-value" style="color:#F56C6C">{{ channelInfo.ai_call || 0 }}</div>
            <div class="stat-label">AI外呼渠道</div>
          </div>
        </el-card>
      </el-col>
    </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">
        <template slot-scope="scope">
          <el-tag type="success" size="small">{{ scope.row.model_name || '-' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="角色" align="center" prop="system_role" width="100" show-overflow-tooltip />
      <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" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(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" />

    <!-- 详情弹窗 -->
    <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.system_role }}</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>
      <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>
  </div>
</template>

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

export default {
  name: 'LobsterModelConfig',
  data() {
    return {
      loading: false,
      showSearch: true,
      list: [],
      total: 0,
      categories: [],
      channelInfo: {},
      detailVisible: false,
      detail: {},
      queryParams: { page: 1, size: 10, search: null, category: null }
    }
  },
  created() {
    this.getList()
    this.getCategories()
    this.getChannelInfo()
  },
  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 || [] })
    },
    getChannelInfo() {
      getAvailableChannels().then(res => { this.channelInfo = 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 })
    }
  }
}
</script>

<style scoped>
.stat-card { text-align: center; padding: 10px 0; }
.stat-value { font-size: 24px; font-weight: bold; color: #409EFF; }
.stat-label { font-size: 12px; color: #909399; margin-top: 4px; }
</style>