<template>
  <view class="activity-page">
    <!-- 搜索栏和一级Tab合并容器 -->
    <view class="search-section">
      <view class="search-bar">
        <uv-search placeholder="请输入搜索内容" v-model="params.title" @search="handleSearch" @clear="handleSearch" @clickIcon="handleSearch" :showAction="false" ></uv-search>
      </view>
      
      <!-- 一级Tab：当前活动/往期活动 移到搜索容器内 -->
      <view class="primary-tabs">
        <view 
          class="primary-tab-item" 
          :class="{ active: primaryActiveTab === 'current' }"
          @click="switchPrimaryTab('current')"
        >
          当前活动
        </view>
        <view 
          class="primary-tab-item" 
          :class="{ active: primaryActiveTab === 'past' }"
          @click="switchPrimaryTab('past')"
        >
          往期活动
        </view>
      </view>
    </view>

    <!-- 二级Tab：自定义Tab组件 -->
    <view class="secondary-tabs">
      <scroll-view scroll-x="true" class="tab-scroll">
        <view class="tab-list">
          <!-- 全部Tab -->
          <view 
            class="tab-item" 
            :class="{ active: secondaryActiveIndex === 0 }"
            @click="switchSecondaryTab(0, '全部')"
          >
            <text class="tab-text">全部</text>
          </view>
          
          <!-- 从store获取的活动分类Tab -->
          <view 
            v-for="(category, index) in categoryActivityList" 
            :key="category.id"
            class="tab-item" 
            :class="{ active: secondaryActiveIndex === index + 1 }"
            @click="switchSecondaryTab(index + 1, category.title, category.id)"
          >
            <text class="tab-text">{{ category.title }}</text>
          </view>
          <!-- 动画下划线 -->
          <!-- <view class="tab-line" :style="lineStyle"></view> -->
        </view>
        
      </scroll-view>
    </view>

    <!-- 活动列表 -->
    <view class="activity-list">
      <view 
        class="activity-item" 
        v-for="(item, index) in activities" 
        :key="index"
        @click="goToActivityDetail(item)"
      >
        <!-- 活动图片 -->
        <view class="activity-image">
          <image :src="item.image" mode="aspectFill" class="image"></image>
        </view>
        
        <!-- 活动信息 -->
        <view class="activity-info">
          <view class="title-row">
            <!-- 活动标签 -->
            <view class="activity-tag" :style="{ backgroundColor: item.tagColor }">
              {{ item.score }}分
            </view>
            <view class="activity-title">{{ item.title }}</view>
          </view>
          <view class="activity-location">
            <uv-icon name="map-fill" size="14" color="#999"></uv-icon>
            <text class="location-text">{{ item.address }}</text>
          </view>
          <view class="activity-time">
            <uv-icon name="calendar" size="14" color="#999"></uv-icon>
            <text class="time-text">{{ item.createTime }}</text>
          </view>
          <view class="activity-participants">
            <uv-icon name="account-fill" size="14" color="#999"></uv-icon>

            <text class="participants-text" >报名人数：{{ item.numActivity }}/{{ item.numLimit }}</text>
 
          </view>
        </view>
        
        <!-- 报名按钮 -->
        <view class="activity-action">
          <uv-button v-if="item.status === '1'" type="primary" size="mini" shape="circle" text="已结束" disabled @click.stop="signUpActivity(item)"></uv-button>
          <uv-button v-else-if="item.isApply === 1" type="primary" size="mini" shape="circle" text="已报名" disabled @click.stop="signUpActivity(item)"></uv-button>
          <uv-button v-else type="primary" size="mini" shape="circle" :text="item.numActivity >= item.numLimit ? '已结束' : '报名中'" :disabled="item.numActivity >= item.numLimit" @click.stop="signUpActivity(item)"></uv-button>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-if="activities.length === 0">
      <uv-empty icon="/static/暂无搜索结果.png" text="暂无活动数据"></uv-empty>
      <!-- <image src="/static/暂无搜索结果.png" style="width: 460rpx; height: 460rpx; margin: 0 auto;"></image> -->
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      searchKeyword: '',
      primaryActiveTab: 'current', // current: 当前活动, past: 往期活动
      params: {
        pageNo: 1,
        pageSize: 10,
        title: '',  // 搜索关键字
        // categoryId: null,  // 分类的id
        status: 0 // 活动的状态 0是当前 1是往期
      },
      secondaryActiveIndex: 0,
      // 模拟活动数据
      activities: []
    }
  },
  computed: {
    // 从store获取活动分类列表
    categoryActivityList() {
      return this.$store.state.categoryActivityList || []
    },
  },
  methods: {
    handleSearch(value) {
      if (value) {
        this.params['title'] = value
      }
      this.initData()
      this.getActivityList()
    },
    // 切换一级tab
    switchPrimaryTab(tab) {
      this.primaryActiveTab = tab;
      this.initData()
      delete this.params['categoryId']
      // 标签回到全部
      this.secondaryActiveIndex = 0
      this.params['status'] = tab === 'current' ? 0 : 1
      this.getActivityList()
    },
    
    // 切换二级tab
    async switchSecondaryTab(index, tabName, categoryId = null) {
      this.initData()
      this.secondaryActiveIndex = index
      delete this.params['categoryId']
      if (index === 0) {
        // 全部Tab
        console.log('点击了全部Tab')
      } else {
        // 活动分类Tab
        this.params['categoryId'] = categoryId
      }
      await this.getActivityList()
    },
    
    // 跳转到活动详情
    goToActivityDetail(activity) {
      uni.navigateTo({
        url: `/subPages/index/activityDetail?id=${activity.id}`
      });
    },
    
    // 报名活动
    signUpActivity(item) {
      if (item.isFullOrExpired) {
        uni.showToast({
          title: '活动已结束',
          icon: 'none'
        });
        return;
      }
      uni.navigateTo({
        url: `/subPages/index/activityDetail?id=${item.id}`
      });
    },

    // 获取活动列表
    async getActivityList(){
      const res = await this.$api.activity.queryActivityList(this.params)
      if(res.result.records.length){
        this.activities.push(...res.result.records)
        this.params.pageNo++
      }else {
        uni.showToast({
          title: '暂无活动数据',
          icon: 'none'
        })
      }
    },
    initData() {
      this.params['pageNo'] = 1
      this.activities = []
    }
  },
  
  async onShow() {
    // 确保store中的活动分类数据已加载
    if (this.categoryActivityList.length === 0) {
      await this.$store.dispatch('getCategoryActivityList')
    }
    this.initData()
    this.params['title'] = ''
    await this.getActivityList()
  },
  
  onReachBottom() {
    this.getActivityList()
  },

  async onPullDownRefresh() {
    this.initData()
    await this.getActivityList()
    uni.stopPullDownRefresh()
  }
}
</script>

