<template>
	<u-popup :show="show" @close="handleClose" @open="handleOpen" round="32rpx" bgColor="#ffffff" zIndex="10077">
		<view class="content">
			<!-- 头部 -->
			<view class="header">
				<view class="row bg">
					<image class="w40 h40" src="https://cdn.his.cdwjyyh.com/images/answer_icon.png"></image>
					<image class="w428 h64" src="https://cdn.his.cdwjyyh.com/images/collecting_coins_title.png"></image>
					<!-- 修改这里：改为调用 handleClose 方法 -->
					<image class="w40 h40" src="https://cdn.his.cdwjyyh.com/images/pop_close_icon.png" @click="handleClose"></image>
				</view>
				<!-- 积分信息 -->
				<view class="row mtb48 plr24">
					<view class="points row">
						<text class="lable">我的积分</text>
						<text class="num">3688</text>
					</view>
					<view class="shop row">
						<image class="w48 h48" src="https://cdn.his.cdwjyyh.com/images/fanghua_coin_icon.png"></image>
						<text class="shop-txt">芳华币商城</text>
						<image class="w24 h24" src="https://cdn.his.cdwjyyh.com/images/coin_arrow.png"></image>
					</view>
				</view>
			</view>

			<!-- 可滚动列表区域 -->
			<scroll-view scroll-y class="list" :style="{ height: scrollHeight + 'px' }" @scroll="handleScroll">
				<!-- 列表项 -->
				<view v-for="(item, index) in taskList" :key="index" class="list-item">
					<view class="row">
						<view class="left">
							<image class="w88 h88 mr24" src="https://cdn.his.cdwjyyh.com/images/img.png"></image>
							<view>
								<view class="mb8 row">
									<text class="title">{{ item.title }}({{ item.current }}/{{ item.total }})</text>
									<image class="w40 h40 mr4 ml8" src="https://cdn.his.cdwjyyh.com/images/fanghua_coin_icon.png"></image>
									<text class="num">+{{ item.points }}</text>
								</view>
								<view class="txt">{{ item.desc }}</view>
							</view>
						</view>
						<view class="button" @click="handleTaskClick(item)">
							{{ item.buttonText }}
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		props: {
			show: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				scrollHeight: 400,
				scrollTop: 0,
				showBackTop: false,
				loadingMore: false,
				hasMore: true,
				taskList: [
					{
						title: '点赞内容',
						current: 0,
						total: 1,
						points: 1,
						desc: '最多1条得芳华币',
						buttonText: '去完成'
					},	{
						title: '点赞内容',
						current: 0,
						total: 1,
						points: 1,
						desc: '最多1条得芳华币',
						buttonText: '去完成'
					},	{
						title: '点赞内容',
						current: 0,
						total: 1,
						points: 1,
						desc: '最多1条得芳华币',
						buttonText: '去完成'
					},	{
						title: '点赞内容',
						current: 0,
						total: 1,
						points: 1,
						desc: '最多1条得芳华币',
						buttonText: '去完成'
					},	{
						title: '点赞内容',
						current: 0,
						total: 1,
						points: 1,
						desc: '最多1条得芳华币',
						buttonText: '去完成'
					},	{
						title: '点赞内容',
						current: 0,
						total: 1,
						points: 1,
						desc: '最多1条得芳华币',
						buttonText: '去完成'
					},	{
						title: '点赞内容',
						current: 0,
						total: 1,
						points: 1,
						desc: '最多1条得芳华币',
						buttonText: '去完成'
					},
					// ... 其他数据项
				]
			}
		},
		mounted() {
			this.calcScrollHeight();
		},
		methods: {
			handleClose() {
				// 通知父组件关闭弹窗
				console.log("关闭弹窗");
				this.$emit('close');
			},
			
			handleOpen() {
				// 弹窗打开时的逻辑
				console.log("弹窗打开");
			},
			
			handleScroll(event) {
				// 更新当前滚动位置
				this.scrollTop = event.detail.scrollTop;
				
				// 判断是否显示返回顶部按钮
				this.showBackTop = this.scrollTop > 300;
				
				// 判断是否滚动到底部（需要加载更多）
				if (!this.loadingMore && this.hasMore) {
					const { scrollHeight } = event.detail;
					const scrollViewHeight = this.scrollHeight;
					
					// 距离底部50rpx时触发加载
					if (scrollHeight - this.scrollTop - scrollViewHeight < 50) {
						this.loadMoreData();
					}
				}
			},
			
			async loadMoreData() {
				if (this.loadingMore || !this.hasMore) return;
				
				this.loadingMore = true;
				try {
					// 调用API加载更多数据
					// const newData = await this.fetchMoreTasks();
					// 这里模拟加载数据
					const newData = await new Promise(resolve => {
						setTimeout(() => {
							resolve([
								{
									title: '新任务',
									current: 0,
									total: 1,
									points: 2,
									desc: '新增任务',
									buttonText: '去完成'
								}
							]);
						}, 1000);
					});
					
					if (newData.length > 0) {
						this.taskList.push(...newData);
					} else {
						this.hasMore = false;
					}
				} catch (error) {
					console.error('加载失败:', error);
				} finally {
					this.loadingMore = false;
				}
			},
			
			calcScrollHeight() {
				// 计算屏幕可用高度
				const systemInfo = uni.getSystemInfoSync();
				// 弹窗高度设置为屏幕高度的70%
				this.scrollHeight = systemInfo.windowHeight * 0.7;
			},
			
			handleTaskClick(item) {
				console.log('点击任务:', item);
				// 处理任务点击逻辑
				uni.showToast({
					title: `点击了${item.title}`,
					icon: 'none'
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		height: 1000rpx;
		background: #FFFFFF;
		border-radius: 32rpx 32rpx 0rpx 0rpx;
		overflow: hidden;
		display: flex;
		flex-direction: column;

		.bg {
			padding: 36rpx 24rpx;
			background: linear-gradient(180deg, #FFDFCD 0%, #FFFFFF 100%);
		}

		.row {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
		
		.mtb48 {
			margin-top: 48rpx;
			margin-bottom: 48rpx;
		}
		
		.plr24 {
			padding-left: 24rpx;
			padding-right: 24rpx;
		}
		
		.mr24 {
			margin-right: 24rpx;
		}
		
		.mr4 {
			margin-right: 4rpx;
		}
		
		.ml8 {
			margin-left: 8rpx;
		}
		
		.mb8 {
			margin-bottom: 8rpx;
		}

		.points {
			font-weight: 400;
			font-size: 28rpx;
			color: #333333;

			.num {
				font-weight: 500;
				font-size: 40rpx;
				color: #222426;
				margin-left: 4rpx;
			}
		}

		.shop {
			.shop-txt {
				font-weight: 500;
				font-size: 28rpx;
				color: #FF5C03;
				margin: 0 12rpx;
			}
		}

		.list {
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			padding: 0 24rpx;
			box-sizing: border-box;
			flex: 1;
			overflow: hidden;

			.list-item {
				width: 100%;
				margin-bottom: 32rpx;

				.button {
					width: 132rpx;
					height: 64rpx;
					background: #FF5C03;
					border-radius: 32rpx 32rpx 32rpx 32rpx;
					text-align: center;
					line-height: 64rpx;
					color: #FFFFFF;
					font-weight: 500;
					font-size: 28rpx;
				}

				.left {
					display: flex;

					.title {
						font-weight: 500;
						font-size: 30rpx;
						color: #333333;
					}

					.num {
						font-weight: 600;
						font-size: 28rpx;
						color: #FF5C03;
					}

					.txt {
						font-size: 24rpx;
						color: #999999;
					}
				}
			}
		}
		
		// 添加图标尺寸类
		.w40 { width: 40rpx; }
		.h40 { height: 40rpx; }
		.w428 { width: 428rpx; }
		.h64 { height: 64rpx; }
		.w48 { width: 48rpx; }
		.h48 { height: 48rpx; }
		.w24 { width: 24rpx; }
		.h24 { height: 24rpx; }
		.w88 { width: 88rpx; }
		.h88 { height: 88rpx; }
	}
</style>