<template>
  <div class="home-view">
    <!-- 轮播横幅区域 -->
    <div class="banner-section">
      <!-- 加载状态 -->
      <div v-if="loading" class="banner-loading">
        <div class="loading-spinner"></div>
        <p>加载中...</p>
      </div>
      
      <!-- 轮播图 -->
      <Carousel 
        v-else-if="banners.length > 0"
        :items="banners" 
        :interval="5000"
        :active-icon="dangIcon"
        indicator-position="bottom-right"
      >
        <template #default="{ item }">
          <BannerCard :banner="item" />
        </template>
      </Carousel>
      
      <!-- 空状态 -->
      <div v-else class="banner-empty">
        <p>暂无轮播内容</p>
      </div>
    </div>

    <!-- 热门资源推荐 -->
    <div class="section">
      <div class="section-header">
        <h2 class="section-title">热门资源推荐</h2>
        <div class="more-link">
          <span>查看更多</span>
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
      <div class="article-grid">
        <HotArticleCard v-for="item in 3" :key="item" />
      </div>
    </div>

    <!-- 思政新闻概览 -->
    <div class="section">
      <div class="section-header">
        <h2 class="section-title">思政新闻概览</h2>
        <div class="more-link">
          <span>查看更多</span>
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
      <div class="article-grid">
        <IdeologicalArticleCard v-for="item in 3" :key="item" />
      </div>
    </div>

    <!-- 我的学习数据 -->
    <div class="section">
      <div class="section-header">
        <h2 class="section-title">我的学习数据</h2>
        <div class="more-link">
          <span>查看更多</span>
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
      <LearningProgress />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { BannerCard, LearningProgress } from '@/views/public/';
import { HotArticleCard, IdeologicalArticleCard } from '@/views/public/article';
import { Carousel } from '@/components/base';
import { ArrowRight } from '@element-plus/icons-vue';
import { bannerApi } from '@/apis/resource/banner';
import { ElMessage } from 'element-plus';
import type { Banner } from '@/types';
import dangIcon from '@/assets/imgs/dang.svg';

// 轮播数据
const banners = ref<Banner[]>([]);
const loading = ref(false);

// 加载轮播图数据
async function loadBanners() {
  try {
    loading.value = true;
    const result = await bannerApi.getHomeBannerList();
    
    if (result.code === 200 && result.dataList) {
      // 只显示启用状态的banner，按排序号排序
      banners.value = result.dataList
        .filter((banner: Banner) => banner.status === 1)
        .sort((a: Banner, b: Banner) => (a.orderNum || 0) - (b.orderNum || 0));
    }
  } catch (error) {
    console.error('加载轮播图失败:', error);
    ElMessage.error('加载轮播图失败');
  } finally {
    loading.value = false;
  }
}

// 组件挂载时加载数据
onMounted(() => {
  loadBanners();
});
</script>

<style lang="scss" scoped>
.home-view {
  background-color: #F9F9F9;
  min-height: 100vh;
  padding-bottom: 60px;
}

.banner-section {
  width: 100%;
  height: 30vh;
  position: relative;
}

.banner-loading,
.banner-empty {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  
  p {
    margin-top: 16px;
    color: #909399;
    font-size: 14px;
  }
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #e4e7ed;
  border-top-color: #E7000B;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.section {
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 120px;
  margin-top: 60px;

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

    .section-title {
      font-family: 'PingFang SC';
      font-weight: 600;
      font-size: 28px;
      line-height: 38px;
      color: #141F38;
      margin: 0;
    }

    .more-link {
      display: flex;
      align-items: center;
      gap: 2px;
      cursor: pointer;
      transition: color 0.3s;

      span {
        font-family: 'PingFang SC';
        font-weight: 400;
        font-size: 16px;
        line-height: 24px;
        color: rgba(20, 31, 56, 0.3);
      }

      .el-icon {
        width: 17px;
        height: 17px;
        color: rgba(20, 31, 56, 0.3);
      }

      &:hover {
        span, .el-icon {
          color: rgba(20, 31, 56, 0.6);
        }
      }
    }
  }

  .article-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 24px;
  }
}
</style>