<template>
  <view class="activity-detail">
    <!-- 轮播图 -->
    <view class="banner-container">
      <swiper class="banner-swiper" height="450rpx" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="500">
        <!-- 如果没有,分割 -->
        <swiper-item v-for="(image, index) in imageList" :key="index">
          <image class="banner-image" :src="image" mode="aspectFill"></image>
        </swiper-item>
      </swiper>
    </view>
    
    <!-- 活动信息 -->
    <view class="activity-info">
      <!-- 活动标题和标签 -->
      <view class="title-section">
        <view class="activity-badge">
          <text class="badge-text">{{ activityData.score }}积分</text>
        </view>
        <text class="activity-title">{{ activityData.title }}</text>
      </view>
      
      <!-- 活动详细信息 -->
      <view class="info-section">
        <view class="info-item">
          <uv-icon name="calendar" size="16" color="#666"></uv-icon>
          <text class="info-label">活动时间：</text>
          <text class="info-value">{{ activityData.activityTime }}</text>
        </view>
        
        <view class="info-item">
          <uv-icon name="clock" size="16" color="#666"></uv-icon>
          <text class="info-label">报名时间：</text>
          <text class="info-value">{{ activityData.startTime }}</text>
        </view>
        
        <view class="info-item">
          <uv-icon name="account-fill" size="16" color="#666"></uv-icon>
          <text class="info-label">联系人：</text>
          <text class="info-value">{{ activityData.contact }}</text>
        </view>
        
        <view class="info-item">
          <uv-icon name="phone" size="16" color="#666"></uv-icon>
          <text class="info-label">取消规则：</text>
          <text class="info-value">{{ activityData.rule }}</text>
        </view>
        
        <view class="info-item">
          <uv-icon name="map-fill" size="16" color="#666"></uv-icon>
          <text class="info-label">活动地点：</text>
          <text class="info-value">{{ activityData.address }}</text>
        </view>
      </view>
      
      <!-- 活动详情 -->
      <view class="detail-section">
        <view class="section-title">
          <text class="title-text">活动详情</text>
        </view>
        <view class="detail-content">
          <!-- <text class="detail-text"> -->
            <rich-text :nodes="activityData.details"></rich-text>
          <!-- </text> -->
        </view>
      </view>
      
      <!-- 活动图集 -->
      <view class="gallery-section">
        <view class="section-title">
          <text class="title-text">活动图集</text>
        </view>
        <view class="gallery-grid">
          <image 
            v-for="(image, index) in atlas" 
            :key="index"
            class="gallery-image" 
            :src="image" 
            mode="aspectFill"
            @click="previewImage(image, atlas)"
          ></image>
          <!-- <uv-album :urls="activityData.gallery"></uv-album> -->
        </view>
      </view>
    </view>
    
    <!-- 固定底部操作栏 -->
    <view class="bottom-action">
      <view class="action-left">
        <button open-type="share">
          <view class="action-item">
            <uv-icon name="share" size="24" color="#000"></uv-icon>
            <text class="action-text">分享</text>
          </view>
        </button>
        <view class="action-item" @click="collectActivity">
          <uv-icon name="heart-fill" class="collection-icon" size="24" :color="activityData.isCollection === 1 ? '#ff4757' : '#999'"></uv-icon>
          <text class="action-text">收藏</text>
        </view>
        <view class="action-item">
          <text class="participants-count">
            <text :style="{'color': activityData.numActivity >= activityData.numLimit ? '#999' : '#1488DB'}">{{ activityData.numActivity }}</text>
            /{{ activityData.numLimit }}</text>
          <text class="action-text">已报名</text>
        </view>
      </view>
      <view class="action-right">
        <uv-button 
          v-if="activityData.status === '1'"
          type="primary" 
          size="normal" 
          text="已结束"
          shape="circle"
          @click="signUpActivity"
          :disabled="true"
        ></uv-button>
        <uv-button 
          v-else-if="activityData.isApply === 1"
          type="primary" 
          size="normal" 
          text="您已报名"
          shape="circle"
          @click="signUpActivity"
          :disabled="true"
        ></uv-button>
        <uv-button 
          v-else
          type="primary" 
          size="normal" 
          text="我要报名"
          shape="circle"
          @click="signUpActivity"
          :disabled="activityData.numActivity >= activityData.numLimit "
        ></uv-button>
      </view>
    </view>
    <SignUpForm 
      ref="signUpFormRef"
      @close="onSignUpFormClose" 
      @submit="onSignUpFormSubmit"
    />
    <GlobalPopup ref="globalPopupRef"></GlobalPopup>
  </view>
  
  <!-- 报名表单弹窗 -->
