<template>
  <div>
    <h2 style="margin-bottom: 24px">事件流水</h2>

    <!-- App selector bar -->
    <div class="app-selector-bar">
      <span class="selector-label">选择应用</span>
      <el-select
        :model-value="appKey"
        placeholder="请选择应用"
        style="width:220px"
        :loading="loadingApps"
        @change="setApp"
      >
        <el-option v-for="a in apps" :key="a.appKey" :label="a.name" :value="a.appKey" />
      </el-select>
    </div>
    <el-empty v-if="gateStatus === 'no-app'" description="请选择一个应用" style="margin-top:80px" />
    <div v-else-if="gateStatus === 'loading'" v-loading="true" style="min-height:200px" />
    <div v-else-if="gateStatus === 'not-enabled'" style="margin-top:60px;text-align:center">
      <el-empty description="当前应用未开通崩溃收集服务">
        <el-button type="primary" @click="applyDialogVisible = true">申请开通</el-button>
      </el-empty>
    </div>
    <template v-else>

    <el-card shadow="never">
      <div class="toolbar responsive-toolbar">
        <el-input
          v-model="filters.eventName"
          placeholder="事件名"
          style="width: 200px"
          clearable
          @clear="loadData"
          @keyup.enter="loadData"
        />
        <el-input
          v-model="filters.userId"
          placeholder="用户 ID"
          style="width: 200px"
          clearable
          @clear="loadData"
          @keyup.enter="loadData"
        />
        <el-date-picker
          v-model="filters.dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          style="width: 280px"
          @change="loadData"
        />
        <el-button type="primary" @click="loadData">搜索</el-button>
        <el-button :loading="loading" @click="loadData">刷新</el-button>
      </div>

      <div class="table-wrap">
        <el-table :data="events" v-loading="loading" border stripe>
          <el-table-column prop="platform" label="平台" width="100" />
          <el-table-column prop="appVersion" label="版本" width="110" />
          <el-table-column prop="userId" label="用户 ID" width="180" show-overflow-tooltip />
          <el-table-column prop="message" label="错误信息" min-width="260" show-overflow-tooltip />
          <el-table-column prop="createdAt" label="时间" width="170" sortable>
            <template #default="{ row }">
              <span class="time-text">{{ formatTime(row.createdAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="堆栈" width="90" align="center">
            <template #default="{ row }">
              <el-popover trigger="click" :width="600">
                <template #reference>
                  <el-button link type="primary" size="small" :disabled="!row.stack">查看</el-button>
                </template>
                <pre class="props-json">{{ row.stackSymbolicated || row.stack }}</pre>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-pagination
        style="margin-top: 16px"
        layout="total, sizes, prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        :page-sizes="[20, 50, 100]"
        @current-change="handlePageChange"
        @size-change="handleSizeChange"
      />
    </el-card>
    </template>
    <el-dialog v-model="applyDialogVisible" title="申请开通崩溃收集" width="400px" :close-on-click-modal="false">
      <el-input v-model="applyReason" type="textarea" placeholder="请说明申请原因（选填）" :rows="3" />
      <template #footer>
        <el-button @click="applyDialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="applyLoading" @click="submitActivation">提交申请</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { bugCollectApi, type BugCollectEventItem } from '@/api/bugcollect'
import { useBugCollectApp } from '@/composables/useBugCollectApp'
import { formatTime } from '@/utils/bugCollect'

const { apps, loadingApps, appKey, setApp, gateStatus, applyDialogVisible, applyReason, applyLoading, submitActivation } = useBugCollectApp()

const events = ref<BugCollectEventItem[]>([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(20)

const filters = ref({
  eventName: '',
  userId: '',
  dateRange: null as [string, string] | null,
})

async function loadData() {
  if (!appKey.value || gateStatus.value !== 'enabled') return
  loading.value = true
  try {
    const res = await bugCollectApi.events(appKey.value, {
      eventName: filters.value.eventName || undefined,
      userId: filters.value.userId || undefined,
      startDate: filters.value.dateRange?.[0] || undefined,
      endDate: filters.value.dateRange?.[1] || undefined,
      page: currentPage.value - 1,
      size: pageSize.value,
    })
    const data = res.data.data
    events.value = data.items ?? []
    total.value = data.total ?? 0
  } catch {
    ElMessage.error('加载事件列表失败')
  } finally {
    loading.value = false
  }
}

function handlePageChange(page: number) {
  currentPage.value = page
  loadData()
}

function handleSizeChange(size: number) {
  pageSize.value = size
  currentPage.value = 1
  loadData()
}

watch(gateStatus, (s) => { if (s === 'enabled') loadData() })
onMounted(loadData)
</script>

<style scoped>
.app-selector-bar { display:flex; align-items:center; gap:12px; margin-bottom:20px; }
.selector-label { font-size:14px; color:#606266; }
.responsive-toolbar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.table-wrap {
  overflow-x: auto;
}
.time-text {
  font-size: 13px;
  color: #666;
}
.props-json {
  background: #f5f5f5;
  padding: 12px;
  border-radius: 6px;
  font-size: 12px;
  overflow-x: auto;
  max-height: 400px;
}
</style>
