<template>
  <div class="admin-page">
    <div class="admin-header">
      <h1>📊 管理后台</h1>
      <button class="back-btn" @click="$router.push('/')">返回首页</button>
    </div>

    <div v-if="loading" class="admin-loading">加载中...</div>

    <template v-else>
      <!-- 概览卡片 -->
      <div class="overview-grid">
        <div class="stat-card">
          <div class="stat-value">{{ overview.totalUsers }}</div>
          <div class="stat-label">总用户数</div>
        </div>
        <div class="stat-card">
          <div class="stat-value">{{ overview.totalPlans }}</div>
          <div class="stat-label">总行程数</div>
        </div>
        <div class="stat-card">
          <div class="stat-value">{{ overview.todayActiveUsers }}</div>
          <div class="stat-label">今日活跃用户</div>
        </div>
        <div class="stat-card">
          <div class="stat-value">{{ overview.todayActiveGuests }}</div>
          <div class="stat-label">今日活跃游客</div>
        </div>
        <div class="stat-card">
          <div class="stat-value">{{ overview.todayEvents }}</div>
          <div class="stat-label">今日事件数</div>
        </div>
        <div class="stat-card">
          <div class="stat-value">{{ overview.totalEvents }}</div>
          <div class="stat-label">总事件数</div>
        </div>
      </div>

      <div class="admin-content">
        <!-- 事件类型统计 -->
        <div class="admin-section">
          <h2>事件类型统计（近30天）</h2>
          <table class="data-table">
            <thead>
              <tr>
                <th>事件类型</th>
                <th>次数</th>
                <th>占比</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="event in events" :key="event.event_type">
                <td>{{ getEventLabel(event.event_type) }}</td>
                <td>{{ event.count }}</td>
                <td>{{ getPercentage(event.count) }}%</td>
              </tr>
            </tbody>
          </table>
        </div>

        <!-- 用户列表 -->
        <div class="admin-section">
          <h2>用户列表</h2>
          <table class="data-table">
            <thead>
              <tr>
                <th>用户名</th>
                <th>角色</th>
                <th>行程数</th>
                <th>活跃次数</th>
                <th>最后登录</th>
                <th>注册时间</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="user in users" :key="user.id">
                <td>{{ user.username }}</td>
                <td>
                  <span class="role-badge" :class="user.role">{{ user.role === 'admin' ? '管理员' : '用户' }}</span>
                </td>
                <td>{{ user.plan_count }}</td>
                <td>{{ user.event_count }}</td>
                <td>{{ user.last_active ? formatDate(user.last_active) : '未活跃' }}</td>
                <td>{{ formatDate(user.created_at) }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </template>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'

const router = useRouter()
const authStore = useAuthStore()

const loading = ref(true)
const overview = ref({})
const events = ref([])
const users = ref([])

const totalEventsCount = ref(0)

onMounted(async () => {
  // 检查管理员权限
  if (!authStore.isAdmin) {
    alert('需要管理员权限')
    router.push('/')
    return
  }
  
  await loadData()
})

async function loadData() {
  loading.value = true
  try {
    const API_BASE = 'http://localhost:3001/api'
    const headers = { 'Authorization': `Bearer ${authStore.token}` }
    
    // 加载概览数据
    const overviewRes = await fetch(`${API_BASE}/stats/admin/overview`, { headers })
    const overviewData = await overviewRes.json()
    overview.value = overviewData.overview
    
    // 加载事件统计
    const eventsRes = await fetch(`${API_BASE}/stats/admin/events-by-type?days=30`, { headers })
    const eventsData = await eventsRes.json()
    events.value = eventsData.events
    totalEventsCount.value = eventsData.events.reduce((sum, e) => sum + e.count, 0)
    
    // 加载用户列表
    const usersRes = await fetch(`${API_BASE}/stats/admin/users`, { headers })
    const usersData = await usersRes.json()
    users.value = usersData.users
  } catch (err) {
    console.error('加载数据失败:', err)
    alert('加载数据失败')
  } finally {
    loading.value = false
  }
}

function getEventLabel(type) {
  const labels = {
    'page_view': '页面访问',
    'plan_create': '创建行程',
    'plan_load': '加载行程',
    'plan_export': '导出行程',
    'chat_start': '开始聊天',
    'scheme_select': '选择方案',
    'user_register': '用户注册',
    'user_login': '用户登录',
    'guest_data_migrated': '游客数据迁移'
  }
  return labels[type] || type
}

function getPercentage(count) {
  if (totalEventsCount.value === 0) return 0
  return ((count / totalEventsCount.value) * 100).toFixed(1)
}

function formatDate(dateStr) {
  if (!dateStr) return '-'
  const date = new Date(dateStr)
  return date.toLocaleDateString('zh-CN', { 
    year: 'numeric', 
    month: '2-digit', 
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
</script>

<style scoped>
.admin-page {
  min-height: 100vh;
  background: #f5f7fa;
  padding: 20px;
}

.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.admin-header h1 {
  font-size: 28px;
  color: #2d3436;
}

.back-btn {
  background: #6c5ce7;
  color: #fff;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: opacity 0.2s;
}

.back-btn:hover {
  opacity: 0.9;
}

.admin-loading {
  text-align: center;
  padding: 40px;
  color: #636e72;
}

.overview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.stat-value {
  font-size: 36px;
  font-weight: 700;
  color: #6c5ce7;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 14px;
  color: #636e72;
}

.admin-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.admin-section {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.admin-section h2 {
  font-size: 18px;
  color: #2d3436;
  margin-bottom: 16px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th,
.data-table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #e9ecef;
}

.data-table th {
  font-size: 13px;
  font-weight: 600;
  color: #636e72;
  background: #f5f7fa;
}

.data-table td {
  font-size: 14px;
  color: #2d3436;
}

.data-table tr:hover {
  background: #f8f9ff;
}

.role-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.role-badge.admin {
  background: #ffe0e0;
  color: #d63031;
}

.role-badge.user {
  background: #e8e4ff;
  color: #6c5ce7;
}
</style>
