<template>
	<view class="common_page adffc" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title="报名信息填写" bgColor="transparent"></cus-header>
		<image src="https://oss.familydaf.cn/sxsnfile/20251218/ff31a76e0e1148378a318382b998a51f.png" class="top_bg_img" mode="widthFix"></image>
		<view class="box box1">
			<view class="box1-top">
				<view class="title">{{activity?.activityName||''}}</view>
				<view class="tip">活动时间：{{activity?.activityStartTime||''}} 到 {{activity?.activityEndTime||''}}</view>
				<view class="tip">活动地点：{{activity?.provinceName||''}}{{activity?.cityName||''}}{{activity?.address||''}}</view>
			</view>
			<view class="box1-bottom adfac">
				<image src="https://oss.familydaf.cn/sxsnfile/20251218/0a68f263fc1b464cba97b7a8fe130ccc.png"></image>
				<text>报名时间截止前可取消并返还{{activityLimitCfg[activity?.activityLimit]}}，截止后不可取消。</text>
			</view>
		</view>
		<view class="box box2">
			<view class="box2-title">报名信息</view>
			<view class="box2-members" v-if="selectMemberList.length">
				<view class="box2-members-pre adfacjb" v-for="(item,index) in selectMemberList" :key="index">
					<view class="box2-members-pre-left">
						<view class="ltop adfac">
							<view class="name">{{item.name}}</view>
							<image class="sex" v-if="item.gender==1" src="https://oss.familydaf.cn/sxsnfile/20251218/473d5677fbdb4106acdb9a163377d27f.png"></image>
							<image class="sex" v-else-if="item.gender==0" src="https://oss.familydaf.cn/sxsnfile/20251218/02bc40a1c47b40f1a36b55cd0553211c.png"></image>
							<view class="age" :class="{'women':item.gender==1,'man':item.gender==0}">{{item.age}}岁</view>
						</view>
						<view class="lbottom">
							身份证 {{item.idCardCopy}}
						</view>
					</view>
					<view class="box2-members-pre-right" @click="deleteMember(item,index)">
						<image src="https://oss.familydaf.cn/sxsnfile/20251218/f94c1d060eca4506a7e645919d158288.png"></image>
					</view>
				</view>
			</view>
			<view class="box2-add adfacjc" @click="handleAdd">
				<image src="https://oss.familydaf.cn/sxsnfile/20251218/5ae3736e1ca74d739b54578af5c7a56d.png"></image>
				<text>添加</text>
			</view>
		</view>
		<view class="box box3">
			<view class="box3-pre adfacjb">
				<view class="box3-pre-left">联系人姓名</view>
				<view class="box3-pre-right">
					<up-input v-model="submitDto.contact" border="none" style="font-size: 30rpx;color: #151B29;text-align: right;" placeholder="请输入联系人姓名"></up-input>
				</view>
			</view>
			<view class="box3-pre adfacjb">
				<view class="box3-pre-left">联系人电话</view>
				<view class="box3-pre-right">
					<up-input v-model="submitDto.contactPhone" border="none" style="font-size: 30rpx;color: #151B29;text-align: right;" placeholder="请输入联系人电话"></up-input>
				</view>
			</view>
		</view>
		<view class="box box4 adfacjb" v-if="activity?.activityLimit==2" @click="handleSelectTicket">
			<view class="box4-left">专享券抵扣</view>
			<view class="box4-right adfac">
				<text>{{ticketText}}</text>
				<image src="https://oss.familydaf.cn/sxsnfile/20251218/48d65d353fd748ccb33c0fa29c1cecb0.png"></image>
			</view>
		</view>
		<view class="cbottom">
			<view class="cbottom-top adfac" @click="changeAgree">
				<image v-if="agree" src="https://oss.familydaf.cn/sxsnfile/20251218/c11b9a1b56f34e1189621f4270f0349a.png"></image>
				<image v-else src="https://oss.familydaf.cn/sxsnfile/20251218/2a2f7bdefb474a3e93faa00aef6d0e1f.png"></image>
				<text>我已知晓并同意活动组织方获取以上我所提交的报名信息。</text>
			</view>
			<view class="cbottom-btn adfacjb">
				<view class="cbottom-btn-left adffcac">
					<template v-if="activity?.activityLimit<3">
						<view class="heart adfac">
							<image src="https://oss.familydaf.cn/sxsnfile/20251218/8ee2fca2eb514e8fb10507ef3a0ed7d3.png"></image>
							<text>{{payValue}}</text>
						</view>
						<view class="text">{{activityLimitCfg[activity?.activityLimit]||''}}</view>
					</template>
					<template v-else>
						<view class="free">免费</view>
					</template>
				</view>
				<view class="cbottom-btn-right" @click="handleApply">确定报名</view>
			</view>
		</view>
	</view>
