<template>
	<view class="index">
		<view class="search">
			<uni-search-bar @confirm="search" cancelButton="none" @clear="onClear"  v-model="queryParams.activityTheme">
			</uni-search-bar>
		</view>
		<scroll-view class="scroll-views" scroll-y="true" @scrolltolower="scrolltolower">
			<view class="list">
				<view v-for="(item,index) in list" :key="index" class="box">
					<view @click="detail(item)" class="content">
						<view>
							<view>活动主题：{{item.activityTheme}}</view>
							<view v-if="item.auditStatus==1" class="active">报名通过</view>
							<view v-else-if="item.auditStatus==2" class="dark">不通过</view>
							<view v-else class="staydark">待审核</view>
						</view>
						<view>
							<text>活动时间：</text>
							<text>{{item.communityActivitiesList[0].activityTime}}</text>
						</view>
						<view>
							<text>活动地点：</text>
							<text>{{item.communityActivitiesList[0].activityPlace}}</text>
						</view>
						<view>
							<text>活动可容纳人数：</text>
							<text>{{item.communityActivitiesList[0].galleryful}}人</text>
						</view>
						<view>
							<text>当前报名人数：</text>
							<text>{{item.numberApplicants}}人</text>
						</view>
						<view>
							<text>具体要求：</text>
							<text>{{item.communityActivitiesList[0].specificRequirements}}</text>
						</view>
					</view>
					<!-- 取消报名按钮 -->
					<view class="cancel-btn-container" v-if="canCancel(item)">
						<button class="cancel-btn" @click.stop="cancelApply(item)">取消报名</button>
					</view>
				</view>
			</view>

			<view class="empty" v-if="list.length==0&&topLoading==false">
				<image src="@/static/empty.png" mode="widthFix"></image>
				暂时没有数据
			</view>
			<view class="loading-more-top" v-if="topLoading">
				<uni-load-more style="padding-top: 90px;" status="loading" />
			</view>
			<view class="loading-more" v-if="loading">
				<uni-load-more status="loading" />
			</view>
			<view class="no-more" v-if="!loading&&list.length!=0">
				到底啦~~
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
	applyList,
	cancelRegistration
} from '@/api/OneStopCommunity/apply.js';
	export default {
		data() {
			return {
				list: [],
				loading: false,
				topLoading: true,
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					activityTheme: "",
					jobNumber:uni.getStorageSync("stuNo")
				}
			}
		},
		methods: {
			search(res) {
				this.getlist()
			},
			onClear(){
				this.queryParams.pageNum = 1;
				this.queryParams.activityTheme="";
				this.getlist();
			},
			scrolltolower() {
				if (this.queryParams.pageNum < this.totalPages) {
					this.queryParams.pageNum++;
					this.loading = true;
					setTimeout(() => {
						//请求参数
						this.getlist()
					}, 1000)
				} else {

				}
			},
			detail(item) {
				uni.navigateTo({
					url: `detail?recordid=${item.recordId}`
				})
			},
			// 判断是否可以取消报名
			canCancel(item) {
				// 只有待审核(0)或已通过(1)的报名可以取消，已拒绝(2)的不能取消
				// 同时需要检查报名截止时间
				if (item.auditStatus === 2) {
					return false; // 已拒绝的不能取消
				}
				
				// 检查报名截止时间
				if (item.communityActivitiesList && item.communityActivitiesList.length > 0) {
					const activity = item.communityActivitiesList[0];
					if (activity.signUpEndTime) {
						const now = new Date();
						const endTime = new Date(activity.signUpEndTime);
						if (now > endTime) {
							return false; // 报名已截止
						}
					}
				}
				
				return true;
			},
			// 取消报名
			cancelApply(item) {
				uni.showModal({
					title: '确认取消',
					content: '确定要取消报名吗？取消后如需重新报名，请在报名截止时间前重新提交申请。',
					success: async (res) => {
						if (res.confirm) {
							try {
								uni.showLoading({
									title: '取消中...'
								});
								
								const result = await cancelRegistration(item.recordId);
								
								uni.hideLoading();
								
								if (result.code === 200) {
									uni.showToast({
										title: '取消报名成功',
										icon: 'success'
									});
									
									// 刷新列表
									this.queryParams.pageNum = 1;
									this.list = [];
									this.getlist();
								} else {
									uni.showToast({
										title: result.msg || '取消报名失败',
										icon: 'none'
									});
								}
							} catch (error) {
								uni.hideLoading();
								uni.showToast({
									title: '取消报名失败',
									icon: 'none'
								});
								console.error('取消报名失败:', error);
							}
						}
					}
				});
			},
			async getlist() {
				let res = await applyList(this.queryParams)
				if (res.code == 200) {
					this.loading = false;
					if (this.queryParams.pageNum == 1) {
						this.list = res.rows; // 如果是第一页，直接显示新数据
						// for (var i = 0; i < res.rows.length; i++) {
						// 	this.list =res.rows.communityActivitiesList[i]
						// }
					} else {
						this.list = this.list.concat(res.rows); // 否则追加新数据到列表中
					}
					this.totalPages = Math.ceil(res.total / this.queryParams.pageSize);
					this.topLoading = false;
				}
				console.log(res)
			}
		},
		onLoad() {},
		onShow() {
			this.topLoading = true;
			this.queryParams.pageNum = 1;
			this.list = [];
			this.getlist();
		},
	}
</script>

<style lang="scss" scoped>
	.index {
		background-color: #F5F5F7;
		min-height: 100vh;

		.search {
			width: 100%;
			background-color: white;
			position: fixed;
			z-index: 10;
			top: 80rpx;
			padding: 0rpx 8rpx;
			background-color: #F5F5F7;

			/deep/ .uni-searchbar__box {
				background-color: white !important;
			}
		}

		.scroll-views {
			height: calc(100vh - 10px);

			.list {
				margin-top: 100rpx;
				padding: 24rpx;

				&>view:nth-child(n+2) {
					margin-top: 30rpx;
				}

				.box {
					width: 100%;
					// height: 364rpx;
					padding: 20rpx 40rpx;
					border-radius: 16rpx;
					background-color: white;
					font-size: 28rpx;

					.content {
						&>view {
							word-break: break-all;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1;
							text-overflow: ellipsis;
						}

						&>view:nth-child(1) {
							display: flex;
							justify-content: space-between;
							&>view:last-child{
								padding:8rpx 10rpx;
								font-size: 22rpx;
							}
							.active {
								background-color: #E7FAF0;
								border: 1px solid #D0F5E0;
								color: #71E2A3;
							}

							.dark {
								background-color: #FFDBDB;
								border: 1px solid #FF9292;
								color: #FF9292;
							}

							.staydark {
								background-color: #FFF8E6;
								border: 1px solid #FFE399;
								color: #FFBA00;
							}
						}

						&>view:nth-child(2) {
							margin-top: 40rpx;
						}

						&>view:nth-child(n+2) {
							line-height: 50rpx;
						}
					}

					.cancel-btn-container {
						margin-top: 20rpx;
						display: flex;
						justify-content: flex-end;

						.cancel-btn {
							background-color: #ff4757 !important;
							color: white;
							margin-top: 100rpx;
							
							&:active {
								background-color: #ff3838;
							}
						}
					}
				}
			}

			.no-more {
				text-align: center;
				color: gray;
				padding-bottom: 10px;
			}

			.empty {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-top: 200rpx;
				color: #9E9E9E;
				font-size: 36rpx;

				image {
					width: 250rpx;
					margin-bottom: 50rpx;
				}
			}
		}
	}
</style>
