<template>
  <div class="landing-page">
    <!-- 背景音乐控制 -->
    <div class="music-control" @click="toggleMusic">
      <van-icon :name="isPlaying ? 'volume-o' : 'volume'" :class="{ playing: isPlaying }" />
    </div>
    <audio ref="audioPlayer" loop>
      <source src="/are-you-ok.mp3" type="audio/mpeg">
    </audio>

    <!-- 顶部导航栏 -->
    <div class="top-nav">
      <van-icon name="arrow-left" class="nav-back" @click="$router.push('/')" />
      <span class="nav-title">小米汽车车友会</span>
      <div class="nav-placeholder"></div>
    </div>

    <!-- Hero轮播区 -->
    <div class="hero-section">
      <van-swipe class="hero-swipe" :autoplay="4000" indicator-color="#FF6700">
        <van-swipe-item v-for="(image, index) in heroImages" :key="index">
          <div class="swipe-item" :style="{ backgroundImage: `url(${image})` }">
            <div class="swipe-overlay"></div>
          </div>
        </van-swipe-item>
      </van-swipe>
      
      <div class="hero-content">
        <div class="hero-badge">小米汽车车友会</div>
        <h1 class="hero-title">驾驭未来 · 智享生活</h1>
        <p class="hero-subtitle">Are You OK? 一起探索小米汽车的无限可能</p>
        <div class="hero-stats">
          <div class="stat-item">
            <div class="stat-number">1000+</div>
            <div class="stat-label">车友</div>
          </div>
          <div class="stat-divider"></div>
          <div class="stat-item">
            <div class="stat-number">50+</div>
            <div class="stat-label">活动</div>
          </div>
          <div class="stat-divider"></div>
          <div class="stat-item">
            <div class="stat-number">20+</div>
            <div class="stat-label">城市</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 特色卡片区 -->
    <div class="features-section">
      <h2 class="section-title">
        <span class="title-icon">✨</span>
        车友会特色
      </h2>
      
      <div class="feature-cards">
        <div 
          v-for="(feature, index) in features" 
          :key="index"
          class="feature-card"
          :style="{ backgroundImage: `url(${feature.image})` }"
        >
          <div class="card-overlay"></div>
          <div class="card-content">
            <div class="card-icon">{{ feature.icon }}</div>
            <h3 class="card-title">{{ feature.title }}</h3>
            <p class="card-desc">{{ feature.desc }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 活动预览区 -->
    <div class="activities-section">
      <h2 class="section-title">
        <span class="title-icon">🎯</span>
        精彩活动
      </h2>
      
      <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-preview">
        <div
          v-for="activity in activities.slice(0, 3)"
          :key="activity.id"
          class="activity-card"
          @click="$router.push(`/activity/${activity.id}`)"
        >
          <div class="activity-image" :style="{ backgroundImage: `url(${activity.coverImage || heroImages[0]})` }">
            <div class="activity-badge">报名中</div>
          </div>
          <div class="activity-info">
            <h3 class="activity-title">{{ activity.title }}</h3>
            <div class="activity-meta">
              <van-icon name="clock-o" />
              <span>{{ formatDate(activity.startTime) }}</span>
            </div>
            <div class="activity-meta">
              <van-icon name="location-o" />
              <span>{{ activity.location }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <van-button 
        type="primary" 
        block 
        round 
        size="large"
        @click="$router.push('/')"
        class="view-more-btn"
      >
        查看全部活动
        <van-icon name="arrow" />
      </van-button>
    </div>

    <!-- 赞助商区域 -->
    <div class="sponsors-section">
      <h2 class="section-title">
        <span class="title-icon">🤝</span>
        合作伙伴
      </h2>
      
      <div class="sponsors-grid">
        <div class="sponsor-card">
          <div class="sponsor-logo xiaomi-logo">
            <svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
              <rect x="10" y="10" width="80" height="80" rx="20" fill="#FF6700"/>
              <text x="50" y="65" font-family="Arial, sans-serif" font-size="40" font-weight="bold" fill="white" text-anchor="middle">MI</text>
            </svg>
          </div>
          <div class="sponsor-info">
            <h3 class="sponsor-name">小米汽车泉州信兴汽车城授权服务中心</h3>
            <p class="sponsor-desc">泉州德劲新能源科技有限公司</p>
          </div>
        </div>
        
        <div class="sponsor-card">
          <div class="sponsor-logo europark-logo">
            <svg viewBox="0 0 200 80" xmlns="http://www.w3.org/2000/svg">
              <defs>
                <linearGradient id="europark-gradient" x1="0%" y1="0%" x2="100%" y2="100%">
                  <stop offset="0%" style="stop-color:#4A90E2;stop-opacity:1" />
                  <stop offset="100%" style="stop-color:#7B68EE;stop-opacity:1" />
                </linearGradient>
              </defs>
              <rect width="200" height="80" rx="12" fill="url(#europark-gradient)"/>
              <text x="100" y="50" font-family="Microsoft YaHei, Arial, sans-serif" font-size="32" font-weight="bold" fill="white" text-anchor="middle">欧乐堡</text>
            </svg>
          </div>
          <div class="sponsor-info">
            <h3 class="sponsor-name">泉州欧乐堡</h3>
            <p class="sponsor-desc">童话世界 · 海洋王国</p>
          </div>
        </div>
      </div>
    </div>

    <!-- CTA区域 -->
    <div class="cta-section">
      <div class="cta-content">
        <h2 class="cta-title">加入我们</h2>
        <p class="cta-desc">与千位小米汽车车主一起，探索智能出行新体验</p>
        <van-button 
          type="primary" 
          round 
          size="large"
          @click="$router.push('/')"
          class="cta-btn"
        >
          立即加入车友会
        </van-button>
      </div>
      <div class="cta-bg"></div>
    </div>

    <!-- 页脚 -->
    <footer class="footer">
      <div class="footer-content">
        <div class="footer-logo">
          <div class="logo-icon">🚗</div>
          <div class="logo-text">
            <div class="logo-name">小米汽车车友会</div>
            <div class="logo-slogan">驾驭未来 · 智享生活</div>
          </div>
        </div>
        <a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener noreferrer" class="beian-link">
          闽ICP备2021011414号-1
        </a>
      </div>
    </footer>
  </div>
</template>

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

const router = useRouter();
const audioPlayer = ref(null);
const isPlaying = ref(false);
const activities = ref([]);
const loading = ref(false);

// Hero轮播图 - 小米汽车官方高清图片
const heroImages = 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',
  'https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_ultra_20250227/pc/4.jpg'
]);

