<template>
  <div class="dashboard">
    <!-- 统计卡片 -->
    <div class="stats-grid">
      <div class="stat-card">
        <div class="stat-icon" style="background: linear-gradient(135deg, #6366F1 0%, #4F46E5 100%);">
          <Building2 :size="24" :stroke-width="1.5" />
        </div>
        <div class="stat-content">
          <span class="stat-value">{{ statsData.tenantCount }}</span>
          <span class="stat-label">租户总数</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon" style="background: linear-gradient(135deg, #10B981 0%, #059669 100%);">
          <BookOpen :size="24" :stroke-width="1.5" />
        </div>
        <div class="stat-content">
          <span class="stat-value">{{ statsData.courseCount }}</span>
          <span class="stat-label">课程包总数</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon" style="background: linear-gradient(135deg, #F59E0B 0%, #D97706 100%);">
          <PlayCircle :size="24" :stroke-width="1.5" />
        </div>
        <div class="stat-content">
          <span class="stat-value">{{ statsData.monthlyLessons }}</span>
          <span class="stat-label">月授课次数</span>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon" style="background: linear-gradient(135deg, #EC4899 0%, #DB2777 100%);">
          <Users :size="24" :stroke-width="1.5" />
        </div>
        <div class="stat-content">
          <span class="stat-value">{{ statsData.studentCount }}</span>
          <span class="stat-label">覆盖学生</span>
        </div>
      </div>
    </div>

    <!-- 趋势图和快捷入口 -->
    <a-row :gutter="24" style="margin-top: 24px">
      <a-col :span="16">
        <a-card title="使用趋势" :bordered="false" :loading="trendLoading" class="modern-card">
          <div ref="trendChartRef" style="height: 300px"></div>
        </a-card>
      </a-col>
      <a-col :span="8">
        <a-card title="快捷入口" :bordered="false" class="modern-card">
          <div class="quick-actions">
            <div class="quick-action-item" @click="$router.push('/admin/courses/create')">
              <div class="action-icon">
                <Plus :size="20" :stroke-width="1.5" />
              </div>
              <span>创建课程包</span>
            </div>
            <div class="quick-action-item" @click="$router.push('/admin/tenants')">
              <div class="action-icon">
                <Building2 :size="20" :stroke-width="1.5" />
              </div>
              <span>管理租户</span>
            </div>
            <div class="quick-action-item" @click="$router.push('/admin/resources')">
              <div class="action-icon">
                <FolderOpen :size="20" :stroke-width="1.5" />
              </div>
              <span>资源库</span>
            </div>
          </div>
        </a-card>
      </a-col>
    </a-row>

    <!-- 活跃租户和热门课程 -->
    <a-row :gutter="24" style="margin-top: 24px">
      <a-col :span="12">
        <a-card title="活跃租户 TOP5" :bordered="false" :loading="tenantsLoading" class="modern-card">
          <div v-if="activeTenants.length > 0" class="rank-list">
            <div v-for="(item, index) in activeTenants" :key="item.id" class="rank-item" @click="viewTenantDetail(item.id)">
              <div class="rank-number" :class="'rank-' + (index + 1)">{{ index + 1 }}</div>
              <div class="rank-content">
                <span class="rank-name">{{ item.name }}</span>
                <span class="rank-desc">教师: {{ item.teacherCount }} | 学生: {{ item.studentCount }}</span>
              </div>
              <a-tag color="blue">{{ item.lessonCount }} 次</a-tag>
            </div>
          </div>
          <a-empty v-else description="暂无数据" />
        </a-card>
      </a-col>
      <a-col :span="12">
        <a-card title="热门课程包 TOP5" :bordered="false" :loading="coursesLoading" class="modern-card">
          <div v-if="popularCourses.length > 0" class="rank-list">
            <div v-for="(item, index) in popularCourses" :key="item.id" class="rank-item" @click="viewCourseDetail(item.id)">
              <div class="rank-number" :class="'rank-' + (index + 1)">{{ index + 1 }}</div>
              <div class="rank-content">
                <span class="rank-name">{{ item.name }}</span>
                <span class="rank-desc">使用教师: {{ item.teacherCount }}</span>
              </div>
              <a-tag color="green">{{ item.usageCount }} 次</a-tag>
            </div>
          </div>
          <a-empty v-else description="暂无数据" />
        </a-card>
      </a-col>
    </a-row>

    <!-- 最近活动 -->
    <a-row :gutter="24" style="margin-top: 24px">
      <a-col :span="24">
        <a-card title="最近活动" :bordered="false" :loading="activitiesLoading" class="modern-card">
          <div v-if="recentActivities.length > 0" class="activity-timeline">
            <div v-for="activity in recentActivities" :key="activity.id" class="activity-item">
              <div class="activity-dot" :class="'type-' + activity.type"></div>
              <div class="activity-content">
                <div class="activity-header">
                  <a-tag :color="getActivityTagColor(activity.type)" class="activity-tag">
                    {{ getActivityTypeText(activity.type) }}
                  </a-tag>
                  <span class="activity-title">{{ activity.title }}</span>
                </div>
                <span class="activity-time">{{ formatTime(activity.time) }}</span>
              </div>
            </div>
          </div>
          <a-empty v-else description="暂无活动记录" />
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, nextTick } from 'vue';
import { useRouter } from 'vue-router';
import * as echarts from 'echarts';
import dayjs from 'dayjs';
// Lucide icons
import {
  Building2,
  BookOpen,
  PlayCircle,
  Users,
  Plus,
  FolderOpen,
} from 'lucide-vue-next';
import {
  getAdminStats,
  getTrendData,
  getActiveTenants,
  getPopularCourses,
  type AdminStats,
  type TrendData,
  type ActiveTenant,
  type PopularCourse,
} from '@/api/admin';

const router = useRouter();

// Chart refs
const trendChartRef = ref<HTMLElement>();
let trendChart: echarts.ECharts | null = null;

// Loading states
const trendLoading = ref(false);
const tenantsLoading = ref(false);
const coursesLoading = ref(false);
const activitiesLoading = ref(false);

// 统计数据
const statsData = ref<AdminStats>({
  tenantCount: 0,
  courseCount: 0,
  monthlyLessons: 0,
  studentCount: 0,
  lessonCount: 0,
  activeTenantCount: 0,
  publishedCourseCount: 0,
  teacherCount: 0,
});

// 活跃租户
const activeTenants = ref<ActiveTenant[]>([]);

// 热门课程包
const popularCourses = ref<PopularCourse[]>([]);

// 最近活动
const recentActivities = ref<Array<{
  id: number;
  type: string;
  title: string;
  time: string;
}>>([]);

// Activity colors
const getActivityTagColor = (type: string) => {
  const colors: Record<string, string> = {
    lesson: 'blue',
    tenant: 'green',
    course: 'orange',
  };
  return colors[type] || 'default';
};

const getActivityTypeText = (type: string) => {
  const texts: Record<string, string> = {
    lesson: '授课',
    tenant: '租户',
    course: '课程',
  };
  return texts[type] || '其他';
};

const formatTime = (time: string) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm');
};

// Navigation
const viewTenantDetail = (id: number) => {
  router.push(`/admin/tenants?id=${id}`);
};

const viewCourseDetail = (id: number) => {
  router.push(`/admin/courses/${id}`);
};

// Initialize trend chart
const initTrendChart = (data: TrendData[]) => {
  if (!trendChartRef.value) return;

  if (trendChart) {
    trendChart.dispose();
  }

  trendChart = echarts.init(trendChartRef.value);

  // 如果没有数据，显示空状态
  if (!data || data.length === 0) {
    trendChart.setOption({
      title: {
        text: '暂无数据',
        left: 'center',
        top: 'center',
        textStyle: {
          color: '#999',
          fontSize: 14,
        },
      },
    });
    return;
  }

  const option: echarts.EChartsOption = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
      },
    },
    legend: {
      data: ['授课次数', '学生数'],
      top: 0,
      right: 10,
      itemWidth: 16,
      itemHeight: 10,
      textStyle: {
        fontSize: 12,
        color: '#666',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      top: '18%',
      bottom: '8%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      data: data.map((d) => d.month),
      axisLine: {
        lineStyle: {
          color: '#E5E7EB',
        },
      },
    },
    yAxis: [
      {
        type: 'value',
        name: '授课次数',
        position: 'left',
        splitLine: {
          lineStyle: {
            color: '#F3F4F6',
          },
        },
      },
      {
        type: 'value',
        name: '学生数',
        position: 'right',
        splitLine: {
          show: false,
        },
      },
    ],
    series: [
      {
        name: '授课次数',
        type: 'bar',
        data: data.map((d) => d.lessonCount),
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#6366F1' },
            { offset: 1, color: '#4F46E5' },
          ]),
          borderRadius: [4, 4, 0, 0],
        },
      },
      {
        name: '学生数',
        type: 'line',
        yAxisIndex: 1,
        data: data.map((d) => d.studentCount),
        smooth: true,
        itemStyle: {
          color: '#10B981',
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(16, 185, 129, 0.3)' },
            { offset: 1, color: 'rgba(16, 185, 129, 0)' },
          ]),
        },
      },
    ],
  };

  trendChart.setOption(option);
};

