<template>
	<view>
		<view class="tabr" :style="{top:headerTop}">
			<view :class="{on:typeClass=='valid'}" @tap="switchType('valid')">待领取({{couponValidList.length}})</view><view :class="{on:typeClass=='invalid'}"  @tap="switchType('invalid')">我的优惠券({{mycouponvalidList.length}})</view>
			<view class="border" :class="typeClass"></view>
		</view>
		<view class="place" ></view>
		<view class="list">
			<!-- 优惠券列表 -->
			<view class="sub-list valid" :class="subState">
				<view class="tis" v-if="couponValidList.length==0">暂无可领优惠券~</view>
				<view class="row" v-for="(row,index) in couponValidList" :key="index" >
					
					<!-- content -->
					<view class="carrier" :class="[typeClass=='valid'?theIndex==index?'open':oldIndex==index?'close':'':'']">
						<view class="left">
							<view class="title">
								{{row.lx}}
							</view>
							<view class="term">
								{{row.brq}} ~ {{row.erq}}
							</view>
							<view class="gap-top"></view>
							<view class="gap-bottom"></view>
						</view>
						<view class="right" @click="dogetcoupon(row.id)">
						 	<view class="ticket">
								<view class="num">
									{{row.jine}}
								</view>
								<view class="unit">元</view>
							</view>
							<view class="criteria">
								满{{row.manjine}}使用
							</view>
							<view class="use">
								立即领取
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="sub-list invalid" :class="subState">
				<view class="tis" v-if="mycouponvalidList.length==0">暂无优惠券哦~</view>
				<view class="row" v-for="(row,index) in mycouponvalidList" :key="index" >
					
					<!-- content -->
					<view class="carrier" :class="[typeClass=='invalid'?theIndex==index?'open':oldIndex==index?'close':'':'']" >
						<view class="left">
							<view class="title">
								{{row.lx}}
							</view>
							<view class="term">
								{{row.brq}} ~ {{row.erq}}
								<view v-if="row.number>1" class="shengyu">剩余{{row.number}}张</view>
							</view>
							<!-- <view class="icon shixiao">
								
							</view> -->
							<view class="gap-top"></view>
							<view class="gap-bottom"></view>
						</view>
						<view class="right invalid">
							<view class="ticket">
								<view class="num">
									{{row.jine}}
								</view>
								<view class="unit">
									元
								</view>
							</view>
							<view class="criteria">
								满{{row.manjine}}使用
							</view>
							<view class="use" v-if="fromchoose==1" >
								<span v-if="row.id==yid" @tap="chooseit(row.id,row.jine,row.manjine,0)" style="color: red;">取消</span>
								<span v-else @tap="chooseit(row.id,row.jine,row.manjine,1)">选择</span>
								</view>
							<view class="use" v-else>待使用</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>

	export default {
		data() {
			return {
				couponValidList:[],
				mycouponvalidList:[],
				headerTop:0,
				//控制滑动效果
				typeClass:'valid',
				subState:'',
				theIndex:null,
				oldIndex:null,
				isStop:false,
				fromchoose:0,//是否过来选择优惠券的
				allmoney:0,//订单金额
				yid:0,//已选中的优惠券id
			}
		},
		onPageScroll(e){
			
		},
		//下拉刷新，需要自己在page.json文件中配置开启页面下拉刷新 "enablePullDownRefresh": true
		onPullDownRefresh() {
		    setTimeout(function () {
		        uni.stopPullDownRefresh();
		    }, 1000);
		},
		onLoad(option) {
			this.fromchoose=parseInt(option.fromchoose);
			this.allmoney=parseInt(option.allmoney);
			this.yid=getApp().globalData.yid;
			//兼容H5下排序栏位置
			// #ifdef H5
				//定时器方式循环获取高度为止，这么写的原因是onLoad中head未必已经渲染出来。
				let Timer = setInterval(()=>{
					let uniHead = document.getElementsByTagName('uni-page-head');
					if(uniHead.length>0){
						this.headerTop = uniHead[0].offsetHeight+'px';
						clearInterval(Timer);//清除定时器
					}
				},1);
			// #endif
			this.couponlist();
			this.mycouponlist();
		},
		methods: {
			//领取优惠券
			dogetcoupon(id){
				uni.showLoading({title: '领取中...'});
				this.getcoupon(id)
			},
			//选择该优惠券
			chooseit(id,jine,maxjine,type){
				
				if(type==1){
					console.log(Number(parseFloat(this.allmoney*100)/100),'this.allmoney')
					console.log(Number(parseFloat(jine*100)/100),'jine')
					if(Number((parseFloat(maxjine*100)/100))>Number((parseFloat(this.allmoney*100)/100))){
						uni.showToast({
							title:'订单金额不满'+maxjine+'元，不可使用',icon:'none'
						})
					}else{
						getApp().globalData.yid=id;
						getApp().globalData.yhqjine=jine;
						getApp().globalData.useyhq=0;
						uni.navigateBack({
							delta:1
						})
					}
				}else{
					getApp().globalData.yid=0;
					getApp().globalData.yhqjine=0;
					getApp().globalData.useyhq=0;
					uni.navigateBack({
						delta:1
					})
				}
				
				
			},
			//获取优惠券列表
			async couponlist(){
				let getuserinfo=uni.getStorageSync('userinfo');
				let info= await this.$api.couponlist({action:'couponlist',uid:getuserinfo.uid,aid:getuserinfo.aid,aid:getuserinfo.aid});
				this.couponValidList=info.data;
			},
			//获取我的优惠券
			async mycouponlist(){
				let getuserinfo=uni.getStorageSync('userinfo');
				let info= await this.$api.mycouponlist({action:'mycouponlist',uid:getuserinfo.uid,aid:getuserinfo.aid});
				this.mycouponvalidList=info.data;
			},
			async getcoupon(id){
				let getuserinfo=uni.getStorageSync('userinfo');
				let info= await this.$api.getcoupon({action:'getcoupon',uid:getuserinfo.uid,id:id,aid:getuserinfo.aid});
				if(info.code==1){
					uni.hideLoading();
					uni.showToast({
						title:'领取成功',icon:'none',duration:2000
					})
					this.couponlist();
				}else{
					uni.showToast({
						title:'领取失败',icon:'fail',duration:1500
					})
				}
			},
			switchType(type){
				this.couponlist();
				this.mycouponlist();
				if(this.typeClass==type){
					return ;
				}
				uni.pageScrollTo({
					scrollTop:0,
					duration:0
				})
				this.typeClass = type;
				this.subState = this.typeClass==''?'':'show'+type;
				setTimeout(()=>{
					this.oldIndex = null;
					this.theIndex = null;
					this.subState = this.typeClass=='valid'?'':this.subState;
				},200)
			},
			
			
			
		}
	}
