<template>
  <div class="home">
    <div class="hero">
      <van-swipe class="hero-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="(image, index) in bannerImages" :key="index">
          <div class="swipe-item" :style="{ backgroundImage: `url(${image})` }"></div>
        </van-swipe-item>
      </van-swipe>
      <div class="hero-content">
        <h1 class="club-name">泉友米车友会</h1>
        <p class="club-slogan">驾驭激情 · 共享旅程</p>
        <van-button 
          type="primary" 
          round 
          size="small"
          @click="$router.push('/landing')"
          class="landing-btn"
        >
          查看宣传页
        </van-button>
      </div>
    </div>
    
    <van-tabs v-model:active="activeTab" @change="loadActivities" class="custom-tabs">
      <van-tab title="精彩活动" name="upcoming"></van-tab>
      <van-tab title="往期回顾" name="completed"></van-tab>
    </van-tabs>
    
    <div class="activity-list">
      <div v-if="loading" class="loading-state">
        <van-loading size="24px">加载中...</van-loading>
      </div>
      <div v-else-if="activities.length === 0" class="empty-state">
        <van-empty description="暂无活动" />
      </div>
      <div v-else class="activity-cards">
        <div
          v-for="activity in activities"
          :key="activity.id"
          class="activity-card"
          @click="$router.push(`/activity/${activity.id}`)"
        >
          <div class="card-image" :style="{ backgroundImage: `url(${activity.coverImage || 'https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_ultra_20250227/pc/5.jpg'})` }">
            <div class="card-badge">{{ activity.status === 'upcoming' ? '报名中' : '已结束' }}</div>
          </div>
          <div class="card-content">
            <h3 class="card-title">{{ activity.title }}</h3>
            <p class="card-desc">{{ activity.description }}</p>
            <div class="card-meta">
              <div class="meta-item">
                <van-icon name="clock-o" />
                <span>{{ formatDate(activity.startTime) }}</span>
              </div>
              <div class="meta-item">
                <van-icon name="location-o" />
                <span>{{ activity.location }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <footer class="footer">
      <a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener noreferrer" class="beian-link">
        闽ICP备2021011414号-1
      </a>
    </footer>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import api from '../api';

const router = useRouter();
const activeTab = ref('upcoming');
const activities = ref([]);
const loading = ref(false);
const finished = ref(false);

// 轮播图图片 - 小米汽车官方高清图片
const bannerImages = ref([
  'https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_20260319/pc/2-1.png',
  'https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_ultra_20250227/pc/1.jpg',
  'https://s1.xiaomiev.com/activity-outer-assets/0328/images/yu7_20250626/pc/1.png',
  'https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_20260319/pc/4-1.png'
]);

const loadActivities = async () => {
  loading.value = true;
  try {
    const { data } = await api.getActivities({ status: activeTab.value });
    activities.value = data;
    finished.value = true;
  } catch (error) {
    console.error(error);
  }
  loading.value = false;
};

const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN');
};

loadActivities();
</script>

<style scoped>
.home {
  min-height: 100vh;
  background: linear-gradient(180deg, #f8f9fa 0%, #fff 100%);
}

.hero {
  position: relative;
  height: 240px;
  overflow: hidden;
}

.hero-swipe {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100%;
}

.swipe-item {
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  position: relative;
}

.swipe-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.5) 100%);
}

.swipe-item::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  right: 0;
  height: 60px;
  background: #f8f9fa;
  border-radius: 50% 50% 0 0 / 100% 100% 0 0;
}

.hero-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #fff;
  text-align: center;
}

.club-name {
  font-size: 32px;
  font-weight: bold;
  margin: 0;
  text-shadow: 0 2px 8px rgba(0,0,0,0.2);
  letter-spacing: 2px;
}

.club-slogan {
  font-size: 14px;
  margin: 12px 0 0;
  opacity: 0.9;
  letter-spacing: 4px;
}

.landing-btn {
  margin-top: 20px;
  background: linear-gradient(135deg, #FF6700 0%, #FF8533 100%);
  border: none;
  font-size: 14px;
  font-weight: 600;
  padding: 0 24px;
  box-shadow: 0 4px 12px rgba(255, 103, 0, 0.4);
}

.custom-tabs {
  margin-top: -30px;
  position: relative;
  z-index: 2;
}

.activity-list {
  padding: 16px;
}

.loading-state,
.empty-state {
  padding: 60px 0;
  text-align: center;
}

.activity-cards {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.activity-card {
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
  transition: transform 0.3s, box-shadow 0.3s;
}

.activity-card:active {
  transform: scale(0.98);
}

.card-image {
  height: 180px;
  background-size: cover;
  background-position: center;
  background-color: #e8e8e8;
  position: relative;
}

.card-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: linear-gradient(135deg, #FF6700 0%, #FF8533 100%);
  color: #fff;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
  box-shadow: 0 2px 8px rgba(255, 103, 0, 0.3);
}

.card-content {
  padding: 16px;
}

.card-title {
  font-size: 18px;
  font-weight: bold;
  margin: 0 0 8px;
  color: #333;
}

.card-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 12px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-meta {
  display: flex;
  gap: 16px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #999;
}

.meta-item .van-icon {
  font-size: 14px;
  color: #FF6700;
}

.footer {
  padding: 24px 16px;
  text-align: center;
  background: #fff;
  border-top: 1px solid #f0f0f0;
  margin-top: 32px;
}

.beian-link {
  font-size: 13px;
  color: #999;
  text-decoration: none;
  transition: color 0.3s ease;
}

.beian-link:hover {
  color: #FF6700;
}

.beian-link:active {
  color: #FF8533;
}
</style>