</template>

<script>
import SignUpForm from '@/subPages/index/components/SignUpForm.vue'


export default {
  components: {
    SignUpForm
  },
  data() {
    return {
      // isCollected: false,
      showSignUpForm: false,

      activityData: {
        title: '关爱自闭症儿童活动',
        duration: '30积分',
        time: '2025-06-12 14:30',
        registrationTime: '2025-06-01 14:30——2025-09-01 14:30',
        contact: '柳老师 (13256484512)',
        cancelRule: '报名随时可取消',
        location: '长沙市雨花区时代阳光大夏国际大厅2145',
        registeredCount: 9,
        maxCount: 30,
        details: [
          '身体健康，热爱志愿服务工作，富有责任感和奉献精神',
          '遵纪守法，思想上进，作风正派，服从安排',
          '年龄在60岁以下，具备广告宣传理能力'
        ],
        gallery: [
          '/static/bannerImage.png',
          '/static/bannerImage.png',
          '/static/bannerImage.png',
          '/static/bannerImage.png'
        ]
      },
      activityId: null
    }
  },
  computed:{
    imageList(){
      if (this.activityData.image) {
        return this.activityData.image.split(',')
      }
      return []
    },
    atlas(){
      if (this.activityData.atlas) {
        return this.activityData.atlas.split(',')
      }
      return []
    }
  },
  onLoad(options) {
    if (options.id) {
      this.activityId = options.id
      this.loadActivityDetail(options.id)
    }else {
      uni.showToast({
        title: '没有给活动id',
        icon: 'none'
      })
    }
  },
  methods: {
    // 自定义分享内容
    mixinCustomShare() {
      return {
        desc: '',
        title: `邀请您参加${this.activityData.title || ''}`,
        imageUrl: this.imageList[0],
        path: '/subPages/index/activityDetail?id=' + this.activityId
      }
    },
    async loadActivityDetail(id) {
      let params = {}
      if (uni.getStorageSync('token')) {
        params.token = uni.getStorageSync('token')
      }
      // 根据ID加载活动详情
      const res = await this.$api.activity.queryActivityById({
        activityId: id,
        ...params
      })
      this.activityData = res.result
    },

    previewImage(current, urls) {
      uni.previewImage({
        current: current,
        urls: urls
      })
    },
    async collectActivity() {
      const res = await this.$api.activity.collectionActivity({
        activityId: this.activityId
      })
      await this.loadActivityDetail(this.activityId)
      uni.showToast({
        title: `${res.message}`,  
        icon: 'none'
      })
    },
    signUpActivity() {
      if (this.activityData.numActivity >= this.activityData.numLimit) {
        uni.showToast({
          title: '报名人数已满',
          icon: 'none'
        })
        return
      }

      this.$refs.signUpFormRef.open()
      
    },
    
    onSignUpFormClose() {
      this.$refs.signUpFormRef.close()
    },
    
    async onSignUpFormSubmit(formData) {
      console.log('报名表单数据:', formData)
      
      
      // 这里可以调用API提交报名数据
      const res = await this.$api.activity.applyActivity({
        activityId: this.activityId,
        ...formData
      })
      if (res.code === 200) {
        this.$refs.globalPopupRef.open({
          content: '恭喜您报名成功',
          subContent: '别忘了准时参与活动哦！',
          titleType: 'submit',
          popupType: 'success',
          closefn: () => {
            setTimeout(() => {
              uni.navigateBack()
            }, 300);
          }
        })
        // 更新状态
        // this.loadActivityDetail(this.activityId)
      }else {
        uni.showToast({
          title: `${res.message}`,
          icon: 'none'
        })
      }
    }
  },
  async onPullDownRefresh() {
    await this.loadActivityDetail(this.activityId)
    uni.stopPullDownRefresh()
  }
}
</script>

