<template>
	<view class="activities-page">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar">
			<view class="navbar-left" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<view class="navbar-title">活动中心</view>
			<view class="navbar-right"></view>
		</view>

		<!-- 顶部公告 -->
		<view class="announcement-bar">
			<text class="announcement-icon">📢</text>
			<text class="announcement-content">红娘特训营报名开始啦！报名得积分，积分兑好礼！</text>
		</view>

		<!-- 活动分类标签 -->
		<view class="activity-tabs">
			<view class="tab-item active">全部活动</view>
		</view>

		<!-- 活动列表 -->
		<view class="activity-grid">
			<view class="activity-card" v-for="(item, index) in activityList" :key="index"
				@click="goToDetail(item.id)">
				<image :src="item.cover_image" class="activity-image" mode="aspectFill"></image>
				<view class="activity-info">
					<view class="activity-name">{{ item.name }}</view>
					<view class="activity-meta">
						<text class="activity-location">📍 {{ item.location }}</text>
					</view>
					<view class="activity-footer">
						<view class="activity-points">
							<text class="points-symbol">💎</text>
							<text class="points-value">{{ item.points }}</text>
						</view>
						<text class="activity-participants">{{ item.participants || 0 }}人参加</text>
					</view>
					<view class="activity-btn" @click.stop="goToDetail(item.id)">查看详情</view>
				</view>
			</view>
		</view>

		<!-- 加载更多 -->
		<view class="load-more" v-if="hasMore">
			<text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
		</view>
		<view class="no-more" v-else>
			<text class="no-more-text">没有更多了</text>
		</view>
	</view>
</template>

<script>
	import api from '@/utils/api.js'
	import { formatTime } from '@/utils/util.js'
	import { DEFAULT_IMAGES } from '@/config/index.js'

	export default {
		data() {
			return {
				activityList: [],
				pageNum: 1,
				pageSize: 10,
				hasMore: true,
				loading: false,
				// 模拟数据，当API返回为空时使用
				mockActivities: [
					{
						id: 1,
						name: '姻缘一线牵·大型相亲会',
						location: '雄安新区',
						points: 299,
						participants: 18,
						cover_image: 'https://images.unsplash.com/photo-1511671782779-c97d3d27a1d4?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
					},
					{
						id: 2,
						name: '一期一会·红娘心得分享',
						location: '北京',
						points: 389,
						participants: 54,
						cover_image: 'https://images.unsplash.com/photo-1464822759023-fed622ff2c3b?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
					},
					{
						id: 3,
						name: '公园相亲角',
						location: '天津',
						points: 369,
						participants: 16,
						cover_image: 'https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
					},
					{
						id: 4,
						name: '以爱为媒·千里相亲会',
						location: '北京',
						points: 268,
						participants: 25,
						cover_image: 'https://images.unsplash.com/photo-1517841905240-472988babdf9?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
					}
				]
			}
		},

		onLoad() {
			this.loadActivityList()
		},

		onReachBottom() {
			if (this.hasMore && !this.loading) {
				this.pageNum++
				this.loadActivityList()
			}
		},

		methods: {
			// 加载活动列表
			async loadActivityList() {
				if (this.loading) return
				this.loading = true

				try {
					const data = await api.activity.getList({
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						status: 1
					})

					if (data && data.length > 0) {
						this.activityList = [...this.activityList, ...data]
						this.hasMore = data.length >= this.pageSize
					} else {
						// 如果API返回为空，使用模拟数据
						this.hasMore = false
						if (this.pageNum === 1) {
							this.activityList = this.mockActivities
						}
					}
				} catch (error) {
				
					if (this.pageNum === 1) {
						this.activityList = this.mockActivities
					}
					this.hasMore = false
				} finally {
					this.loading = false
				}
			},

			// 格式化时间
			formatTime,

			// 跳转到详情
			goToDetail(id) {
				uni.navigateTo({
					url: `/pages/activities/detail?id=${id}`
				})
			},

			// 返回
			goBack() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.activities-page {
		min-height: 100vh;
		background-color: #FFF9F9;
		padding-top: 90rpx;
	}

	/* 自定义导航栏 */
	.custom-navbar {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
		z-index: 999;

		.navbar-left,
		.navbar-right {
			width: 80rpx;
		}

		.back-icon {
			font-size: 40rpx;
			color: #333333;
			font-weight: bold;
		}

		.navbar-title {
			flex: 1;
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
		}
	}

	/* 顶部公告 */
	.announcement-bar {
		display: flex;
		align-items: center;
		padding: 15rpx 20rpx;
		background-color: #FFF3E0;
		margin: 10rpx;
		border-radius: 15rpx;

		.announcement-icon {
			font-size: 28rpx;
			margin-right: 15rpx;
		}

		.announcement-content {
			flex: 1;
			font-size: 26rpx;
			color: #FF6B8A;
			line-height: 1.4;
		}
	}

	/* 活动分类标签 */
	.activity-tabs {
		display: flex;
		align-items: center;
		padding: 15rpx 20rpx;
		background-color: #FFFFFF;
		margin: 0 10rpx;
		border-radius: 15rpx;

		.tab-item {
			padding: 10rpx 30rpx;
			font-size: 28rpx;
			color: #666666;
			border-radius: 30rpx;
			transition: all 0.3s;

			&.active {
				background-color: #9C27B0;
				color: #FFFFFF;
			}
		}
	}

	/* 活动列表 - 两列布局 */
	.activity-grid {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 20rpx;
		padding: 20rpx;

		.activity-card {
			position: relative;
			background-color: #FFFFFF;
			border-radius: 20rpx;
			overflow: hidden;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);

			.activity-image {
				width: 100%;
				height: 240rpx;
				background-color: #F5F5F5;
			}

			.activity-info {
				padding: 20rpx;

				.activity-name {
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 10rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}

				.activity-meta {
					margin-bottom: 15rpx;

					.activity-location {
						font-size: 24rpx;
						color: #666666;
					}
				}

				.activity-footer {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 15rpx;

					.activity-points {
						display: flex;
						align-items: center;

						.points-symbol {
							font-size: 24rpx;
							margin-right: 5rpx;
						}

						.points-value {
							font-size: 26rpx;
							color: #FF6B8A;
							font-weight: bold;
						}
					}

					.activity-participants {
						font-size: 22rpx;
						color: #999999;
					}
				}

				.activity-btn {
					width: 100%;
					height: 60rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background-color: #9C27B0;
					color: #FFFFFF;
					border-radius: 30rpx;
					font-size: 26rpx;
				}
			}
		}
	}

	/* 加载更多 */
	.load-more,
	.no-more {
		padding: 30rpx 0;
		text-align: center;

		.load-text,
		.no-more-text {
			font-size: 24rpx;
			color: #999999;
		}
	}
</style>


