<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="scenario">
        <el-select v-model="queryParams.scenario" placeholder="请选择场景" clearable size="small">
          <el-option v-for="s in scenarios" :key="s.code" :label="s.name" :value="s.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="已分析" value="analyzed" /><el-option label="待分析" value="pending" /><el-option label="已学习" value="learned" />
        </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="handleAddDialog">录入对话</el-button></el-col>
      <el-col :span="1.5"><el-button type="success" plain icon="el-icon-data-analysis" size="mini" @click="handleAnalyze">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="salespersonName" width="100" />
      <el-table-column label="客户问题" align="center" prop="customerQuestion" show-overflow-tooltip />
      <el-table-column label="销冠回答" align="center" prop="salesAnswer" show-overflow-tooltip />
      <el-table-column label="场景" align="center" prop="scenario" width="100" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==='learned'?'success':scope.row.status==='analyzed'?'warning':'info'" size="small">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.size" @pagination="getList" />
    <!-- 录入对话弹窗 -->
    <el-dialog title="录入销冠对话" :visible.sync="addVisible" width="600px" append-to-body>
      <el-form ref="addForm" :model="addForm" :rules="addRules" label-width="100px">
        <el-form-item label="销冠姓名" prop="salespersonName"><el-input v-model="addForm.salespersonName" placeholder="请输入销冠姓名" /></el-form-item>
        <el-form-item label="场景" prop="scenario">
          <el-select v-model="addForm.scenario" placeholder="请选择场景"><el-option v-for="s in scenarios" :key="s.code" :label="s.name" :value="s.code" /></el-select>
        </el-form-item>
        <el-form-item label="客户问题" prop="customerQuestion"><el-input v-model="addForm.customerQuestion" type="textarea" :rows="3" placeholder="请输入客户的问题" /></el-form-item>
        <el-form-item label="销冠回答" prop="salesAnswer"><el-input v-model="addForm.salesAnswer" type="textarea" :rows="3" placeholder="请输入销冠的回答" /></el-form-item>
      </el-form>
      <div slot="footer"><el-button @click="addVisible=false">取消</el-button><el-button type="primary" @click="submitAdd">录入</el-button></div>
    </el-dialog>
  </div>
</template>
<script>
import { listAllCompanies } from '@/api/workflow/lobster-admin'
import { listSalesCorpus, addCorpusDialog, analyzeCorpus, getCorpusScenarios } from '@/api/workflow/lobster'
export default {
  name: 'SalesCorpus',
  data() {
    return { loading: false, showSearch: true, list: [], total: 0, scenarios: [], addVisible: false,
      addForm: { salespersonName: '', scenario: '', customerQuestion: '', salesAnswer: '' },
      addRules: { customerQuestion: [{ required: true, message: '请输入客户问题', trigger: 'blur' }], salesAnswer: [{ required: true, message: '请输入销冠回答', trigger: 'blur' }] },
      queryParams: { page: 1, size: 10, scenario: null, status: null } }
  },
  created() { this.getCompanyList()
    this.getList(); this.getScenarios() },
  methods: {
    getCompanyList() {
      listAllCompanies().then(res => { this.companyList = res.data || [] })
    },
    getList() { this.loading = true; listSalesCorpus(this.queryParams).then(res => { let d = res.data || {}; this.list = d.list || []; this.total = d.total || 0; this.loading = false }).catch(() => { this.loading = false }) },
    getScenarios() { getCorpusScenarios().then(res => { this.scenarios = res.data || [] }) },
    handleQuery() { this.queryParams.page = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleAddDialog() { this.addVisible = true },
    submitAdd() { this.$refs.addForm.validate(v => { if (!v) return; addCorpusDialog(this.addForm).then(() => { this.$message.success('录入成功'); this.addVisible = false; this.getList() }) }) },
    handleAnalyze() { this.$confirm('确定触发AI语料分析吗？分析可能需要几分钟。', '提示', { type: 'warning' }).then(() => { analyzeCorpus().then(res => { let d = res.data || {}; this.$message.success('分析完成: 总录入' + d.totalEntries + '条, 评分' + d.overallScore) }) }) }
  }
}
</script>