// Fetch all data
const fetchStats = async () => {
  try {
    const stats = await getAdminStats();
    statsData.value = stats;
  } catch (error) {
    console.error('Failed to fetch stats:', error);
  }
};

const fetchTrendData = async () => {
  trendLoading.value = true;
  let trendData: TrendData[] = [];
  try {
    trendData = await getTrendData();
  } catch (error) {
    console.error('Failed to fetch trend data:', error);
  } finally {
    trendLoading.value = false;
  }
  // 在 loading 结束后初始化图表
  await nextTick();
  if (trendData && trendData.length > 0) {
    initTrendChart(trendData);
  }
};

const fetchActiveTenants = async () => {
  tenantsLoading.value = true;
  try {
    activeTenants.value = await getActiveTenants(5);
  } catch (error) {
    console.error('Failed to fetch active tenants:', error);
  } finally {
    tenantsLoading.value = false;
  }
};

const fetchPopularCourses = async () => {
  coursesLoading.value = true;
  try {
    popularCourses.value = await getPopularCourses(5);
  } catch (error) {
    console.error('Failed to fetch popular courses:', error);
  } finally {
    coursesLoading.value = false;
  }
};

const fetchRecentActivities = async () => {
  activitiesLoading.value = true;
  try {
    const response = await fetch('/api/v1/admin/stats/activities?limit=10', {
      headers: {
        Authorization: `Bearer ${localStorage.getItem('token')}`,
      },
    });
    if (response.ok) {
      recentActivities.value = await response.json();
    }
  } catch (error) {
    console.error('Failed to fetch recent activities:', error);
  } finally {
    activitiesLoading.value = false;
  }
};

