<template>
	<view class="common_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title="我的专享卷" bgColor="#FFFFFF"></cus-header>
		<view class="tab adf">
			<view class="tab-pre adfacjc" :class="{'active':tidx===0}" @click="changeTab(0,0)">未使用</view>
			<view class="tab-pre adfacjc" :class="{'active':tidx===1}" @click="changeTab(1,1)">已使用</view>
			<view class="tab-pre adfacjc" :class="{'active':tidx===2}" @click="changeTab(2,-1)">已过期</view>
		</view>
		<template v-if="list.length">
			<view class="list">
				<up-list @scrolltolower="scrolltolower" style="height: 100%;">
					<up-list-item v-for="(item, index) in list" :key="index">
						<view class="box" :style="{'margin-top':index===0?0:'24rpx'}" :class="{'grey':tidx!==0}">
							<image class="box-img" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/10/09/4320f839-19f2-484f-a827-f2b403bb00ff.png" v-if="tidx===1"></image>
							<image class="box-img" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/10/09/106a02e0-e979-42a5-979b-0d934d6690f6.png" v-if="tidx===2"></image>
							<view class="box-top" @click="handleCheck(item,index)">
								<template v-if="queryParams.activityId&&tidx===0">
									<image class="box-check" v-if="item.check" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/87b5b244-d14f-43cd-991b-4ac9f48d909e.png"></image>
									<image class="box-check" v-else src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/12/b8a5cabd-57f8-4ad7-9677-f6372423c50a.png"></image>
								</template>
								<view class="box-top-title">活动专享卷</view>
								<view class="box-top-tip">{{item?.activityName||''}}</view>
								<view class="box-top-date adfacjb">
									<view class="box-top-date-left">有效期至{{item?.expiredTime||''}}</view>
									<view class="box-top-date-right" v-if="item?.state===0&&!queryParams.activityId" @click="handleUse(item)">立即使用</view>
								</view>
							</view>
							<view class="box-bottom adf" @click="handleExpand(item,index)">
								<view class="box-bottom-left">
									<view class="h" v-if="!item.select">本券仅限报名指定活动使用，不可用于其他场景</view>
									<view class="s" v-else>
										<view class="p">1.适用范围：本券仅限报名指定活动使用；</view>
										<view class="p">2.使用方式：在报名指定活动时将自动抵扣爱心值；</view> 
										<view class="p">3.不退不换：券一经使用，不可退还、不可取消；</view>
										<view class="p">4.有效期：请于券面标注日期前使用，逾期自动失效。</view>
									</view>
								</view>
								<view class="box-bottom-right">
									<up-icon name="arrow-right" color="#CBCBCB" size="32rpx" v-if="!item.select"></up-icon>
									<up-icon name="arrow-up" color="#CBCBCB" size="32rpx" v-else></up-icon>
								</view>
							</view>
						</view>
					</up-list-item>
				</up-list>
			</view>
			<view class="confirm" v-if="queryParams.activityId&&tidx===0" @click="confirmCheck">确定，已选择{{selectNum}}张专享券</view>
		</template>
		<view class="dataEmpty" v-else>
			<page-empty text='没有券'></page-empty>
		</view>
	</view>
</template>

