<template>
  <section class="scene-section end-section" :class="{ 'active': isActive }">
    <!-- 背景图片层 -->
    <div class="background-layer">
      <img src="/static/bg/bg_finish.jpg" alt="结束页背景" class="background-image" />
    </div>
    
    <!-- 内容层 -->
    <div class="content-layer">
      <div class="end-page-content">
        <!-- 上方标题图片 -->
        <image 
          src="/static/images/finish_title2.png" 
          mode="widthFix" 
          class="finish-title-top"
        ></image>
        
        <!-- 福印收集展示区域 -->
        <div class="seal-collection-display">
          <!-- 前门 - 左上角 -->
          <div class="gift-item gift-pos-1" :class="{ collected: collectedSeals.qianmen }">
            <image 
              src="/static/images/gift1.png" 
              mode="widthFit" 
              class="gift-image"
            ></image>
          </div>
          
          <!-- 崇文门 - 右上角 -->
          <div class="gift-item gift-pos-2" :class="{ collected: collectedSeals.chongwen }">
            <image 
              src="/static/images/gift2.png" 
              mode="widthFit" 
              class="gift-image"
            ></image>
          </div>
          
          <!-- 王府井 - 左下角 -->
          <div class="gift-item gift-pos-3" :class="{ collected: collectedSeals.wangfujing }">
            <image 
              src="/static/images/gift3.png" 
              mode="widthFit" 
              class="gift-image"
            ></image>
          </div>
          
          <!-- 隆福寺 - 底部中间 -->
          <div class="gift-item gift-pos-4" :class="{ collected: collectedSeals.longfusi }">
            <image 
              src="/static/images/gift4.png" 
              mode="widthFit" 
              class="gift-image"
            ></image>
          </div>
          
          <!-- 东直门 - 右下角 -->
          <div class="gift-item gift-pos-5" :class="{ collected: collectedSeals.dongzhimen }">
            <image 
              src="/static/images/gift5.png" 
              mode="widthFit" 
              class="gift-image"
            ></image>
          </div>
        </div>
        
        <!-- 功能按钮区域 -->
        <div class="end-buttons">
          <image 
            src="/static/images/btn_lottery.png" 
            mode="aspectFit" 
            class="function-image lottery-image"
            @click="handleLottery"
          ></image>
          <image 
            src="/static/images/btn_ai.png" 
            mode="aspectFit" 
            class="function-image couplet-image"
            @click="handleCouplet"
          ></image>
        </div>
        
        <!-- 下方标题图片 -->
        <image 
          src="/static/images/finish_title.png" 
          mode="widthFix" 
          class="finish-title-bottom"
        ></image>

      </div>
    </div>
  </section>
</template>

<script setup>
import { onMounted } from 'vue'
import ImagePreloader from '@/utils/preload'

const props = defineProps({
  isActive: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    required: true
  },
  collectionProgress: {
    type: Number,
    default: 0
  },
  collectedCount: {
    type: Number,
    default: 0
  },
  totalCount: {
    type: Number,
    default: 5
  },
  collectedSeals: {
    type: Object,
    default: () => ({
      qianmen: false,
      chongwen: false,
      wangfujing: false,
      longfusi: false,
      dongzhimen: false
    })
  },
  hasSubmitted: {
    type: Boolean,
    default: false
  },
  generatedCouplet: {
    type: Object,
    default: () => null
  }
})

const emit = defineEmits(['lottery', 'couplet', 'showCouplet'])

const handleLottery = () => {
  // 如果已经提交过，直接提示
  if (props.hasSubmitted) {
    uni.showToast({
      title: '您已经提交过信息了，请勿重复提交',
      icon: 'none',
      duration: 2000
    })
    return
  }
  emit('lottery')
}

const handleCouplet = () => {
  // 如果已经生成过海报，直接显示海报
  if (props.generatedCouplet && props.generatedCouplet.image_url) {
    emit('showCouplet')
    return
  }
  emit('couplet')
}



// 预加载图片
const preloadImages = async () => {
  try {
    const imageUrls = [
      // EndPage 图片
      '/static/bg/bg_finish.jpg',
      '/static/images/finish_title2.png',
      '/static/images/finish_title.png',
      '/static/images/gift1.png',
      '/static/images/gift2.png',
      '/static/images/gift3.png',
      '/static/images/gift4.png',
      '/static/images/gift5.png',
      '/static/images/btn_lottery.png',
      '/static/images/btn_ai.png',
      // AICoupletForm 图片
      '/static/info/couplet_info_box.png',
      '/static/images/btn_submit.png',
      '/static/images/btn_close.png',
      // LotteryFormModal 图片
      '/static/info/info_bg.jpg',
      '/static/images/btn_back.png',
      '/static/info/info_title.png',
      '/static/info/info_tips.png'
    ]
    await ImagePreloader.preloadAll(imageUrls)
    console.log('EndPage 图片预加载完成')
  } catch (error) {
    console.error('EndPage 图片预加载失败:', error)
  }
}