// 特色功能 - 使用小米汽车真实图片
const features = ref([
  {
    icon: '🚀',
    title: '极致性能',
    desc: '零百加速2.78秒，800V超级快充，续航800km+',
    image: 'https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_ultra_20250227/pc/5.jpg'
  },
  {
    icon: '🤝',
    title: '车友互动',
    desc: '定期组织自驾游、车友聚会、技术交流等活动',
    image: 'https://s1.xiaomiev.com/activity-outer-assets/0328/images/yu7_20250522/pc/3.jpg'
  },
  {
    icon: '🎁',
    title: '专属福利',
    desc: '车友专属优惠、保养折扣、配件优惠等福利',
    image: 'https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_20260319/pc/8-1.png'
  },
  {
    icon: '🌟',
    title: '智能生态',
    desc: '小米全生态互联，打造智能出行新体验',
    image: 'https://s1.xiaomiev.com/activity-outer-assets/0328/images/yu7_20250626/pc/15.png'
  }
]);

// 音乐控制
const toggleMusic = () => {
  if (!audioPlayer.value) return;
  
  if (isPlaying.value) {
    audioPlayer.value.pause();
    isPlaying.value = false;
  } else {
    audioPlayer.value.play().catch(err => {
      console.log('音乐播放需要用户交互:', err);
    });
    isPlaying.value = true;
  }
};

// 尝试自动播放音乐(需要用户交互)
const tryAutoPlay = () => {
  if (!audioPlayer.value || isPlaying.value) return;
  
  audioPlayer.value.play().then(() => {
    isPlaying.value = true;
    // 移除事件监听
    document.removeEventListener('click', tryAutoPlay);
    document.removeEventListener('touchstart', tryAutoPlay);
  }).catch(() => {
    // 自动播放失败,等待用户点击
  });
};

// 加载活动
const loadActivities = async () => {
  loading.value = true;
  try {
    const { data } = await api.getActivities({ status: 'upcoming' });
    activities.value = data;
  } catch (error) {
    console.error('加载活动失败:', error);
  }
  loading.value = false;
};