<style lang="scss" scoped>
.activity-detail {
  min-height: 100vh;
  background: #f8f8f8;
  padding-bottom: 120rpx;
  
  .banner-container {
    width: 100%;
    height: 450rpx;
    
    .banner-swiper {
      width: 100%;
      height: 100%;
      
      .banner-image {
        width: 100%;
        height: 100%;
      }
    }
  }
  
  .activity-info {
    background: #ffffff;
    // border: 1rpx dashed #F3F7F8;
    margin: 20rpx;
    border-radius: 16rpx;
    padding: 30rpx;
    
    .title-section {
      display: flex;
      align-items: center;
      margin-bottom: 30rpx;
      
      .activity-badge {
        background: #218CDD;
        border-radius: 8rpx;
        height: 40rpx;
        padding: 0 5rpx 10rpx;
        line-height: 40rpx;
        text-align: center;
        margin-right: 16rpx;
        
        .badge-text {
          color: #ffffff;
          font-size: 24rpx;
          font-weight: 500;
        }
      }
      
      .activity-title {
        font-size: 36rpx;
        font-weight: bold;
        color: #333333;
        flex: 1;
      }
    }
    
    .info-section {
      background: #F3F7F8;
      margin-bottom: 40rpx;
      // 虚线属性
      border: 2rpx dashed #F3F7F8;
      
      .info-item {
        display: flex;
        align-items: center;
        margin-bottom: 20rpx;
        
        &:last-child {
          margin-bottom: 0;
        }
        
        .info-label {
          font-size: 28rpx;
          color: #999999;
          margin-left: 12rpx;
          margin-right: 8rpx;
        }
        
        .info-value {
          font-size: 28rpx;
          color: #999999;
          flex: 1;
        }
      }
    }
    
    .detail-section {
      margin-bottom: 40rpx;
      
      .section-title {
        margin-bottom: 20rpx;
        
        .title-text {
          font-size: 32rpx;
          font-weight: bold;
          color: #333333;
        }
      }
      
      .detail-content {
        .detail-text {
          display: block;
          font-size: 28rpx;
          color: #666666;
          line-height: 1.6;
          margin-bottom: 16rpx;
          
          &:last-child {
            margin-bottom: 0;
          }
        }
      }
    }
    
    .gallery-section {
      .section-title {
        margin-bottom: 20rpx;
        
        .title-text {
          font-size: 32rpx;
          font-weight: bold;
          color: #333333;
        }
      }
      
      .gallery-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 16rpx;
        
        .gallery-image {
          width: 100%;
          height: 200rpx;
          border-radius: 12rpx;
        }
      }
    }
  }
  
  .bottom-action {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #ffffff;
    padding: 20rpx 30rpx;
    border-top: 1rpx solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 100;
    
    .action-left {
      display: flex;
      align-items: center;
      gap: 100rpx;
      
      .action-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 8rpx;
        
        .action-text {
          font-size: 22rpx;
          color: #000;
        }
        
        .participants-count {
          font-size: 24rpx;
          color: #333333;
          // font-weight: bold;
        }
      }
    }
    
    .action-right {
      flex-shrink: 0;
    }
  }
}
</style>