<template>
  <div v-if="detail">
    <el-page-header @back="$router.back()" :content="detail.title" style="margin-bottom: 24px" />

    <!-- Basic Info -->
    <el-card style="margin-bottom: 16px">
      <template #header>基本信息</template>
      <el-descriptions :column="3" border>
        <el-descriptions-item label="类型">
          <el-tag size="small" :type="issueTypeTag(detail.type)">{{ detail.type }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="平台">{{ detail.platform }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ detail.status }}</el-descriptions-item>
        <el-descriptions-item label="应用版本">{{ detail.appVersion }}</el-descriptions-item>
        <el-descriptions-item label="系统版本">{{ detail.osVersion }}</el-descriptions-item>
        <el-descriptions-item label="设备型号">{{ detail.deviceModel }}</el-descriptions-item>
        <el-descriptions-item label="总次数">{{ detail.count }}</el-descriptions-item>
        <el-descriptions-item label="影响用户">{{ detail.affectedUsers }}</el-descriptions-item>
        <el-descriptions-item label="首次出现">{{ formatTime(detail.firstSeenAt) }}</el-descriptions-item>
        <el-descriptions-item label="最后出现" :span="2">{{ formatTime(detail.lastSeenAt) }}</el-descriptions-item>
      </el-descriptions>
    </el-card>

    <!-- Stack Trace -->
    <el-card style="margin-bottom: 16px">
      <template #header>Stack Trace</template>
      <pre class="stack-trace">{{ detail.stackTrace }}</pre>
    </el-card>

    <!-- Source Context -->
    <el-card v-if="detail.sourceContext?.length" style="margin-bottom: 16px">
      <template #header>源码上下文</template>
      <div class="source-context">
        <div
          v-for="(line, idx) in detail.sourceContext"
          :key="idx"
          class="source-line"
          :class="{ highlight: line.highlight }"
        >
          <span class="line-number">{{ line.line }}</span>
          <code>{{ line.content }}</code>
        </div>
      </div>
    </el-card>

    <!-- Recent Events -->
    <el-card>
      <template #header>最近事件</template>
      <el-table :data="detail.recentEvents" border stripe>
        <el-table-column prop="eventName" label="事件名" width="180" />
        <el-table-column prop="userId" label="用户 ID" width="200" show-overflow-tooltip />
        <el-table-column prop="timestamp" label="时间" width="170">
          <template #default="{ row }">{{ formatTime(row.timestamp) }}</template>
        </el-table-column>
        <el-table-column label="属性" min-width="200">
          <template #default="{ row }">
            <el-popover trigger="click" :width="400">
              <template #reference>
                <el-button link type="primary" size="small">查看</el-button>
              </template>
              <pre class="props-json">{{ JSON.stringify(row.properties, null, 2) }}</pre>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>

  <div v-else v-loading="loading" style="min-height: 300px" />
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { bugCollectApi, type BugCollectIssueDetail } from '@/api/bugcollect'

const route = useRoute()
const detail = ref<BugCollectIssueDetail | null>(null)
const loading = ref(false)

function issueTypeTag(type: string) {
  const map: Record<string, string> = {
    CRASH: 'danger',
    ERROR: 'warning',
    ANR: 'danger',
    WARNING: '',
  }
  return (map[type] ?? '') as '' | 'success' | 'warning' | 'info' | 'danger'
}

function formatTime(ts: string) {
  if (!ts) return '-'
  return new Date(ts).toLocaleString('zh-CN')
}

onMounted(async () => {
  const id = route.params.id as string
  loading.value = true
  try {
    const res = await bugCollectApi.issueDetail(id)
    detail.value = res.data.data
  } catch {
  } finally {
    loading.value = false
  }
})
</script>

<style scoped>
.stack-trace {
  background: #1e1e1e;
  color: #d4d4d4;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
}
.source-context {
  background: #1e1e1e;
  border-radius: 8px;
  padding: 12px 0;
  overflow-x: auto;
}
.source-line {
  display: flex;
  align-items: stretch;
  font-family: 'Courier New', monospace;
  font-size: 13px;
  line-height: 1.6;
  color: #d4d4d4;
  padding: 0 12px;
}
.source-line.highlight {
  background: rgba(255, 200, 0, 0.15);
  border-left: 3px solid #ffc800;
}
.line-number {
  width: 48px;
  text-align: right;
  color: #858585;
  padding-right: 12px;
  user-select: none;
  flex-shrink: 0;
}
.source-line code {
  white-space: pre;
}
.props-json {
  background: #f5f5f5;
  padding: 12px;
  border-radius: 6px;
  font-size: 12px;
  overflow-x: auto;
  max-height: 400px;
}
</style>
