<template>
	<view class="container">
		<view class="status_bar"></view>
		<!-- 顶部标题栏 -->
		<view class="top-bar">
			  <uni-nav-bar left-icon="left"  title="活动列表" backgroundColor ="none" :border="false" @clickLeft="narBack" />
		</view>
		<!-- 顶部图片 -->
		<view class="topImg">
			<image src="http://47.104.199.163:9090/images/home/banner.png" style="width: 100%;height:100%;"></image>
		</view>
		<!-- 选项卡 -->
		<view class="tabs-container">
			<view class="tab-item" :class="{ 'active': activeTab === 'list' }" @tap="switchTab('list')">
				活动列表
				<view v-if="activeTab === 'list'" class="tab-indicator"></view>
			</view>
			<view class="tab-item" :class="{ 'active': activeTab === 'my' }" @tap="switchTab('my')">
				我的活动
				<view v-if="activeTab === 'my'" class="tab-indicator"></view>
			</view>
		</view>

		<!-- 我的活动状态筛选 -->
		<view v-if="activeTab === 'my'" class="filter-container">
			<scroll-view class="filter-scroll" scroll-x>
				<view class="filter-buttons">
					<view v-for="filter in filters" :key="filter.value" class="filter-btn"
						:class="{ 'active': myFilter === filter.value }" @tap="switchFilter(filter.value)">
						{{ filter.label }}
					</view>
				</view>
			</scroll-view>
		</view>

		<!-- 活动列表 -->
		<scroll-view class="activity-list" scroll-y @scrolltolower="loadMoreData" :lower-threshold="100">
			<!-- 空状态 -->
			<view v-if="displayedActivities.length === 0" class="empty-state">
				<uni-icons type="info" size="60" color="#ccc"></uni-icons>
				<text class="empty-text">
					{{ activeTab === 'list' ? '暂无活动' : '暂无参与的活动' }}
				</text>
			</view>

			<!-- 活动卡片 -->
			<view v-for="activity in displayedActivities" :key="activity.id" class="activity-card"
				@tap="viewActivityDetail(activity)">
				<!-- 活动状态标签 -->
				<!-- <view class="status-badge" :class="getStatusClass(activity.status)">
					{{ getStatusText(activity.status) }}
				</view> -->
				<view class="status-badge">
					进行中
				</view>

				<!-- 已报名标识 -->
				<view v-if="activity.hasSigned && activeTab === 'list'" class="signed-badge">
					<uni-icons type="checkbox-filled" size="14" color="#fff"></uni-icons>
					<text>已报名</text>
				</view>

				<!-- 活动信息 -->
				<view class="activity-info">
					<view class="u-demo-block__content ">
						<up-row justify="space-between" customStyle="margin-bottom: 10px">
							<up-col span="4">
								<view class="info-image-left">
									<image src="http://47.104.199.163:9090/images/home/activity-img.png" style="width: 100%;height: 100%;"></image>
								</view>
							</up-col>
							<up-col span="8">
								<view class="demo-layout bg-purple">
									<view class="activity-title">
										{{ activity.title }}
									</view>
									<view class="activity-detail">
										<text class="detail-text">时间：{{ formatDate(activity.startTime) }} 至
											{{ formatDate(activity.endTime) }}</text>
									</view>
									
									<view class="activity-detail">
										<text class="detail-text">地点：{{ activity.location }}</text>
									</view>
									
									<view class="activity-detail">
										<text class="detail-text-num">活动名额{{ activity.totalQuota }}人，已报名{{ activity.signedCount }}人</text>
									</view>

								</view>
							</up-col>
						</up-row>
					</view>

					<!-- 我的活动额外信息 -->
				<!-- 	<view v-if="activeTab === 'my'" class="my-activity-info">
						<view class="my-status" :class="getMyStatusClass(activity.myStatus)">
							{{ getMyStatusText(activity.myStatus) }}
						</view>

						<view v-if="activity.myStatus === 'reviewing'" class="review-tips">
							<text>审核中，请耐心等待</text>
						</view>

						<view v-if="activity.myStatus === 'rejected'" class="review-tips">
							<text>未通过审核，如有疑问请联系管理员</text>
						</view>
					</view> -->

					<!-- 操作按钮 -->
					<view class="action-buttons">
						<button v-if="activeTab === 'list' && !activity.hasSigned" class="action-btn signup-btn"
							@tap.stop="signupActivity(activity)">
							立即报名
						</button>

						<!-- <button v-if="activeTab === 'list' && activity.hasSigned" class="action-btn signed-btn"
							disabled>
							已报名
						</button> -->

					<!-- 	<button
							v-if="activeTab === 'my' && activity.myStatus === 'passed' && activity.status === 'ongoing'"
							class="action-btn signup-btn" @tap.stop="viewActivityDetail(activity)">
							查看详情
						</button> -->

						<!-- <button
							v-if="activeTab === 'my' && activity.myStatus === 'passed' && activity.status === 'ended'"
							class="action-btn ended-btn" disabled>
							已结束
						</button> -->

						<!-- <button v-if="activeTab === 'my' && activity.myStatus === 'rejected'"
							class="action-btn reject-btn" @tap.stop="contactAdmin(activity)">
							联系管理员
						</button> -->
					</view>
				</view>
			</view>

			<!-- 加载更多 -->
			<view v-if="hasMoreData && displayedActivities.length > 0" class="load-more-container">
				<view v-if="!isLoading" class="load-more-btn" @tap="loadMoreData">
					<text>加载更多</text>
					<uni-icons type="arrow-down" size="16" color="#007AFF"></uni-icons>
				</view>
				<view v-else class="loading-container">
					<uni-icons type="spinner-cycle" size="20" color="#007AFF" class="loading-icon"></uni-icons>
					<text>正在加载...</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed,
		onMounted,
		watch
	} from 'vue'
	import moment from 'moment';

	// 响应式数据
	const activeTab = ref('list') // 'list' 或 'my'
	const myFilter = ref('reviewing') // 我的活动筛选条件
	const isLoading = ref(false)
	const hasMoreData = ref(true)
	const currentPage = ref(1)
	const pageSize = 5

	// 筛选条件
	const filters = ref([{
			label: '审核中',
			value: 'reviewing'
		},
		{
			label: '已通过',
			value: 'passed'
		},
		{
			label: '未通过',
			value: 'rejected'
		},
		{
			label: '已结束',
			value: 'ended'
		}
	])

	// 模拟活动数据
	const allActivities = ref([{
			id: 1,
			title: '阳光海岸社区生活节活动',
			startTime: '2023-06-19 08:00',
			endTime: '2023-06-30 17:00',
			location: '山东省日照市东港区莱顿二期花语墅西沿街',
			totalQuota: 100,
			signedCount: 68,
			status: 'ongoing', // ongoing, upcoming, ended
			hasSigned: true,
			myStatus: 'passed', // reviewing, passed, rejected
			coverImage: '/static/activity1.jpg',
			description: '阳光海岸社区生活节是一个为期两周的社区文化活动，包括文艺表演、手工艺品展示、美食节等多种活动。'
		},
		{
			id: 2,
			title: '社区环保志愿者招募活动',
			startTime: '2023-07-01 09:00',
			endTime: '2023-07-31 18:00',
			location: '社区公园及周边街道',
			totalQuota: 50,
			signedCount: 32,
			status: 'upcoming',
			hasSigned: false,
			myStatus: 'reviewing',
			coverImage: '/static/activity2.jpg',
			description: '招募社区环保志愿者，参与垃圾分类宣传、社区清洁等环保活动。'
		},
		{
			id: 3,
			title: '端午节包粽子亲子活动',
			startTime: '2023-06-10 14:00',
			endTime: '2023-06-10 17:00',
			location: '社区活动中心',
			totalQuota: 30,
			signedCount: 30,
			status: 'ended',
			hasSigned: true,
			myStatus: 'passed',
			coverImage: '/static/activity3.jpg',
			description: '端午节亲子活动，教孩子们包粽子，了解传统文化。'
		},
		{
			id: 4,
			title: '老年人智能手机使用培训',
			startTime: '2023-07-05 14:00',
			endTime: '2023-07-12 16:00',
			location: '社区图书馆二楼',
			totalQuota: 20,
			signedCount: 15,
			status: 'upcoming',
			hasSigned: false,
			myStatus: null,
			coverImage: '/static/activity4.jpg',
			description: '为社区老年人提供智能手机使用培训，包括微信、支付宝等常用APP的使用。'
		},
		{
			id: 5,
			title: '社区篮球友谊赛',
			startTime: '2023-06-25 15:00',
			endTime: '2023-06-25 18:00',
			location: '社区体育馆',
			totalQuota: 40,
			signedCount: 25,
			status: 'ongoing',
			hasSigned: false,
			myStatus: 'rejected',
			coverImage: '/static/activity5.jpg',
			description: '社区篮球友谊赛，欢迎篮球爱好者报名参加。'
		},
		{
			id: 6,
			title: '儿童暑期绘画班',
			startTime: '2023-07-10 09:00',
			endTime: '2023-08-20 11:00',
			location: '社区艺术工作室',
			totalQuota: 25,
			signedCount: 18,
			status: 'upcoming',
			hasSigned: true,
			myStatus: 'reviewing',
			coverImage: '/static/activity6.jpg',
			description: '暑期儿童绘画班，培养孩子的艺术兴趣和创造力。'
		},
		{
			id: 7,
			title: '社区消防安全演练',
			startTime: '2023-05-15 10:00',
			endTime: '2023-05-15 12:00',
			location: '社区广场',
			totalQuota: 100,
			signedCount: 85,
			status: 'ended',
			hasSigned: true,
			myStatus: 'passed',
			coverImage: '/static/activity7.jpg',
			description: '社区消防安全知识讲座和应急演练。'
		},
		{
			id: 8,
			title: '邻里美食分享会',
			startTime: '2023-07-08 16:00',
			endTime: '2023-07-08 19:00',
			location: '社区花园',
			totalQuota: 40,
			signedCount: 22,
			status: 'upcoming',
			hasSigned: false,
			myStatus: null,
			coverImage: '/static/activity8.jpg',
			description: '邻里美食分享会，每家带一道拿手菜，共享美食。'
		}
	])

	// 当前显示的活动列表
	const displayedActivities = ref([])

	// 计算属性：根据当前标签和筛选条件过滤活动
	const filteredActivities = computed(() => {
		if (activeTab.value === 'list') {
			// 活动列表页：显示所有活动
			return allActivities.value
		} else {
			// 我的活动页：根据筛选条件过滤
			if (myFilter.value === 'all') {
				return allActivities.value.filter(activity => activity.hasSigned)
			} else {
				return allActivities.value.filter(activity =>
					activity.hasSigned && activity.myStatus === myFilter.value
				)
			}
		}
	})

