<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="机器人编号" prop="robotCode">
        <el-input
          v-model="queryParams.robotCode"
          placeholder="请输入机器人编号"
          clearable
          class="search-input"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日志级别" prop="logLevel">
        <el-select v-model="queryParams.logLevel" placeholder="请选择日志级别" clearable class="search-select">
          <el-option
            v-for="dict in robot_log_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日志类型" prop="logType">
        <el-select v-model="queryParams.logType" placeholder="请选择日志类型" clearable class="search-select">
          <el-option
            v-for="dict in robot_log_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模块名称" prop="moduleName">
        <el-input
          v-model="queryParams.moduleName"
          placeholder="请输入模块名称"
          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="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="daterangeLogTime"
          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:sysLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sysLogList">
      <el-table-column label="日志时间" align="center" prop="logTime" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.logTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日志级别" align="center" prop="logLevel" width="100">
        <template #default="scope">
          <dict-tag :options="robot_log_level" :value="scope.row.logLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="日志类型" align="center" prop="logType" width="110">
        <template #default="scope">
          <dict-tag :options="robot_log_type" :value="scope.row.logType"/>
        </template>
      </el-table-column>
      <el-table-column label="模块名称" align="center" prop="moduleName" width="120" show-overflow-tooltip />
      <el-table-column label="日志摘要" align="left" prop="contentSummary" min-width="260" show-overflow-tooltip>
        <template #default="scope">
          <span>{{ scope.row.contentSummary || formatSummary(scope.row.content) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结果状态" align="center" prop="resultStatus" width="100">
        <template #default="scope">
          <dict-tag :options="common_result_status" :value="scope.row.resultStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="机器人编号" align="center" prop="robotCode" width="130" show-overflow-tooltip />
      <el-table-column label="追踪ID" align="center" prop="traceId" width="150" show-overflow-tooltip />
      <el-table-column label="接收时间" align="center" prop="receiveTime" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.receiveTime) }}</span>
        </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="760px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="机器人编号">{{ detail.robotCode || '-' }}</el-descriptions-item>
        <el-descriptions-item label="日志时间">{{ parseTime(detail.logTime) || '-' }}</el-descriptions-item>
        <el-descriptions-item label="接收时间">{{ parseTime(detail.receiveTime) || '-' }}</el-descriptions-item>
        <el-descriptions-item label="日志级别">
          <dict-tag :options="robot_log_level" :value="detail.logLevel"/>
        </el-descriptions-item>
        <el-descriptions-item label="日志类型">
          <dict-tag :options="robot_log_type" :value="detail.logType"/>
        </el-descriptions-item>
        <el-descriptions-item label="模块名称">{{ detail.moduleName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="结果状态">
          <dict-tag :options="common_result_status" :value="detail.resultStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="追踪ID">
          <span>{{ detail.traceId || '-' }}</span>
          <el-button
            v-if="detail.traceId"
            link
            type="primary"
            size="small"
            @click="copyTraceId"
          >复制</el-button>
        </el-descriptions-item>
        <el-descriptions-item label="日志摘要" :span="2">{{ detail.contentSummary || '-' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ detail.remark || '-' }}</el-descriptions-item>
      </el-descriptions>
      <div class="log-content-block">
        <div class="log-content-title">日志内容</div>
        <pre class="log-content">{{ detail.content || '-' }}</pre>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SysLog">
import { listSysLog, getSysLog } from "@/api/base/sysLog"

const { proxy } = getCurrentInstance()
const { robot_log_type, robot_log_level, common_result_status } = useDict('robot_log_type', 'robot_log_level', 'common_result_status')

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

const daterangeLogTime = ref([])

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  robotCode: undefined,
  logLevel: undefined,
  logType: undefined,
  moduleName: undefined,
  keyword: undefined,
  resultStatus: undefined
})

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

/** 查询运行日志列表 */
function getList() {
  loading.value = true
  const params = proxy.addDateRange(queryParams.value, daterangeLogTime.value, 'LogTime')
  listSysLog(params).then(response => {
    sysLogList.value = response.rows || []
    total.value = response.total || 0
  }).finally(() => {
    loading.value = false
  })
}

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

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

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

/** 复制追踪ID */
function copyTraceId() {
  if (!detail.value.traceId) return
  navigator.clipboard.writeText(detail.value.traceId).then(() => {
    proxy.$modal.msgSuccess('追踪ID已复制')
  }).catch(() => {
    proxy.$modal.msgError('复制失败，请手动复制')
  })
}

/** 摘要兜底方法 */
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, daterangeLogTime.value, 'LogTime')
    proxy.download('base/sysLog/export', {
      ...params
    }, `运行日志_${new Date().getTime()}.xlsx`)
  }).catch(() => {})
}

getList()
</script>

<style scoped>
.search-select,
.search-input {
  width: 220px;
}
.search-date {
  width: 260px;
}
.log-content-block {
  margin-top: 16px;
}
.log-content-title {
  margin-bottom: 8px;
  font-weight: 600;
  color: #303133;
}
.log-content {
  min-height: 120px;
  max-height: 320px;
  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>