</template>

<script setup name="">
	import CusHeader from '@/components/CusHeader/index.vue'
	import { ref, getCurrentInstance, onMounted } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	const { proxy } = getCurrentInstance()
	
	const activity = ref(null)
	const activityLimitCfg = ref({
		1:'爱心值',
		2:'专享券',
		3:'免费'
	})
	const selectMemberList = ref([])
	const submitDto = ref({
		activityId:'',
		contact:'',
		contactPhone:'',
		couponIds:[],
		memberIds:[],
		userId:''
	})
	const agree = ref(false)
	const payValue = ref(0)
	const userLoveTicket = ref(null)
	const selectCouponIds = ref([])
	const ticketText = ref('暂无可用专享券')
	
	const handleAdd = () => {
		uni.navigateTo({
			url:'/pagesHome/applyMember',
			events:{
				'selectMembers': data => {
					selectMemberList.value = data;
					payValue.value = selectMemberList.value.length*activity.value?.valueLimit;
				}
			}
		})
	}
	
	const handleSelectTicket = () => {
		uni.navigateTo({
			url:'/pagesMy/exclusiveScroll?activityId='+activity.value?.id,
			events:{
				'selectTickets': data => {
					selectCouponIds.value = data;
					ticketText.value = `已选${data?.length||0}张，剩余${userLoveTicket.value?.couponIds?.length-data?.length}张可用`;
				}
			}
		})
	}
	
	const handleApply = async () => {
		if(selectMemberList.value.length===0) return proxy.$showToast('请至少添加一位报名人员')
		if(!submitDto.value.contact) return proxy.$showToast('请输入联系人姓名')
		if(!proxy.$reg.mobile(submitDto.value.contactPhone)) return proxy.$showToast('请输入正确的联系电话')
		submitDto.value.memberIds = selectMemberList.value.map(m=>m.id);
		submitDto.value.activityId = activity.value.id;
		submitDto.value.userId = JSON.parse(uni.getStorageSync('userInfo')).id;
		if(activity.value?.activityLimit==2){
			let smnum = selectMemberList.value.length;
			let stnum = selectCouponIds.value.length;
			if(stnum<smnum) return proxy.$showToast(`请选择${smnum}张专享券`)
		}
		submitDto.value.couponIds = selectCouponIds.value;
		if(!agree.value) return proxy.$showToast('请勾选已知晓并同意')
		
		proxy.$api.post('/core/activity/signup/signup',submitDto.value).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			uni.redirectTo({
				url:'/pagesHome/applyResult'
			})
		})
	}
	
	const getUserLoveTicket = () => {
		let userId = JSON.parse(uni.getStorageSync('userInfo'))?.id;
		proxy.$api.get(`/core/activity/signup/userAsset/${userId}/${activity.value?.id}`).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			userLoveTicket.value = res.data;
			ticketText.value = `剩余${res.data?.couponIds?.length||0}张可用`;
		})
	}
	
	const changeAgree = () => {
		agree.value = !agree.value;
	}
	
	const deleteMember = (item,index) => {
		selectMemberList.value.splice(index,1);
		payValue.value = selectMemberList.value.length*activity.value?.valueLimit;
	}
	
	onLoad(options=>{
		activity.value = options.activity&&JSON.parse(decodeURIComponent(options.activity))
	})
	
	onMounted(()=>{
		getUserLoveTicket()
	})
</script>