<style lang="scss" scoped>
.activity-page {
  background-color: #f5f5f5;
  min-height: 100vh;
}

// 搜索栏样式 - 修改为包含一级Tab
.search-section {
  height: 350rpx;
  background: linear-gradient(180deg,#1488db, #98b5f1);
  padding-top: 180rpx; /* 使用padding-top避免margin塌陷 */
  box-sizing: border-box; /* 确保padding包含在高度内 */
}

.search-bar {
  // background-color: white;
  // border-radius: 50rpx;
  padding: 5rpx 40rpx;
  // display: flex;
  // align-items: center;
  // gap: 20rpx;
  // width: 85%;
  // margin: 0 auto ; /* 移除margin-top，只保留左右居中和下边距 */
}

.search-input {
  flex: 1;
  font-size: 28rpx;
  color: #333;
  
  &::placeholder {
    color: #999;
  }
}

// 一级Tab样式 - 调整为在蓝色背景内
.primary-tabs {
  display: flex;
  padding: 0 20rpx;
  margin-bottom: 20rpx;
}

.primary-tab-item {
  flex: 1;
  text-align: center;
  padding: 20rpx 0;
  font-size: 32rpx;
  color: #000000; /* 白色半透明 */
  position: relative;
  transition: color 0.3s ease;
  &.active {
    color: white; /* 激活状态为纯白色 */
    font-weight: 600;
    
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      // transition: transform 0.3s ease;
      width: 100rpx;
      height: 6rpx;
      background-color: white; /* 下划线改为白色 */
      border-radius: 3rpx;
    }
  }
}

// 二级Tab样式 - 自定义实现
.secondary-tabs {
  background-color: white;
  border-bottom: 1px solid #f0f0f0;
  position: relative;
  
  .tab-scroll {
    white-space: nowrap;
    
    .tab-list {
      display: flex;
      // position: relative;
      justify-content: space-evenly;
      .tab-item {
        flex-shrink: 0;
        padding: 24rpx 32rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;
        
        .tab-text {
          font-size: 28rpx;
          color: #666666;
          font-weight: 500;
          transition: color 0.3s ease;
        }
        
        &.active {
          .tab-text {
            color: #007AFF;
            font-weight: 600;
          }
        }
      }
    }
    
    .tab-line {
      position: absolute;
      bottom: 10;
      height: 6rpx;
      background-color: #007AFF;
      border-radius: 3rpx;
      transition: transform 0.3s ease;
    }
  }
}

// 活动列表样式
.activity-list {
  padding: 20rpx;
}

.activity-item {
  background-color: white;
  border-radius: 12rpx;
  margin-bottom: 30rpx;
  padding: 20rpx;
  display: flex;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
}

.activity-image {
  width: 180rpx;
  height: 180rpx;
  border-radius: 8rpx;
  overflow: hidden;
  flex-shrink: 0;
  margin-right: 20rpx;
}

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

.activity-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.title-row {
  display: flex;
  align-items: center;
  margin-bottom: 10rpx;
}

.activity-tag {
  width: 31px;
  height: 20px;
  background: #218cdd;
  border-radius: 3.5px;
  margin-right: 7rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18rpx;
  color: white;
  font-weight: 600;
}

.activity-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  line-height: 1.4;
}

.activity-location,
.activity-time,
.activity-participants {
  display: flex;
  align-items: center;
  margin-bottom: 6rpx;
  
  .location-text,
  .time-text,
  .participants-text {
    font-size: 24rpx;
    color: #666;
    margin-left: 6rpx;
  }
}

.activity-action {
  display: flex;
  align-items: flex-end;
  padding-bottom: 10rpx;
}

// 空状态样式
.empty-state {
  padding: 100rpx 40rpx;
}
</style>