<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { adminApi } from '@/api/admin'
import type { AdminStatistics } from '@/types'

const loading = ref(true)
const statistics = ref<AdminStatistics | null>(null)

onMounted(async () => {
  try {
    const response = await adminApi.getStatistics()
    if (response.success && response.data) {
      statistics.value = response.data
    }
  } finally {
    loading.value = false
  }
})
</script>

<template>
  <div class="dashboard">
    <h2 class="page-title">仪表盘</h2>

    <div v-loading="loading" class="stats-grid">
      <div class="stat-card">
        <div class="stat-icon users">
          <el-icon size="28"><User /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-value">{{ statistics?.totalUsers || 0 }}</div>
          <div class="stat-label">总用户数</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon active">
          <el-icon size="28"><UserFilled /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-value">{{ statistics?.activeUsersLast7Days || 0 }}</div>
          <div class="stat-label">7日活跃用户</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon bookmarks">
          <el-icon size="28"><Collection /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-value">{{ statistics?.totalBookmarks || 0 }}</div>
          <div class="stat-label">总书签数</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon devices">
          <el-icon size="28"><Monitor /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-value">{{ statistics?.totalDevices || 0 }}</div>
          <div class="stat-label">总设备数</div>
        </div>
      </div>
    </div>

    <div class="stats-row">
      <div class="stat-card small">
        <div class="stat-label">今日新增用户</div>
        <div class="stat-value">{{ statistics?.newUsersToday || 0 }}</div>
      </div>
      <div class="stat-card small">
        <div class="stat-label">本周新增用户</div>
        <div class="stat-value">{{ statistics?.newUsersThisWeek || 0 }}</div>
      </div>
      <div class="stat-card small">
        <div class="stat-label">本月新增用户</div>
        <div class="stat-value">{{ statistics?.newUsersThisMonth || 0 }}</div>
      </div>
      <div class="stat-card small">
        <div class="stat-label">禁用用户数</div>
        <div class="stat-value warning">{{ statistics?.disabledUsers || 0 }}</div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.dashboard {
  .page-title {
    font-size: 20px;
    font-weight: 600;
    color: #333;
    margin-bottom: 24px;
  }
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.stat-card {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);

  &.small {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;

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

    .stat-value {
      font-size: 28px;
      font-weight: 700;
      color: #333;

      &.warning {
        color: #f56c6c;
      }
    }
  }

  .stat-icon {
    width: 56px;
    height: 56px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;

    &.users {
      background: linear-gradient(135deg, #667eea, #764ba2);
    }

    &.active {
      background: linear-gradient(135deg, #11998e, #38ef7d);
    }

    &.bookmarks {
      background: linear-gradient(135deg, #f093fb, #f5576c);
    }

    &.devices {
      background: linear-gradient(135deg, #4facfe, #00f2fe);
    }
  }

  .stat-content {
    .stat-value {
      font-size: 32px;
      font-weight: 700;
      color: #333;
      line-height: 1;
    }

    .stat-label {
      font-size: 14px;
      color: #999;
      margin-top: 4px;
    }
  }
}
</style>