<style scoped lang="scss">
	::v-deep .u-input__content__field-wrapper__field{
		text-align: right !important;
	}
	
	.common_page{
		padding-bottom: 248rpx;
		
		.box{
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			position: relative;
		}
		
		.box1{
			&-top{
				padding: 36rpx 24rpx 24rpx;
				border-bottom: 1rpx solid #E5E7EB;
				&>.title{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #151B29;
					line-height: 32rpx;
				}
				&>.tip{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #666666;
					line-height: 24rpx;
					margin-top: 30rpx;
				}
			}
			&-bottom{
				padding: 26rpx 24rpx;
				image{
					width: 20rpx;
					height: 20rpx;
				}
				text{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #C9A771;
					line-height: 24rpx;
					margin-left: 10rpx;
				}
			}
		}
	
		.box2{
			padding: 40rpx 24rpx 48rpx;
			&-title{
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #151B29;
				line-height: 36rpx;
				margin-bottom: 26rpx;
				display: inline-block;
			}
			&-members{
				&-pre{
					padding: 36rpx 0;
					box-shadow: inset 0rpx -1rpx 0rpx 0rpx #F2F2F2;
					&-left{
						.ltop{
							.name{
								font-family: PingFang-SC, PingFang-SC;
								font-weight: bold;
								font-size: 32rpx;
								color: #151B29;
								line-height: 32rpx;
							}
							.sex{
								width: 44rpx;
								height: 32rpx;
								margin-left: 20rpx;
							}
							.age{
								border-radius: 13rpx;
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 20rpx;
								line-height: 24rpx;
								padding: 4rpx 10rpx;
								margin-left: 13rpx;
								&.women{
									background: rgba(244,101,122,0.14);
									color: #F4657A;
								}
								&.man{
									background: rgba(5,169,254,0.12);
									color: #05A9FE;
								}
							}
						}
						.lbottom{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #989998;
							line-height: 24rpx;
							margin-top: 24rpx;
						}
					}
					&-right{
						width: 36rpx;
						height: 36rpx;
						image{
							width: 100%;
							height: 100%;
						}
					}
				}
			}
			&-add{
				width: 100%;
				height: 78rpx;
				background: rgba(112,207,82,0.08);
				border-radius: 24rpx;
				border: 1rpx dotted #70CF52;
				margin-top: 40rpx;
				image{
					width: 36rpx;
					height: 36rpx;
				}
				text{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #70CF52;
					line-height: 40rpx;
					margin-left: 10rpx;
				}
			}
		}
	
		.box3{
			&-pre{
				padding: 36rpx 24rpx;
				&:first-child{
					box-shadow: inset 0rpx -1rpx 0rpx 0rpx #ECEEF5;
				}
				&-left{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #151B29;
					line-height: 36rpx;
				}
			}
		}
	
		.box4{
			padding: 36rpx 24rpx;
			&-left{
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #151B29;
				line-height: 36rpx;
			}
			&-right{
				text{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #989998;
					line-height: 33rpx;
				}
				image{
					width: 32rpx;
					height: 32rpx;
					margin-left: 20rpx;
				}
			}
		}
	
		.cbottom{
			width: 100%;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			&-top{
				padding: 0 30rpx;
				image{
					width: 36rpx;
					height: 36rpx;
				}
				text{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #989998;
					line-height: 33rpx;
					margin-left: 16rpx;
				}
			}
			&-btn{
				background: #FFFFFF;
				padding: 20rpx 30rpx 44rpx 36rpx;
				margin-top: 40rpx;
				&-left{
					.heart{
						image{
							width: 30rpx;
							height: 30rpx;
						}
						text{
							font-family: PingFang-SC, PingFang-SC;
							font-weight: bold;
							font-size: 36rpx;
							color: #F4657A;
							line-height: 36rpx;
							margin-left: 2rpx;
						}
					}
					.text{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #252525;
						line-height: 24rpx;
						margin-top: 11rpx;
					}
					.free{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 32rpx;
						color: #252525;
						line-height: 32rpx;
					}
				}
				&-right{
					width: 528rpx;
					height: 90rpx;
					background: #B7F358;
					border-radius: 45rpx;
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #151B29;
					line-height: 90rpx;
					text-align: center;
					letter-spacing: 2rpx;
				}
			}
		}
	}
</style>