<template>
  <div class="share-page">
    <div v-if="loading" class="loading-container">
      <van-loading size="32px">加载中...</van-loading>
    </div>
    
    <div v-else-if="activity.id" class="share-content">
      <div class="share-header" :style="{ backgroundImage: `url(${activity.coverImage || 'https://s1.xiaomiev.com/activity-outer-assets/0328/images/su7_ultra_20250227/pc/5.jpg'})` }">
        <div class="header-overlay"></div>
        <div class="header-back" @click="$router.push('/')">
          <van-icon name="arrow-left" />
        </div>
        <div class="header-info">
          <div class="club-badge">泉友米车友会</div>
          <h1 class="activity-title">{{ activity.title }}</h1>
          <div class="activity-status">
            <span class="status-badge" :class="activity.status">
              {{ activity.status === 'upcoming' ? '🔥 报名中' : '✅ 已结束' }}
            </span>
          </div>
        </div>
      </div>
      
      <div class="share-body">
        <div class="info-cards">
          <div class="info-card">
            <van-icon name="clock-o" class="card-icon" />
            <div class="card-content">
              <div class="card-label">活动时间</div>
              <div class="card-value">{{ formatDate(activity.startTime) }}</div>
            </div>
          </div>
          
          <div class="info-card">
            <van-icon name="location-o" class="card-icon" />
            <div class="card-content">
              <div class="card-label">活动地点</div>
              <div class="card-value">{{ activity.location }}</div>
            </div>
          </div>
          
          <div class="info-card">
            <van-icon name="calendar-o" class="card-icon" />
            <div class="card-content">
              <div class="card-label">报名截止</div>
              <div class="card-value">{{ formatDate(activity.registrationDeadline) }}</div>
            </div>
          </div>
        </div>
        
        <div class="description-section">
          <h3 class="section-title">
            <van-icon name="notes-o" />
            活动介绍
          </h3>
          <p class="description-text">{{ activity.description }}</p>
        </div>
        
        <div v-if="registrations.length" class="members-section">
          <h3 class="section-title">
            <van-icon name="friends-o" />
            已报名 <span class="member-count">{{ registrations.length }}人</span>
          </h3>
          <div class="member-avatars">
            <div v-for="(reg, index) in registrations.slice(0, 10)" :key="reg.id" class="member-avatar" :style="{ zIndex: 10 - index }">
              {{ reg.name.charAt(0) }}
            </div>
            <div v-if="registrations.length > 10" class="member-more">
              +{{ registrations.length - 10 }}
            </div>
          </div>
        </div>
        
        <div class="action-section">
          <van-button 
            type="primary" 
            block 
            round 
            size="large"
            @click="goToDetail"
            class="action-btn"
          >
            <van-icon name="arrow" />
            查看详情并报名
          </van-button>
          
          <div class="share-tip">
            <van-icon name="info-o" />
            点击按钮查看完整活动信息
          </div>
        </div>
      </div>
      
      <div class="share-footer">
        <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>
      </div>
    </div>
    
    <div v-else class="error-state">
      <van-empty description="活动不存在或已删除" />
      <van-button type="primary" round @click="$router.push('/')">返回首页</van-button>
    </div>
  </div>
</template>

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

const route = useRoute();
const router = useRouter();
const activity = ref({});
const registrations = ref([]);
const loading = ref(true);

const loadData = async () => {
  try {
    const activityId = route.params.id || route.query.id;
    if (!activityId) {
      loading.value = false;
      return;
    }
    
    const { data } = await api.getActivity(activityId);
    activity.value = data.activity;
    
    const { data: regs } = await api.getActivityRegistrations(activityId);
    registrations.value = regs;
  } catch (error) {
    console.error('加载失败:', error);
  } finally {
    loading.value = false;
  }
};

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

const goToDetail = () => {
  router.push(`/activity/${activity.value.id}`);
};

onMounted(loadData);
</script>

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

.loading-container,
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 20px;
  padding: 20px;
}

.share-header {
  position: relative;
  height: 320px;
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: flex-end;
  padding: 24px;
}

.header-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%);
}

.header-back {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 2;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,0.3);
  border-radius: 50%;
  color: #fff;
  font-size: 20px;
  backdrop-filter: blur(10px);
  cursor: pointer;
}

.header-info {
  position: relative;
  z-index: 1;
  width: 100%;
}

.club-badge {
  display: inline-block;
  background: rgba(255,255,255,0.25);
  backdrop-filter: blur(10px);
  color: #fff;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 12px;
  border: 1px solid rgba(255,255,255,0.3);
}

.activity-title {
  font-size: 28px;
  font-weight: bold;
  color: #fff;
  margin: 0 0 12px;
  text-shadow: 0 2px 12px rgba(0,0,0,0.4);
  line-height: 1.3;
}

.activity-status {
  display: flex;
  gap: 8px;
}

.status-badge {
  display: inline-block;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  backdrop-filter: blur(10px);
}

.status-badge.upcoming {
  background: rgba(255,107,107,0.9);
  color: #fff;
  box-shadow: 0 4px 12px rgba(255,107,107,0.4);
}

.status-badge.completed {
  background: rgba(76,175,80,0.9);
  color: #fff;
}

.share-body {
  padding: 20px 16px;
}

.info-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 24px;
}

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

.card-icon {
  font-size: 28px;
  color: #667eea;
}

.card-content {
  flex: 1;
}

.card-label {
  font-size: 13px;
  color: #999;
  margin-bottom: 4px;
}

.card-value {
  font-size: 15px;
  color: #333;
  font-weight: 500;
}

.description-section,
.members-section {
  background: #fff;
  padding: 20px;
  border-radius: 16px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
  margin-bottom: 16px;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 17px;
  font-weight: bold;
  color: #333;
  margin: 0 0 16px;
}

.section-title .van-icon {
  font-size: 20px;
  color: #667eea;
}

.member-count {
  color: #667eea;
  font-size: 15px;
}

.description-text {
  font-size: 15px;
  line-height: 1.8;
  color: #666;
  margin: 0;
  white-space: pre-line;
}

.member-avatars {
  display: flex;
  align-items: center;
}

.member-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
  border: 3px solid #fff;
  margin-left: -12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.member-avatar:first-child {
  margin-left: 0;
}

.member-more {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: #f0f0f0;
  color: #666;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  border: 3px solid #fff;
  margin-left: -12px;
}

.action-section {
  margin-top: 24px;
}

.action-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  font-size: 17px;
  font-weight: 600;
  height: 52px;
  box-shadow: 0 4px 16px rgba(102,126,234,0.4);
}

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

.share-tip {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-top: 12px;
  font-size: 13px;
  color: #999;
}

.share-tip .van-icon {
  font-size: 16px;
}

.share-footer {
  padding: 32px 16px;
  border-top: 1px solid #f0f0f0;
  background: #fff;
}

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

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

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

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

.logo-slogan {
  font-size: 13px;
  color: #999;
  letter-spacing: 2px;
}
</style>
