<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="会话ID" prop="sessionId">
        <el-input
          v-model="queryParams.sessionId"
          placeholder="请输入会话ID"
          clearable
          class="search-input"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入用户问题关键字"
          clearable
          class="search-input"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="命中方式" prop="hitType">
        <el-select v-model="queryParams.hitType" placeholder="请选择命中方式" clearable class="search-select">
          <el-option
            v-for="dict in dialogue_hit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源场景" prop="sceneType">
        <el-select v-model="queryParams.sceneType" placeholder="请选择来源场景" clearable class="search-select">
          <el-option
            v-for="dict in dialogue_scene_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="结果状态" prop="resultStatus">
        <el-select v-model="queryParams.resultStatus" placeholder="请选择结果状态" clearable class="search-select">
          <el-option
            v-for="dict in common_result_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提问时间">
        <el-date-picker
          v-model="daterangeAskTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="search-date"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['base:dialogueLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dialogueLogList">
      <el-table-column label="提问时间" align="center" prop="askTime" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.askTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会话ID" align="center" prop="sessionId" width="150" show-overflow-tooltip />
      <el-table-column label="用户问题" align="left" prop="question" min-width="220" show-overflow-tooltip />
      <el-table-column label="回答摘要" align="left" prop="answerSummary" min-width="260" show-overflow-tooltip>
        <template #default="scope">
          <span>{{ scope.row.answerSummary || formatSummary(scope.row.answer) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="命中方式" align="center" prop="hitType" width="110">
        <template #default="scope">
          <dict-tag :options="dialogue_hit_type" :value="scope.row.hitType"/>
        </template>
      </el-table-column>
      <el-table-column label="来源场景" align="center" prop="sceneType" width="110">
        <template #default="scope">
          <dict-tag :options="dialogue_scene_type" :value="scope.row.sceneType"/>
        </template>
      </el-table-column>
      <el-table-column label="结果状态" align="center" prop="resultStatus" width="110">
        <template #default="scope">
          <dict-tag :options="common_result_status" :value="scope.row.resultStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="100">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="对话日志详情" v-model="detailOpen" width="820px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="机器人编号">{{ detail.robotCode || '-' }}</el-descriptions-item>
        <el-descriptions-item label="会话ID">{{ detail.sessionId || '-' }}</el-descriptions-item>
        <el-descriptions-item label="提问时间">{{ parseTime(detail.askTime) || '-' }}</el-descriptions-item>
        <el-descriptions-item label="命中方式">
          <dict-tag :options="dialogue_hit_type" :value="detail.hitType"/>
        </el-descriptions-item>
        <el-descriptions-item label="来源场景">
          <dict-tag :options="dialogue_scene_type" :value="detail.sceneType"/>
        </el-descriptions-item>
        <el-descriptions-item label="结果状态">
          <dict-tag :options="common_result_status" :value="detail.resultStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="用户问题" :span="2">{{ detail.question || '-' }}</el-descriptions-item>
        <el-descriptions-item label="回答摘要" :span="2">{{ detail.answerSummary || '-' }}</el-descriptions-item>
        <el-descriptions-item label="错误信息" :span="2">{{ detail.errorMsg || '-' }}</el-descriptions-item>
      </el-descriptions>
      <div class="detail-block">
        <div class="detail-block-title">机器人回答</div>
        <pre class="detail-content">{{ detail.answer || '-' }}</pre>
      </div>
      <div class="detail-block">
        <div class="detail-block-title">原始请求</div>
        <pre class="detail-content">{{ detail.rawRequest || '-' }}</pre>
      </div>
      <div class="detail-block">
        <div class="detail-block-title">原始响应</div>
        <pre class="detail-content">{{ detail.rawResponse || '-' }}</pre>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DialogueLog">
import { listDialogueLog, getDialogueLog } from "@/api/base/dialogueLog"

const { proxy } = getCurrentInstance()
const { dialogue_scene_type, common_result_status, dialogue_hit_type } = useDict('dialogue_scene_type', 'common_result_status', 'dialogue_hit_type')

const dialogueLogList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)

const daterangeAskTime = ref([])

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  sessionId: undefined,
  keyword: undefined,
  hitType: undefined,
  sceneType: undefined,
  resultStatus: undefined
})

const detailOpen = ref(false)
const detail = ref({})

/** 查询对话日志列表 */
function getList() {
  loading.value = true
  const params = proxy.addDateRange(queryParams.value, daterangeAskTime.value, 'AskTime')
  listDialogueLog(params).then(response => {
    dialogueLogList.value = response.rows || []
    total.value = response.total || 0
  }).finally(() => {
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeAskTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 详情按钮操作 */
function handleDetail(row) {
  getDialogueLog(row.id).then(response => {
    detail.value = response.data || row
    detailOpen.value = true
  })
}

/** 摘要兜底方法 */
function formatSummary(content) {
  if (!content) return '-'
  return content.length > 80 ? content.substring(0, 80) + '...' : content
}

/** 导出按钮操作 */
function handleExport() {
  proxy.$modal.confirm('确认导出当前查询条件下的对话日志数据吗？').then(() => {
    const params = proxy.addDateRange(queryParams.value, daterangeAskTime.value, 'AskTime')
    proxy.download('base/dialogueLog/export', {
      ...params
    }, `对话日志_${new Date().getTime()}.xlsx`)
  }).catch(() => {})
}

getList()
</script>

<style scoped>
.search-input,
.search-select {
  width: 220px;
}
.search-date {
  width: 260px;
}
.detail-block {
  margin-top: 16px;
}
.detail-block-title {
  margin-bottom: 8px;
  font-weight: 600;
  color: #303133;
}
.detail-content {
  min-height: 80px;
  max-height: 260px;
  overflow: auto;
  padding: 12px;
  margin: 0;
  line-height: 1.6;
  color: #303133;
  background: #f8fafc;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  white-space: pre-wrap;
  word-break: break-all;
}
</style>