// 组件挂载时预加载图片
onMounted(() => {
  preloadImages()
})
</script>

<style scoped>
.scene-section {
  position: relative;
  width: 100%;
}

.scene-section.active {
  /* 当前活动场景样式 */
  opacity: 1;
}

/* 背景图片层 */
.background-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.background-image {
  width: 100%;
  height: 100%;
  object-fit: cover; /* 使用裁切方式展示，保持图片比例 */
  object-position: center center; /* 从中心开始裁切 */
}

/* 内容层 */
.content-layer {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 40px;
}

.end-page-content {
  width: 100%;
  box-sizing: border-box;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.end-title {
  font-size: 32px;
  color: #FF6B35;
  margin-bottom: 30px;
}

.seal-collection-section {
  margin-bottom: 40px;
}

.section-title {
  font-size: 24px;
  color: #333;
  margin-bottom: 20px;
}

.collection-progress {
  margin-bottom: 20px;
}

.progress-bar {
  width: 100%;
  height: 10px;
  background-color: #ddd;
  border-radius: 5px;
  overflow: hidden;
  margin-bottom: 10px;
}

.progress-fill {
  height: 100%;
  background-color: #FF6B35;
  transition: width 0.5s ease;
}

.progress-text {
  font-size: 16px;
  color: #666;
  margin: 0;
}

.end-buttons {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-bottom: 60px;
}

.function-btn {
  cursor: pointer;
  width: 306rpx;
  height: 97rpx;
}

.lottery-btn {
  background-color: #FFD700;
  color: #333;
}

.lottery-btn:hover {
  background-color: #FFC107;
  transform: translateY(-2px);
}

.couplet-btn {
  background-color: #4CAF50;
  color: white;
}

.couplet-btn:hover {
  background-color: #45a049;
  transform: translateY(-2px);
}

.restart-btn-container {
  margin-top: 20px;
}

.restart-btn {
  padding: 10px 20px;
  font-size: 16px;
  background-color: #999;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.restart-btn:hover {
  background-color: #777;
}

/* 福印收集展示区域 - 绝对定位布局 */
.seal-collection-display {
  position: relative;
  width: 100%;
  height: 600rpx;
  margin: 80rpx 0;
}

/* 福印基础样式 */
.gift-item {
  position: absolute;
  transition: all 0.3s ease;
  opacity: 0.4;
  filter: grayscale(100%);
}

.gift-item.collected {
  opacity: 1;
  filter: grayscale(0%);
  transform: scale(1.05);
}

.gift-image {
  width: 100%;
  height: 100%;
}

/* 前门 - 左上位置 */
.gift-pos-1 {
  width: 237rpx;
  height: 233rpx;
  left: 80rpx;
  top: 0;
}

/* 崇文门 - 右上位置 */
.gift-pos-2 {
  width: 344rpx;
  height: 343rpx;
  right: 80rpx;
  top: -64rpx;
}

/* 王府井 - 左下位置 */
.gift-pos-3 {
  width: 268rpx;
  height: 254rpx;
  left: 40rpx;
  bottom: 0rpx;
}

/* 隆福寺 - 底部中间位置 */
.gift-pos-4 {
  width: 153rpx;
  height: 350rpx;
  left: calc(50% + 20rpx);
  bottom: 0;
  transform: translateX(-50%);
}

.gift-pos-4.collected {
  transform: translateX(-50%) scale(1.05);
}

/* 东直门 - 右下位置 */
.gift-pos-5 {
  width: 238rpx;
  height: 282rpx;
  right: 40rpx;
  bottom: 0;
}

.gift-label {
  font-size: 12px;
  color: #333;
  margin: 0;
  font-weight: bold;
}

.collection-status {
  font-size: 16px;
  color: #FF6B35;
  font-weight: bold;
  text-align: center;
  margin: 0;
}

/* 标题图片 */
.finish-title-top {
  width: 747rpx;
  margin-top: 110rpx;
  display: block;
}

.finish-title-bottom {
  width: 733rpx;
  margin-top: 30rpx;
  display: block;
}

/* 功能按钮图片：假设原图约400px*120px，使用rpx单位 */
.function-image {
  cursor: pointer;
  transition: all 0.3s ease;
  width: 306rpx;
  height: 97rpx;
}

.function-image:active {
  transform: scale(0.95);
}

/* 响应式相关样式已移除 - 本页面仅用于手机端 */
/* 如需调整各元素位置，请直接修改对应选择器的样式 */
</style>
