<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8"><el-col :span="1.5"><el-button type="primary" icon="el-icon-video-play" size="mini" @click="trigger">触发学习周期</el-button></el-col></el-row>
    <el-table border v-loading="loading" :data="list">
      <el-table-column prop="title" label="发现模式" show-overflow-tooltip/>
      <el-table-column prop="learningType" label="类型" width="90"/>
      <el-table-column prop="description" label="证据" show-overflow-tooltip/>
      <el-table-column prop="confidence" label="置信度" width="80"/>
      <el-table-column prop="status" label="状态" width="80"><template slot-scope="s"><el-tag :type="s.row.status==='discovered'?'warning':'success'" size="small">{{ s.row.status }}</el-tag></template></el-table-column>
      <el-table-column label="操作" width="100"><template slot-scope="s"><el-button v-if="s.row.status==='discovered'" size="mini" type="primary" @click="apply(s.row)">应用</el-button></template></el-table-column>
    </el-table>
  </div>
</template>
<script>
import { triggerLearningCycle, getLearningResults, applyLearningResult } from '@/api/workflow/lobster-dashboard'
export default {
  data() { return { list:[], loading:false } },
  computed: {
    companyId() {
      return this.$store.getters.companyId || (this.$store.state.user && this.$store.state.user.companyId) || 0
    }
  },
  created() { this.getList() },
  methods: {
    async getList() {
      this.loading = true
      try {
        const res = await getLearningResults(this.companyId)
        const data = res.data || res
        this.list = data.results || []
      } catch (e) { /* ignore */ }
      this.loading = false
    },
    async trigger() {
      try {
        const res = await triggerLearningCycle(this.companyId)
        const data = res.data || res
        this.$message.success(data.summary || '已触发')
        this.getList()
      } catch (e) { /* ignore */ }
    },
    async apply(row) {
      try {
        await applyLearningResult(this.companyId, row.id)
        this.$message.success('已应用')
        this.getList()
      } catch (e) { /* ignore */ }
    }
  }
}
</script>
