<template>
  <view class="activity-calendar">
    <!-- 活动列表 -->
    <view class="calendar-content">
      <view 
        v-for="(dayData, index) in activityData" 
        :key="index" 
        class="day-section"
      >
        <!-- 日期和日历图标 (在容器外部) -->
        <view class="date-header">
          <image 
            src="/subPages/static/活动日历_图标@2x.png" 
            class="calendar-icon"
          ></image>
          <text class="date-text">{{ dayData.activityTime }} {{ dayData.dayOfWeek }}</text>
        </view>
        
        <!-- 活动列表容器 -->
        <view class="activities-container">
          <view 
            v-for="(activity, actIndex) in dayData.activities" 
            :key="actIndex" 
            class="activity-item"
            @click="viewActivityDetail(activity)"
          >
            <!-- 活动图片 -->
            <image class="activity-image" :src="activity.image" mode="aspectFill"></image>
            
            <!-- 活动信息 -->
            <view class="activity-info">
              <view class="title-row">
                <view class="activity-badge">
                  <text class="badge-text">{{ activity.score }}分</text>
                </view>
                <text class="activity-title">{{ activity.title }}</text>
              </view>
              
              <view class="activity-location">
                <uv-icon name="map-fill" size="14" color="#999"></uv-icon>
                <text class="location-text">{{ activity.address }}</text>
              </view>
              
              <view class="activity-time">
                <uv-icon name="calendar" size="14" color="#999"></uv-icon>
                <text class="time-text">{{ activity.activityTime }}</text>
              </view>
              
              <view class="activity-participants">
                <uv-icon name="account-fill" size="14" color="#999"></uv-icon>
                <text class="participants-text">{{ activity.numActivity }}/{{ activity.numLimit }}人已报名</text>
              </view>
            </view>
            
            <!-- 查看详情按钮 -->
            <view class="activity-action">
              <view class="detail-btn" @click.stop="viewActivityDetail(activity)">
                <text class="detail-btn-text">查看详情</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'ActivityCalendar',
  data() {
    return {
      activityData: [],
      pageNo: 1,
      pageSize: 10,
      hasMore: true
    }
  },
  methods: {
    viewActivityDetail(activity) {
      // 跳转到活动详情页面
      uni.navigateTo({
        url: `/subPages/index/activityDetail?id=${activity.id}`
      });
    },
    
    // 处理后端返回的时间格式
    formatTime(timeString) {
      // 只截取年月日 中间有空格区分年月日和具体时间
      const [datePart, timePart] = timeString.split(' ');
      return datePart;
    },

    // 转化为时间格式
    changeData(arr, isRefresh) {
      if (isRefresh) {
        this.activityData = []
      }
      arr.forEach(item => {
        // 先查找是否存在相同日期的数据
        const existingDay = this.activityData.find(day => 
           day.dayOfWeek === item.dayOfWeek && this.formatTime(day.activityTime) === this.formatTime(item.activityTime)
        );
        
        if (existingDay) {
          // 如果找到了，添加到现有的activities数组中
          existingDay.activities.push(item);
        } else {
          // 如果没找到，创建新的日期条目
          this.activityData.push({
            activityTime: this.formatTime(item.activityTime),
            dayOfWeek: item.dayOfWeek,
            activities: [item]
          });
        }
      });
    },

    async getActivityData(isRefresh = false) {
      if (!this.hasMore) return
      const res = await this.$api.activity.queryActivityList({
        pageNo: this.pageNo,
        pageSize: this.pageSize
      })
      if (res.result.records.length){
        this.changeData(res.result.records, isRefresh)
        this.pageNo++
      }else {
        uni.showToast({
          title: '暂无数据',
          icon: 'none'
        })
        this.hasMore = false
      }
    },

    initData() {
      this.hasMore = true
      // this.activityData = []
      this.pageNo = 1
    }
  },
  
  async onShow() {
    this.initData()
    await this.getActivityData(true);
  },
  onReachBottom() {
    this.getActivityData();
  },
  async onPullDownRefresh() {
    this.initData()
    await this.getActivityData(true);
    uni.stopPullDownRefresh()
  }
}
</script>

<style lang="scss" scoped>
// @import '@/uni.scss';

.activity-calendar {
  background-color: #f5f5f5;
  min-height: 100vh;
  
  .calendar-content {
    padding: 40rpx 30rpx;
    
    .day-section {
      margin-bottom: 60rpx;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .date-header {
        display: flex;
        align-items: center;
        margin-bottom: 30rpx;
        
        .calendar-icon {
          width: 48rpx;
          height: 48rpx;
          margin-right: 20rpx;
        }
        
        .date-text {
          font-size: 32rpx;
          font-weight: bold;
          color: #000000;
        }
      }
      
      .activities-container {
        .activity-item {
          background: #ffffff;
          // border-radius: 16rpx;
          padding: 24rpx;
          margin-bottom: 20rpx;
          // box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
          display: flex;
          align-items: flex-start;
          transition: all 0.3s ease;
          position: relative;
          
          &:last-child {
            margin-bottom: 0;
          }
          
          &:active {
            transform: scale(0.98);
            box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.12);
          }
          
          .activity-image {
             width: 190rpx;
             height: 190rpx;
             border-radius: 8rpx;
             margin-right: 20rpx;
           }
           
           .activity-info {
             flex: 1;
             display: flex;
             flex-direction: column;
             justify-content: space-between;
             
             .title-row {
               display: flex;
               align-items: center;
               margin-bottom: 10rpx;
               
               .activity-badge {
                 width: 31px;
                 height: 20px;
                 background: #218cdd;
                 border-radius: 3.5px;
                 margin-right: 7rpx;
                 display: flex;
                 align-items: center;
                 justify-content: center;
                 
                 .badge-text {
                   font-size: 18rpx;
                   color: #fff;
                 }
               }
             }
             
             .activity-title {
               font-size: 28rpx;
               font-weight: bold;
               color: $uni-text-color;
               overflow: hidden;
               text-overflow: ellipsis;
               white-space: nowrap;
             }
             
             .activity-location,
             .activity-time,
             .activity-participants {
               display: flex;
               align-items: center;
               margin-bottom: 6rpx;
               
               .location-text,
               .time-text,
               .participants-text {
                 font-size: 24rpx;
                 color: $uni-text-color-grey;
                 margin-left: 6rpx;
                 overflow: hidden;
                 text-overflow: ellipsis;
                 white-space: nowrap;
                 flex: 1;
               }
             }
           }
           
           .activity-action {
             position: absolute;
             bottom: 20rpx;
             right: 20rpx;
             
             .detail-btn {
               background: #218CDD;
               border-radius: 26rpx;
               width: 140rpx;
               height: 52rpx;
               text-align: center;
               line-height: 44rpx;
               .detail-btn-text {
                 font-size: 26rpx;
                 color: #ffffff;
                 font-weight: 500;
               }
             }
             
             .detail-btn:active {
               background: #1976C7;
               transform: scale(0.95);
             }
           }
        }
      }
    }
  }
}
</style>