// 返回
const narBack = () =>{
	if(uni.getStorageSync('sourcePage') === "index"){
		uni.switchTab({
			url:"/pages/index/index"
		})
	}else if(uni.getStorageSync('sourcePage') === "serviceIndex"){
		uni.switchTab({
			url:"/pages/serviceIndex/serviceIndex"
		})
	}
}
	// 切换选项卡
	const switchTab = (tab) => {
		activeTab.value = tab
		currentPage.value = 1
		displayedActivities.value = []
		loadPageData(1)
	}

	// 切换筛选条件
	const switchFilter = (filter) => {
		myFilter.value = filter
		currentPage.value = 1
		displayedActivities.value = []
		loadPageData(1)
	}

	// 加载分页数据
	const loadPageData = (pageNum) => {
		isLoading.value = true

		// 模拟网络请求延迟
		setTimeout(() => {
			const startIndex = (pageNum - 1) * pageSize
			const endIndex = startIndex + pageSize
			const pageData = filteredActivities.value.slice(startIndex, endIndex)

			if (pageNum === 1) {
				displayedActivities.value = pageData
			} else {
				displayedActivities.value = [...displayedActivities.value, ...pageData]
			}

			currentPage.value = pageNum
			hasMoreData.value = displayedActivities.value.length < filteredActivities.value.length
			isLoading.value = false
		}, 500)
	}

	// 加载更多数据
	const loadMoreData = () => {
		if (isLoading.value || !hasMoreData.value) return
		loadPageData(currentPage.value + 1)
	}

	// 获取活动状态文本
	// const getStatusText = (status) => {
	// 	const statusMap = {
	// 		'ongoing': '进行中',
	// 		'upcoming': '即将开始',
	// 		'ended': '已结束'
	// 	}
	// 	return statusMap[status] || '未知'
	// }

	// 获取活动状态类名
	const getStatusClass = (status) => {
		const classMap = {
			'ongoing': 'status-ongoing',
			// 'upcoming': 'status-upcoming',
			// 'ended': 'status-ended'
		}
		return classMap[status] || ''
	}

	// 获取我的活动状态文本
	const getMyStatusText = (status) => {
		const statusMap = {
			'reviewing': '审核中',
			'passed': '已通过',
			'rejected': '未通过',
			'ended': '已结束'
		}
		return statusMap[status] || '未知'
	}

	// 获取我的活动状态类名
	const getMyStatusClass = (status) => {
		const classMap = {
			'reviewing': 'my-status-reviewing',
			'passed': 'my-status-passed',
			'rejected': 'my-status-rejected',
			'ended': 'my-status-ended'
		}
		return classMap[status] || ''
	}

	// 格式化日期
	function formatDate(date){
		return moment(date).format('YYYY-MM-DD HH:mm:ss')
	}

	// 报名活动
	const signupActivity = (activity) => {
		// console.log('fffff', activity)
		uni.navigateTo({
			url: `/pages/activity-list/activityListDetails?id=${activity.id}&title=${activity.title}`
		})
	}
	// const signupActivity = (activity) => {
	//   uni.showModal({
	//     title: '报名确认',
	//     content: `确定要报名参加"${activity.title}"吗？`,
	//     success: (res) => {
	//       if (res.confirm) {
	//         // 模拟报名成功
	//         activity.hasSigned = true
	//         activity.myStatus = 'reviewing'
	//         activity.signedCount += 1

	//         uni.showToast({
	//           title: '报名成功，等待审核',
	//           icon: 'success',
	//           duration: 2000
	//         })

	//         // 如果是我的活动页，刷新列表
	//         if (activeTab.value === 'my') {
	//           currentPage.value = 1
	//           displayedActivities.value = []
	//           loadPageData(1)
	//         }
	//       }
	//     }
	//   })
	// }

	// 查看活动详情
	const viewActivityDetail = (activity) => {
		uni.navigateTo({
			url: `/pages/activityDetail/activityDetail?id=${activity.id}`,
			fail: () => {
				// 如果页面不存在，显示活动信息
				uni.showModal({
					title: activity.title,
					content: `${activity.description}\n\n时间：${formatDate(activity.startTime)} 至 ${formatDate(activity.endTime)}\n地点：${activity.location}\n名额：${activity.totalQuota}人，已报名：${activity.signedCount}人`,
					showCancel: false,
					confirmText: '知道了'
				})
			}
		})
	}

	// 联系管理员
	const contactAdmin = (activity) => {
		uni.showModal({
			title: '联系管理员',
			content: `您报名"${activity.title}"未通过审核，是否联系管理员了解原因？`,
			success: (res) => {
				if (res.confirm) {
					uni.makePhoneCall({
						phoneNumber: '400-123-4567'
					})
				}
			}
		})
	}

	// 监听筛选条件变化
	watch(() => myFilter.value, () => {
		if (activeTab.value === 'my') {
			currentPage.value = 1
			displayedActivities.value = []
			loadPageData(1)
		}
	})

	// 初始化
	onMounted(() => {
		loadPageData(1)
	})