</script>
<style lang="scss">
	view{
		display: flex;
		flex-wrap: wrap;
		
	}
	page{position: relative;background-color: #f5f5f5;}
	@font-face {font-family:"HMfont-home";src:url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAYcAAsAAAAAC7AAAAXPAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDMgqKTIgRATYCJAMUCwwABCAFhG0HShufCciemjxtEyiE0MJUUYjN7gIePI911ftJqrY61RxhAeHWc1taR0BXaRAvS0cGxd7FXycwvQF4WLxnP3czaSZwFdHkm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/Mfh19p/c7nHRR5YK7XptuvF7QOvFX69hfpQ6srnigetMRz0rWDm1T2q8fa94jtnd66ybdVzz1LBxq05KW6YcWn07znwk6XVt/3aZ579c0C9R6X69ypu7NhkLTgT8i38db4t+V+B9o/ydc543/BHFkRMR/l33g7XQpbG4JycmBaVjYlou27bi3arLR0VrOSgl1B9y1Jggx2vQO/hKcFbmxc5mCmI61611R3x7ZmueuXdueyE6CR2/bO2cu5a+PmxrRp1L3JHT8o6/XIBCneoIDkQRII9yLpLvBKenqYu3JHobtpKAm5KaAX9q76sXoK+D99XnsCINpCVsB/+oHu/rvYjG+TOQ/62Nb86vnfh5kHD1Lmofs/A00KH24ZZN6vgEccMYbA1jlGa/e4Wq4j5YGAzxX/A9/y2Xcm8GAeeTKZ9yOTJ2egyEU2aDwKog2+JDj4VAQXjzpgKUH80T6JdCGBmABQnOEMhCgbQRHmHGiinEcb/H1wSOE9uERFg6WzJJ7Sp7AMsw3G08CiMZb3xroKVWFcg5WPC++JtaWBkRdE9GcyTD6Anm4e5cQlKWTMsUfXHuxmt24MVndRZYxjVyNJUrFTF1WgkbVa283Sdtzdq5vWtFrRIkOeBhgQAyyIMUg5PkgdI1QUmGC6Rb7y9fcQLDYSA4yOjjrnM4SBCT8/4okbHgT0pVQhddxLeVccWDbphmEGpFrMV5FB4tBMSE5UBenUvJEAMcJildoRYY0d3HFSNVW6ur5cfox5f8Gu9qkdUaLFiCMugWnDTI73+IGaGO6Lk55LyUwYCpERZwMAAA==') format('woff2');}
	.icon {
		font-family:"HMfont-home" !important;
		font-size:60upx;
		font-style:normal;
		color:#000000;
		&.jia {
			&:before{content:"\e641";}
		}
		&.jian {
			&:before{content:"\e643";}
		}
		&.shixiao {
			&:before{content:"\e669";}
		}
		
	}
	.hidden{
		display: none !important;
	}
	.place{
		width: 100%;
		height: 95upx;
	}
	.tabr{
		background-color: #fff;
		width: 100%;
		height: 95upx;
		padding: 0 3%;
		border-bottom: solid 1upx #dedede;
		position: fixed;
		top: 0;
		z-index: 10;
		view{
			width: 50%;
			height: 90upx;
			justify-content: center;
			align-items: center;
			font-size: 32upx;
			color: #999;
		}
		.on{
			color: #f06c7a;
		}
		.border{
			height: 4upx;
			background-color: #f06c7a;
			transition: all .3s ease-out;
			&.invalid{
				transform: translate3d(100%,0,0);
			}
		}
		
	}
	.list{
		width: 100%;
		display: block;
		position: relative;
	}
	@keyframes showValid {
		0% {transform: translateX(-100%);}100% {transform: translateX(0);}
	}
	@keyframes showInvalid {
		0% {transform: translateX(0);}100% {transform: translateX(-100%);}
	}
	.sub-list{
		&.invalid{
			position: absolute;
			top: 0;
			left:100%;
			display: none;
		}
		&.showvalid{
			display: flex;
			animation: showValid 0.20s linear both;
		}
		&.showinvalid{
			display: flex;
			animation: showInvalid 0.20s linear both;
		}
		width: 100%;
		padding: 20upx 0 120upx 0;
		.tis{
			width: 100%;
			height: 60upx;
			justify-content: center;
			align-items: center;
			color: #C0C0C0;
			font-size: 32upx;
		}
		.row{
			width: 92%;
			height: 24vw; 
			margin: 20upx auto 10upx auto;
			border-radius: 8upx;
			// box-shadow: 0upx 0 10upx rgba(0,0,0,0.1);
			align-items: center;
			position: relative;
			overflow: hidden;
			z-index: 4;
			border: 0;
			.menu{
				.icon{
					color: #fff;
					font-size:50upx;
				}
				position: absolute;
				width: 28%;
				height: 100%;
				right: 0;
				justify-content: center;
				align-items: center;
				background-color: red;
				color: #fff;
				z-index: 2;
			}
			.carrier{
				@keyframes showMenu {
					0% {transform: translateX(0);}100% {transform: translateX(-28%);}
				}
				@keyframes closeMenu {
					0% {transform: translateX(-28%);}100% {transform: translateX(0);}
				}
				&.open{
					animation: showMenu 0.25s linear both;
				}
				&.close{
					animation: closeMenu 0.15s linear both;
				}
				background-color: #fff;
				position: absolute;
				width: 100%;
				padding: 0 0;
				height: 100%;
				z-index: 3;
				flex-wrap: nowrap;
				.left{
					width: 100%;
					.title{
						padding-top: 3vw;
						width: 90%;
						margin: 0 5%;
						font-size: 36upx;
					}
					.term{
						width: 90%;
						margin: 0 5%;
						font-size: 26upx;
						color: #999;
						.shengyu{
							font-size: 30upx;
							color: red;
						}
					}
					position: relative;
					.gap-top,.gap-bottom{
						position: absolute;
						width: 20upx;
						height: 20upx;
						right: -10upx;
						border-radius: 100%;
						background-color: #f5f5f5;
					}
					.gap-top{
						top: -10upx;
					}
					.gap-bottom{
						bottom: -10upx;
					}
					.shixiao{
						position: absolute;
						right: 20upx;
						font-size: 150upx;
						z-index: 6;
						color: rgba(153,153,153,0.2)
					}
				}
				.right{
					flex-shrink: 0;
					width: 28%;
					color: #fff;
					background:linear-gradient(to right,#ec625c,#ee827f);
					&.invalid{
						background:linear-gradient(to right,#4D86C1,#5886B7);
						.use{
							color: #5886B7;
						}
					}
					justify-content: center;
					.ticket,.criteria{width: 100%;}
					.ticket{
						padding-top: 1vw;
						justify-content: center;
						align-items: baseline;
						height: 6vw;
						.num{
							font-size: 42upx;
							font-weight: 600;
						}
						.unit{
							font-size: 24upx;
						}
					}
					.criteria{
						justify-content: center;
						
						font-size: 28upx;
					}
					.use{
						width: 50%;
						height: 40upx;
						justify-content: center;
						align-items: center;
						font-size: 24upx;
						background-color: #fff;
						color: #ee827f;
						border-radius: 40upx;
						padding: 0 10upx;
					}
				}
			}
			
		}
	}
	
</style>