const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN', {
    month: '2-digit',
    day: '2-digit'
  });
};

onMounted(() => {
  loadActivities();
  
  // 监听用户交互,尝试自动播放音乐
  document.addEventListener('click', tryAutoPlay, { once: true });
  document.addEventListener('touchstart', tryAutoPlay, { once: true });
  
  // 3秒后如果还没播放,尝试一次
  setTimeout(() => {
    if (!isPlaying.value) {
      tryAutoPlay();
    }
  }, 3000);
});

onUnmounted(() => {
  if (audioPlayer.value && isPlaying.value) {
    audioPlayer.value.pause();
  }
  // 清理事件监听
  document.removeEventListener('click', tryAutoPlay);
  document.removeEventListener('touchstart', tryAutoPlay);
});
</script>

<style scoped>
.landing-page {
  min-height: 100vh;
  background: #fff;
}

/* 顶部导航栏 */
.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 46px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 999;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.nav-back {
  font-size: 20px;
  color: #333;
  cursor: pointer;
  padding: 8px;
}

.nav-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.nav-placeholder {
  width: 36px;
}

/* 音乐控制 */
.music-control {
  position: fixed;
  top: 60px;
  right: 20px;
  z-index: 1000;
  width: 44px;
  height: 44px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  color: #FF6700;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.music-control:hover {
  background: rgba(255, 255, 255, 1);
  box-shadow: 0 4px 16px rgba(255, 103, 0, 0.2);
}

.music-control:active {
  transform: scale(0.95);
}

.music-control .van-icon {
  font-size: 22px;
  transition: transform 0.3s;
}

.music-control .van-icon.playing {
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.music-text {
  font-size: 14px;
  font-weight: 500;
}

/* Hero区域 */
.hero-section {
  position: relative;
  height: 100vh;
  min-height: 600px;
  overflow: hidden;
  margin-top: 46px;
}

.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-overlay {
  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.6) 100%);
}

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

.hero-badge {
  display: inline-block;
  background: rgba(255, 103, 0, 0.9);
  padding: 8px 24px;
  border-radius: 30px;
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 24px;
  letter-spacing: 2px;
  box-shadow: 0 4px 16px rgba(255, 103, 0, 0.4);
  animation: fadeInDown 1s;
}

.hero-title {
  font-size: 48px;
  font-weight: bold;
  margin: 0 0 16px;
  text-shadow: 0 4px 16px rgba(0,0,0,0.3);
  letter-spacing: 4px;
  animation: fadeInUp 1s 0.2s both;
}

.hero-subtitle {
  font-size: 18px;
  margin: 0 0 48px;
  opacity: 0.95;
  letter-spacing: 2px;
  animation: fadeInUp 1s 0.4s both;
}

.hero-stats {
  display: flex;
  gap: 32px;
  align-items: center;
  animation: fadeInUp 1s 0.6s both;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 36px;
  font-weight: bold;
  color: #FF6700;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 14px;
  opacity: 0.9;
}

.stat-divider {
  width: 1px;
  height: 40px;
  background: rgba(255,255,255,0.3);
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 特色区域 */
.features-section,
.activities-section {
  padding: 80px 16px;
  max-width: 1200px;
  margin: 0 auto;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 32px;
  font-weight: bold;
  color: #333;
  margin: 0 0 48px;
  text-align: center;
}

.title-icon {
  font-size: 36px;
}

.feature-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.feature-card {
  position: relative;
  height: 320px;
  border-radius: 24px;
  overflow: hidden;
  background-size: cover;
  background-position: center;
  cursor: pointer;
  transition: transform 0.3s, box-shadow 0.3s;
}

.feature-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 32px rgba(0,0,0,0.15);
}

.card-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, rgba(0,0,0,0.2) 0%, rgba(0,0,0,0.7) 100%);
}

.card-content {
  position: relative;
  z-index: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 24px;
  color: #fff;
}

.card-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.card-title {
  font-size: 24px;
  font-weight: bold;
  margin: 0 0 12px;
}

.card-desc {
  font-size: 14px;
  line-height: 1.6;
  opacity: 0.95;
  margin: 0;
}

/* 活动预览 */
.activity-preview {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

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

.activity-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
}

.activity-image {
  height: 200px;
  background-size: cover;
  background-position: center;
  position: relative;
}