</script>

<style>
	/* 页面容器 */
	.container {
		min-height: 100vh;
		background-color: #f9f9f9;
		display: flex;
		flex-direction: column;
		padding: 20rpx;
	}
	.status_bar {
		height: 32px;
		width: 100%;
	 }
	/* 顶部标题 */
	.top-bar {
	  text-align: center;
	  margin-bottom: 20rpx;
	}
	.topImg {
		width: 100%;
		height: 280rpx;
		margin-bottom: 20rpx;
	}

	/* 顶部选项卡 */
	.tabs-container {
		display: flex;
		/* background-color: #fff; */
		/* border-bottom: 1px solid #eee; */
		/* position: sticky; */
		top: 0;
		z-index: 10;
		margin-bottom: 20rpx
	}

	.tab-item {
		flex: 1;
		text-align: center;
		padding: 20rpx 0;
		font-size: 32rpx;
		color: #666;
		position: relative;
	}

	.tab-item.active {
		color: #007AFF;
		font-weight: 500;
	}

	.tab-indicator {
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		width: 120rpx;
		height: 6rpx;
		background-color: #007AFF;
		border-radius: 4rpx;
	}

	/* 筛选容器 */
	.filter-container {
		background-color: #fff;
		padding: 20rpx 0;
		border-bottom: 1px solid #eee;
	}

	.filter-scroll {
		width: 100%;
		white-space: nowrap;
	}

	.filter-buttons {
		display: inline-flex;
		padding: 0 30rpx;
	}

	.filter-btn {
		display: inline-block;
		padding: 12rpx 30rpx;
		margin-right: 20rpx;
		font-size: 28rpx;
		color: #666;
		background-color: #f8f8f8;
		border-radius: 25rpx;
		border: 1px solid #eee;
	}

	.filter-btn.active {
		color: #fff;
		background-color: #2F77FD;
		/* border-color: #007AFF; */
	}

	/* 活动列表 */
	.activity-list {
		flex: 1;
		/* padding: 30rpx; */
		margin-bottom: 20rpx;
	}

	/* 活动卡片 */
	.activity-card {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		position: relative;
	}

	/* 活动状态标签 */
	.status-badge {
		position: absolute;
		top: 10rpx;
		left: 20rpx;
		padding: 6rpx 20rpx;
		font-size: 22rpx;
		border-radius: 15rpx 0rpx 15rpx 0rpx;
		color: #fff;
		z-index: 1;
		background: repeating-linear-gradient(to right, #FD7373  , #FB3F3F);
	}

/* 	.status-ongoing {
		 background: repeating-linear-gradient(to right, #FD7373  , #FB3F3F);
	} */

/* 	.status-upcoming {
		background-color: #ff9d00;
	}

	.status-ended {
		background-color: #999;
	} */

	/* 已报名标识 */
	.signed-badge {
		position: absolute;
		top: 20rpx;
		right: 8rpx;
		display: flex;
		align-items: center;
		padding: 6rpx 14rpx;
		background-color: #007AFF;
		color: #fff;
		font-size: 22rpx;
		border-radius: 20rpx;
		z-index: 1;
	}

	.signed-badge text {
		margin-left: 8rpx;
	}

	/* 活动信息 */
	.activity-info {
		margin-top: 10rpx;
	}

	.info-image-left {
		width: 170rpx;
		height: 220rpx;
	}

	.activity-title {
		font-size: 30rpx;
		font-weight: 500;
		color: #222;
		margin-bottom: 30rpx;
		line-height: 1.4;
		padding-right: 100rpx;
	}

	.activity-detail {
		display: flex;
		align-items: flex-start;
		margin-bottom: 20rpx;
		color: #999;
		font-size: 24rpx;
		line-height: 1.4;
	}

	.activity-detail .uni-icons {
		margin-right: 15rpx;
		margin-top: 4rpx;
		flex-shrink: 0;
	}
	.detail-text-num{
		font-size: 28rpx;
		color: #666666;
	}

	.detail-text {
		flex: 1;
	}

	/* 我的活动信息 */
	.my-activity-info {
		margin-top: 20rpx;
		padding: 20rpx;
		background-color: #f8f8f8;
		border-radius: 12rpx;
	}

	.my-status {
		display: inline-block;
		padding: 8rpx 24rpx;
		font-size: 24rpx;
		border-radius: 20rpx;
		margin-bottom: 10rpx;
	}

	.my-status-reviewing {
		background-color: #e6f2ff;
		color: #007AFF;
	}

	.my-status-passed {
		background-color: #e6f7f0;
		color: #07c160;
	}

	.my-status-rejected {
		background-color: #fdeeee;
		color: #f5222d;
	}

	.my-status-ended {
		background-color: #f5f5f5;
		color: #999;
	}

	.review-tips {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}

	/* 操作按钮 */
	.action-buttons {
		margin-top: 30rpx;
		display: flex;
		justify-content: flex-end;
	}

	.action-btn {
		padding: 0 40rpx;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 24rpx;
		border-radius: 35rpx;
		margin: 0;
	}

	.signup-btn {
		background-color: #007AFF;
		color: #fff;
	}

	.signup-btn:active {
		background-color: #0056cc;
	}

	.signed-btn {
		background-color: #f0f0f0;
		color: #999;
	}

	.reject-btn {
		background-color: #fff;
		color: #f5222d;
		border: 1px solid #f5222d;
	}

	.ended-btn {
		background-color: #f5f5f5;
		color: #999;
	}

	/* 加载更多 */
	.load-more-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 40rpx 0;
		color: #999;
	}

	.load-more-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 20rpx 40rpx;
		background-color: #f8f8f8;
		border-radius: 50rpx;
		color: #007AFF;
		font-size: 28rpx;
		margin-bottom: 20rpx;
	}

	.load-more-btn text {
		margin-right: 10rpx;
	}

	.loading-container {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 20rpx 40rpx;
		background-color: #f8f8f8;
		border-radius: 50rpx;
		color: #007AFF;
		font-size: 28rpx;
		margin-bottom: 20rpx;
	}

	.loading-icon {
		margin-right: 10rpx;
		animation: rotate 1s linear infinite;
	}

	@keyframes rotate {
		from {
			transform: rotate(0deg);
		}

		to {
			transform: rotate(360deg);
		}
	}

	/* 空状态 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;
		color: #ccc;
	}

	.empty-text {
		font-size: 32rpx;
		color: #999;
		margin-top: 20rpx;
		margin-bottom: 10rpx;
	}

	/* 适配不同屏幕 */
	@media (min-width: 768px) {
		.container {
			max-width: 500px;
			margin: 0 auto;
		}
	}
</style>