<template>
  <div class="dashboard-content">
    <div class="welcome-section">
      <h2>欢迎回来，{{ authStore.user?.username }}！</h2>
      <p>这是您的四川路桥AI后台管理平台</p>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-grid">
      <el-card class="stats-card">
        <div class="stats-content">
          <div class="stats-icon">
            <el-icon size="24" color="#409EFF"><Grid /></el-icon>
          </div>
          <div class="stats-info">
            <div class="stats-number">{{ stats.appCount }}</div>
            <div class="stats-label">我的应用</div>
          </div>
        </div>
      </el-card>

      <el-card class="stats-card">
        <div class="stats-content">
          <div class="stats-icon">
            <el-icon size="24" color="#67C23A"><Key /></el-icon>
          </div>
          <div class="stats-info">
            <div class="stats-number">{{ stats.tokenCount }}</div>
            <div class="stats-label">活跃令牌</div>
          </div>
        </div>
      </el-card>

      <el-card class="stats-card">
        <div class="stats-content">
          <div class="stats-icon">
            <el-icon size="24" color="#E6A23C"><Clock /></el-icon>
          </div>
          <div class="stats-info">
            <div class="stats-number">{{ stats.loginCount }}</div>
            <div class="stats-label">今日登录</div>
          </div>
        </div>
      </el-card>

      <el-card class="stats-card">
        <div class="stats-content">
          <div class="stats-icon">
            <el-icon size="24" color="#F56C6C"><Warning /></el-icon>
          </div>
          <div class="stats-info">
            <div class="stats-number">{{ stats.alertCount }}</div>
            <div class="stats-label">安全警告</div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 快速操作 -->
    <el-card class="quick-actions">
      <template #header>
        <span>快速操作</span>
      </template>
      <div class="actions-grid">
        <el-button type="primary" @click="$router.push('/apps/create')">
          <el-icon><Plus /></el-icon>
          创建应用
        </el-button>
        <el-button @click="$router.push('/profile')">
          <el-icon><User /></el-icon>
          编辑资料
        </el-button>
        <el-button @click="$router.push('/apps')">
          <el-icon><Grid /></el-icon>
          管理应用
        </el-button>
      </div>
    </el-card>

    <!-- 最近活动 -->
    <el-card class="recent-activity">
      <template #header>
        <span>最近活动</span>
      </template>
      <el-timeline>
        <el-timeline-item
          v-for="activity in recentActivities"
          :key="activity.id"
          :timestamp="activity.timestamp"
          :type="activity.type"
        >
          {{ activity.description }}
        </el-timeline-item>
      </el-timeline>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

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

// 统计数据
const stats = ref({
  appCount: 3,
  tokenCount: 12,
  loginCount: 8,
  alertCount: 0
})

// 最近活动
const recentActivities = ref([
  {
    id: 1,
    description: '创建了新应用 "测试应用"',
    timestamp: '2024-01-15 14:30',
    type: 'primary'
  },
  {
    id: 2,
    description: '更新了个人资料',
    timestamp: '2024-01-15 10:20',
    type: 'success'
  },
  {
    id: 3,
    description: '登录系统',
    timestamp: '2024-01-15 09:00',
    type: 'info'
  }
])

onMounted(() => {
  // 加载统计数据
  loadStats()
})

const loadStats = async () => {
  // TODO: 从API加载真实统计数据
  console.log('加载统计数据')
}
</script>

<style scoped>
.dashboard-content {
  padding: 20px;
}

.welcome-section {
  margin-bottom: 24px;
}

.welcome-section h2 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.welcome-section p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

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

.stats-card {
  cursor: pointer;
  transition: transform 0.2s;
}

.stats-card:hover {
  transform: translateY(-2px);
}

.stats-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stats-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f9ff;
}

.stats-info {
  flex: 1;
}

.stats-number {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  line-height: 1;
}

.stats-label {
  font-size: 14px;
  color: #666;
  margin-top: 4px;
}

.quick-actions {
  margin-bottom: 24px;
}

.actions-grid {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.recent-activity {
  margin-bottom: 24px;
}
</style>