<template>
  <div class="container">
    <Info :infoData="projectData" />
    <div class="test-container">
      <div class="title">测试结果</div>
      <div>
        <el-button>生成报告</el-button>
        <el-button>导出结果</el-button>
      </div>
    </div>

    <div class="context-container">
      <div class="left">
        <div class="title">方案选择</div>
        <div class="schemaList">
          <div v-for="item in schemaList" class="schema" :class="{ activeItem: activeSchema === item.schemeId }" @click="clickSchema(item)">
            <div class="name">{{ item.schemeName }}</div>
            <div class="type"><el-tag>{{ item.sceneType === 1 ? '唤醒场景' : item.sceneType === 2 ? '单词对话场景' : '连续对话场景' }}</el-tag></div>
          </div>
        </div>
      </div>
      <div class="right">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="结果ID" />
          <el-table-column prop="time" label="时间" />
          <el-table-column prop="text" label="语料文本" />
          <el-table-column prop="responseTime" label="响应时间" show-overflow-tooltip>
            <template #default="{ row }">
              <div v-if="row.responseTime">{{ row.responseTime }}</div>
              <div v-else><el-tag type="primary">未测试</el-tag></div>
            </template>
          </el-table-column>
          <el-table-column prop="rate" label="字识别率" show-overflow-tooltip>
            <template #default="{ row }">
              <div v-if="row.rate">{{ row.rate }}</div>
              <div v-else><el-tag type="info">未测试</el-tag></div>
            </template>
          </el-table-column>
          <el-table-column prop="result" label="测试结果">
            <template #default="{ row }">
              <el-tag :type="row.result ? 'success' : 'warning'">{{ row.result ? '通过' : '分析中' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="can" label="CAN结果">
            <template #default="{ row }">
              <div v-if="row.rate">{{ row.can }}</div>
              <div v-else><el-tag type="danger">未测试</el-tag></div>
            </template>
          </el-table-column>
          <el-table-column prop="flag" label="结果标记 " />
        </el-table>
      </div>
    </div>

  </div>
</template>
<script setup>
import { onMounted } from 'vue';
import Info from './info.vue';
import { listScheme } from '@/api/vi/plan'
import { listCorpus, getBatchCorpus } from '@/api/vi/corpus'

const route = useRoute();

const projectData = route.query.data 
  ? JSON.parse(route.query.data) 
  : {}

console.log('projectData', projectData)

const schemaList = ref([])

const tableData = ref([])

const getListScheme = async () => {
  const res = await listScheme({
    pageNum: 1,
    pageSize: 100000,
    projectId: route.params.id,
  })

  if (res.code === 200) {
    schemaList.value = res.rows;
  }
}

const activeSchema = ref(null)
const clickSchema = async (item) => {
  activeSchema.value = item.schemeId
  const obj = {
    type: 'TEST',
    continuous: 0
  }
  let res
  if (item.sceneType === 1) {
    obj.type = 'WAKEUP'
    obj.continuous = 1
    obj.parentIds = item.wakeId
    res = await listCorpus({
      pageNum: 1,
      pageSize: 100000,
      ...obj
    })
  } else if (item.sceneType === 2) {
    obj.continuous = 1
    obj.parentIds = item.corpusIds.split(",")
    res = await listCorpus({
      pageNum: 1,
      pageSize: 100000,
      ...obj
    })
  } else if (item.sceneType === 3) {
    obj.continuous = 0
    obj.parentIds = item.corpusIds.split(",")
    res = await getBatchCorpus({
      pageNum: 1,
      pageSize: 100000,
      ...obj
    })
  }
  
  if (res.code === 200) {
    tableData.value = res.rows.map(item => {
      return {
        id: 'test result 876',
        time: '2025-11-26 16:46:23',
        text: item.corpusName,
        responseTime: 0,
        rate: 0,
        result: 1,
        can: 0,
        flag: '无'
      }
    })
  }
}

onMounted(() => {
  getListScheme()
})

</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;

  .btn-container {
    background: #fff;
    margin: 4px 0;
    padding: 4px 0;
  }

  .test-container {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .title {
      font-size: 20px;
      font-weight: bold;
    }
  }

  .context-container {
    height: calc(100% - 200px);
    display: flex;
    gap: 10px;

    .left {
      width: 300px;
      
      .title {
        height: 40px;
        display: flex;
        align-items: center;
        background: #fff;
        padding-left: 12px;
        border: 1px solid #e4e7ed;
        margin-bottom: 2px;
      }

      .schemaList {
        height: calc(100% - 40px);
        overflow-y: auto;
        background: #fff;
        border: 1px solid #e4e7ed;

        .schema {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 12px;

          &:hover {
            background: #f0ecec;
          }
        }

        .activeItem {
          background: #f0ecec;
        }
      }
    }

    .right {
      flex: 1;
    }
  }
}
</style>