// Handle window resize
const handleResize = () => {
  trendChart?.resize();
};

onMounted(() => {
  fetchStats();
  fetchTrendData();
  fetchActiveTenants();
  fetchPopularCourses();
  fetchRecentActivities();

  window.addEventListener('resize', handleResize);
});

onUnmounted(() => {
  window.removeEventListener('resize', handleResize);
  trendChart?.dispose();
});
</script>

<style scoped lang="scss">
// 现代化配色
$primary-color: #6366F1;
$primary-light: #EEF2FF;
$success-color: #10B981;
$warning-color: #F59E0B;
$pink-color: #EC4899;
$text-color: #1F2937;
$text-secondary: #6B7280;
$border-color: #E5E7EB;
$bg-light: #F9FAFB;

.dashboard {
  // 统计卡片网格
  .stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
  }

  .stat-card {
    display: flex;
    align-items: center;
    padding: 20px;
    background: white;
    border-radius: 16px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }

    .stat-icon {
      width: 56px;
      height: 56px;
      border-radius: 14px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      margin-right: 16px;
    }

    .stat-content {
      display: flex;
      flex-direction: column;

      .stat-value {
        font-size: 28px;
        font-weight: 700;
        color: $text-color;
        line-height: 1.2;
      }

      .stat-label {
        font-size: 14px;
        color: $text-secondary;
        margin-top: 4px;
      }
    }
  }

  // 现代化卡片
  .modern-card {
    border-radius: 16px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);

    :deep(.ant-card-head) {
      border-bottom: 1px solid $border-color;

      .ant-card-head-title {
        font-weight: 600;
        color: $text-color;
      }
    }
  }

  // 快捷入口
  .quick-actions {
    display: flex;
    flex-direction: column;
    gap: 12px;

    .quick-action-item {
      display: flex;
      align-items: center;
      padding: 12px 16px;
      background: $bg-light;
      border-radius: 10px;
      cursor: pointer;
      transition: all 0.25s ease;

      &:hover {
        background: $primary-light;
        transform: translateX(4px);

        .action-icon {
          background: $primary-color;
          color: white;
        }
      }

      .action-icon {
        width: 36px;
        height: 36px;
        border-radius: 10px;
        background: white;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 12px;
        color: $primary-color;
        transition: all 0.25s ease;
      }

      span {
        font-weight: 500;
        color: $text-color;
      }
    }
  }

  // 排名列表
  .rank-list {
    .rank-item {
      display: flex;
      align-items: center;
      padding: 12px 0;
      border-bottom: 1px solid $border-color;
      cursor: pointer;
      transition: all 0.25s ease;

      &:last-child {
        border-bottom: none;
      }

      &:hover {
        background: $bg-light;
        margin: 0 -16px;
        padding: 12px 16px;
        border-radius: 8px;
      }

      .rank-number {
        width: 28px;
        height: 28px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 600;
        font-size: 14px;
        margin-right: 12px;
        background: $bg-light;
        color: $text-secondary;

        &.rank-1 {
          background: linear-gradient(135deg, #F59E0B 0%, #D97706 100%);
          color: white;
        }

        &.rank-2 {
          background: linear-gradient(135deg, #9CA3AF 0%, #6B7280 100%);
          color: white;
        }

        &.rank-3 {
          background: linear-gradient(135deg, #CD7F32 0%, #B8860B 100%);
          color: white;
        }
      }

      .rank-content {
        flex: 1;
        display: flex;
        flex-direction: column;

        .rank-name {
          font-weight: 500;
          color: $text-color;
        }

        .rank-desc {
          font-size: 12px;
          color: $text-secondary;
          margin-top: 2px;
        }
      }
    }
  }

  // 活动时间线
  .activity-timeline {
    .activity-item {
      display: flex;
      align-items: flex-start;
      padding: 12px 0;
      border-bottom: 1px solid $border-color;

      &:last-child {
        border-bottom: none;
      }

      .activity-dot {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        margin-top: 6px;
        margin-right: 16px;
        flex-shrink: 0;

        &.type-lesson {
          background: $primary-color;
        }

        &.type-tenant {
          background: $success-color;
        }

        &.type-course {
          background: $warning-color;
        }
      }

      .activity-content {
        flex: 1;

        .activity-header {
          display: flex;
          align-items: center;
          gap: 8px;
          margin-bottom: 4px;

          .activity-tag {
            margin: 0;
          }

          .activity-title {
            color: $text-color;
          }
        }

        .activity-time {
          font-size: 12px;
          color: $text-secondary;
        }
      }
    }
  }
}

@media (max-width: 1200px) {
  .dashboard .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .dashboard .stats-grid {
    grid-template-columns: 1fr;
  }
}
</style>