<script setup name="">
	import CusHeader from '@/components/CusHeader/index.vue'
	import PageEmpty from '@/components/pageEmpty/index.vue'
	import { ref, getCurrentInstance } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	const { proxy } = getCurrentInstance()
	
	const tidx = ref(0)
	const queryParams = ref({
		page:1,
		limit:10,
		activityId:'',
		state:0,
		userId:''
	})
	const isOver = ref(false)
	const list = ref([])
	const selectNum = ref(0)

	const changeTab = (index,status) => {
		tidx.value = index;
		queryParams.value.state = status;
		init()
		getList()
	}
	
	const scrolltolower = () => {
		if(isOver.value) return
		getList()
	}
	
	const handleCheck = (item,index) => {
		list.value[index].check = !list.value[index].check;
		selectNum.value = list.value.filter(l=>l.check).length;
	}
	
	const handleExpand = (item,index) => {
		list.value[index].select = !list.value[index].select;
	}
	
	const init = () =>{
		queryParams.value.page = 1;
		isOver.value = false;
		list.value = [];
	}
	
	const getList = () => {
		proxy.$api.get('/core/activity/coupon/page',queryParams.value).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			list.value = res.data.list;
			list.value.forEach(l=>{
				l.select = false;
				l.check = false;
			})
			queryParams.value.page++;
			if(res.data.list.length===0) isOver.value = true;
		})
	}
	
	const handleUse = item => {
		uni.navigateTo({
			url:'/pagesHome/activityDetail?id='+item.activityId
		})
	}
	
	const confirmCheck = () => {
		if(list.value.filter(l=>l.check).length===0) return proxy.$showToast('请至少选择一张专享券')
		let selectIds = list.value.filter(l=>l.check).map(l=>l.id);
		proxy.getOpenerEventChannel().emit('selectTickets',selectIds);
		uni.navigateBack();
	}
	
	onLoad((options)=>{
		queryParams.value.activityId = options?.activityId||'';
		queryParams.value.userId = JSON.parse(uni.getStorageSync('userInfo')).id;
		getList()
	})
</script>

<style scoped lang="scss">
	.common_page{
		padding: 0 0 40rpx;
		.tab{
			width: 100%;
			height: 110rpx;
			background: #FFFFFF;
			&-pre{
				width: 50%;
				height: 110rpx;
				position: relative;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #6B7280;
				line-height: 28rpx;
				&.active{
					font-weight: bold;
					font-size: 32rpx;
					color: #252525;
					line-height: 32rpx;
					&::after{
						content: '';
						width: 120rpx;
						height: 6rpx;
						background: #252525;
						border-radius: 3rpx;
						position: absolute;
						left: 50%;
						margin-left: -60rpx;
						bottom: 6rpx;
					}
				}
			}
		}
		
		.list{
			margin-top: 20rpx;
			padding: 0 24rpx;
			flex: 1;
			overflow: auto;
			
			.box{
				position: relative;
				&-img{
					width: 128rpx;
					height: 118rpx;
					position: absolute;
					top: 0;
					right: 0;
				}
				&-check{
					width: 42rpx;
					height: 42rpx;
					border-radius: 50%;
					position: absolute;
					top: 30rpx;
					right: 30rpx;
				}
				&-top{
					border-radius: 24rpx 24rpx 0 0;
					padding: 40rpx 24rpx 24rpx;
					background: linear-gradient(90deg,#FBE9CA 50%,#FCF3E4 100%);
					&-title{
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 36rpx;
						color: #7E541A;
						line-height: 36rpx;
					}
					&-tip{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #AA7633;
						line-height: 24rpx;
						margin-top: 20rpx;
					}
					&-date{
						margin-top: 42rpx;
						&-left{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #AA7633;
							line-height: 40rpx;
						}
						&-right{
							padding: 8rpx 20rpx;
							background: #AA7633;
							border-radius: 24rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #FFFFFF;
							line-height: 33rpx;
						}
					}
				}
				&-bottom{
					border-radius: 0 0 24rpx 24rpx;
					background: #FFFFFF;
					padding: 16rpx 24rpx;
					&-left{
						width: calc(100% - 54rpx);
						.h{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #A4A4A4;
							line-height: 40rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}
						.s{
							.p{
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #A4A4A4;
								line-height: 44rpx;
							}
						}
					}
					&-right{
						width: 32rpx;
						height: 32rpx;
						padding-top: 4rpx;
					}
				}
				&.grey{
					.box-top{
						background: #FFFFFF;
						&-title{
							color: #657588;
						}
						&-tip,&-date-left{
							color: #A4A4A4;
						}
					}
				}
			}
		}	
	
		.confirm{
			width: calc(100% - 100rpx);
			padding: 0 40rpx;
			margin: 40rpx auto 64rpx;
			display: inline-block;
			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;
		}
	
		.empty{
			flex: 1;
			image{
				width: 184rpx;
				height: 113rpx;
			}
			text{
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 32rpx;
				color: #252525;
				line-height: 45rpx;
				margin-top: 60rpx;
			}
		}
	}
</style>