.activity-badge {
  position: absolute;
  top: 16px;
  right: 16px;
  background: #FF6700;
  color: #fff;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(255, 103, 0, 0.3);
}

.activity-info {
  padding: 20px;
}

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

.activity-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.activity-meta .van-icon {
  color: #FF6700;
}

.view-more-btn {
  background: linear-gradient(135deg, #FF6700 0%, #FF8533 100%);
  border: none;
  font-size: 17px;
  font-weight: 600;
  height: 52px;
  box-shadow: 0 4px 16px rgba(255, 103, 0, 0.3);
}

.view-more-btn .van-icon {
  margin-left: 8px;
}

/* 赞助商区域 */
.sponsors-section {
  padding: 60px 16px;
  max-width: 900px;
  margin: 0 auto;
  background: #fafafa;
}

.sponsors-grid {
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: 16px;
  margin-top: 32px;
  flex-wrap: wrap;
}

.sponsor-card {
  background: #fff;
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
  transition: transform 0.3s, box-shadow 0.3s;
  flex: 0 1 140px;
  max-width: 150px;
  min-width: 120px;
}

.sponsor-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

.sponsor-logo {
  width: 45px;
  height: 45px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
}

.sponsor-logo svg {
  width: 100%;
  height: 100%;
}

.xiaomi-logo svg {
  filter: drop-shadow(0 2px 8px rgba(255, 103, 0, 0.2));
}

.europark-logo svg {
  filter: drop-shadow(0 2px 8px rgba(74, 144, 226, 0.2));
}

.sponsor-info {
  width: 100%;
}

.sponsor-name {
  font-size: 11px;
  font-weight: 600;
  color: #333;
  margin: 0 0 4px;
  line-height: 1.3;
  word-break: break-all;
}

.sponsor-desc {
  font-size: 10px;
  color: #999;
  margin: 0;
  line-height: 1.4;
  word-break: break-all;
}

/* CTA区域 */
.cta-section {
  position: relative;
  padding: 100px 20px;
  text-align: center;
  overflow: hidden;
  background: linear-gradient(135deg, #FF6700 0%, #FF8533 100%);
}

.cta-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0.1;
  background-image: url('https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_ultra_20250227/pc/7-1-1.jpeg');
  background-size: cover;
  background-position: center;
}

.cta-content {
  position: relative;
  z-index: 1;
  max-width: 600px;
  margin: 0 auto;
}

.cta-title {
  font-size: 40px;
  font-weight: bold;
  color: #fff;
  margin: 0 0 16px;
}

.cta-desc {
  font-size: 18px;
  color: rgba(255,255,255,0.95);
  margin: 0 0 32px;
  line-height: 1.6;
}

.cta-btn {
  background: #fff;
  color: #FF6700;
  font-size: 17px;
  font-weight: 600;
  height: 52px;
  padding: 0 48px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}

/* 页脚 */
.footer {
  background: #1a1a1a;
  padding: 48px 20px;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.footer-logo {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo-icon {
  font-size: 48px;
}

.logo-text {
  text-align: left;
}

.logo-name {
  font-size: 20px;
  font-weight: bold;
  color: #fff;
  margin-bottom: 4px;
}

.logo-slogan {
  font-size: 14px;
  color: #999;
  letter-spacing: 2px;
}

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

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

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

/* 响应式 */
@media (max-width: 768px) {
  .hero-title {
    font-size: 32px;
  }
  
  .hero-subtitle {
    font-size: 16px;
  }
  
  .hero-stats {
    gap: 20px;
  }
  
  .stat-number {
    font-size: 28px;
  }
  
  .section-title {
    font-size: 28px;
  }
  
  .feature-cards {
    grid-template-columns: 1fr;
  }
  
  .activity-preview {
    grid-template-columns: 1fr;
  }
  
  .cta-title {
    font-size: 32px;
  }
  
  .sponsors-grid {
    gap: 12px;
  }
  
  .sponsor-card {
    flex: 0 1 calc(20% - 10px);
    min-width: 100px;
    max-width: 130px;
    padding: 10px;
  }
  
  .sponsor-logo {
    width: 40px;
    height: 40px;
    margin-bottom: 6px;
  }
  
  .sponsor-name {
    font-size: 10px;
  }
  
  .sponsor-desc {
    font-size: 9px;
  }
}
</style>
