<template>
	<view>
		<view class="tabr" :style="{top:headerTop}">
			<!-- ({{couponcout}}) -->
			<view :class="{on:typeClass=='valid'}" @tap="switchType('valid')">待使用</view>
			<view :class="{on:typeClass=='invalid'}" @tap="switchType('invalid')">已使用</view>
			<view :class="{on:typeClass=='noinvalid'}" @tap="switchType('noinvalid')">已过期</view>
		</view>
		<view class="place"></view>
		<view class="list">
			<!-- 优惠券列表 -->
			<view class="sub-list" v-if="typeClass == 'valid'">
				<view class="tis" v-if="couponValidList.length==0">
					<image mode="widthFix" style="width: 70vw;" :src="imagewxUrl+imgadres"></image>
				</view>
				<view class="row" v-for="(row,index) in couponValidList" :key="index" @click="jumpcoupons(row)">
					<!-- content -->
					<view class="carrier">
						<view class="left">
							<view class="title">
								{{row.highDiscount.discountName}}
							</view>
							<view class="term">
								截止时间:{{row.useEndTime | formatDate('-')}}
							</view>
						</view>
						<view class="right">
							<view class="ticket">
								<view class="num" v-if="row.highDiscount.discountType == 3">
									{{row.highDiscount.discountPrice * 10}}
								</view>
								<view class="num" v-else>
									{{row.highDiscount.discountPrice}}
								</view>
								<view class="unit" v-if="row.highDiscount.discountType == 3">
									折
								</view>
								<view class="unit" v-else>
									元
								</view>
							</view>
							<!-- <view class="criteria">
								{{row.highDiscount.discountName}}
							</view> -->
							<view class="use">
								去使用
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- //已使用 -->
			<view class="sub-list" v-if="typeClass == 'invalid'">
				<view class="tis" v-if="couponinvalidList.length==0">
					<image mode="widthFix" style="width: 70vw;" :src="imagewxUrl+imgadres"></image>
				</view>
				<view class="row" v-for="(row,index) in couponinvalidList" :key="index">
					<!-- content -->
					<view class="carrier">
						<view class="left">
							<view class="title">
								{{row.highDiscount.discountName}}
							</view>
							<view class="term">
								使用时间:{{row.useTime | formatDate('-')}}
							</view>
						</view>
						<view class="right invalid">
							<view class="ticket">
								<view class="num">
									{{row.highDiscount.discountPrice}}
								</view>
								<view class="unit">
									元
								</view>
							</view>
							<view class="criteria">
								{{row.highDiscount.discountName}}
							</view>
							<view class="use">
								已使用
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 已过期 -->
			<view class="sub-list" v-if="typeClass == 'noinvalid'">
				<view class="tis" v-if="couponnoinvalidList.length==0">
					<image mode="widthFix" style="width: 70vw;" :src="imagewxUrl+imgadres"></image>
				</view>
				<view class="row" v-for="(row,index) in couponnoinvalidList" :key="index">
					<!-- content -->
					<view class="carrier">
						<view class="left">
							<view class="title">
								{{row.highDiscount.discountName}}
							</view>
							<view class="term">
								过期时间:{{row.useEndTime | formatDate('-')}}
							</view>
						</view>
						<view class="right invalid">
							<view class="ticket">
								<view class="num">
									{{row.highDiscount.discountPrice}}
								</view>
								<view class="unit">
									元
								</view>
							</view>
							<view class="criteria">
								{{row.highDiscount.discountName}}
							</view>
							<view class="use">
								已过期
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getUserDiscountList
	} from '../../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			return {
				couponValidList: [],
				couponinvalidList: [],
				couponnoinvalidList: [],
				imagewxUrl: app.globalData.imageWxImg,
				imgadres: 'noorder.png',
				headerTop: 0,
				//控制滑动效果
				typeClass: 'valid',
				subState: '',
				theIndex: null,
				oldIndex: null,
				isStop: false,
				pageNum: 1,
				pageSize: 10,
				isNoMoreData: false,
				loadingText: '',
				couponcout: 0
			}
		},
		onPageScroll(e) {

		},
		//下拉刷新，需要自己在page.json文件中配置开启页面下拉刷新 "enablePullDownRefresh": true
		onPullDownRefresh() {
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onLoad() {
			this.getUserDiscountList(1);
			//兼容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
		},
		filters: {
			//过滤器 用于格式化时间
			formatDate: function(value, spe = '/') {
				let data = new Date(value);
				let year = data.getFullYear();
				let month = data.getMonth() + 1;
				let day = data.getDate();
				let h = data.getHours();
				let mm = data.getMinutes();
				let s = data.getSeconds();
				month = month >= 10 ? month : "0" + month;
				day = day >= 10 ? day : "0" + day;
				h = h >= 10 ? h : "0" + h;
				mm = mm >= 10 ? mm : "0" + mm;
				s = s >= 10 ? s : "0" + s;
				return `${year}${spe}${month}${spe}${day} ${h}:${mm}:${s}`;
			}
		},
		onReachBottom() {
			if (this.typeClass == 'valid') {
				this.getUserDiscountList(1);
			} else if (this.typeClass == 'invalid') {
				this.getUserDiscountList(2);
			} else {
				this.getUserDiscountList(0);
			}
		},
		methods: {
			//查询我的优惠券
			getUserDiscountList(item) {
				uni.showLoading({
					title: '加载中...'
				})
				if (this.isNoMoreData) {
					uni.hideLoading()
					this.loadingText = '到底了';
					return false;
				}
				let pagenum = this.pageNum;
				let params = {
					pageNum: pagenum,
					status: item,
					pageSize: this.pageSize
				}
				getUserDiscountList(params).then(res => {
					if (res.return_code == '000000') {
						uni.hideLoading();
						this.isNoMoreData = res.return_data.list.length == this.pageSize ? false : true;
						if (res.return_data.total == (this.pageNum * this.pageSize)) {
							this.isNoMoreData = true;
						}
						if (this.typeClass == 'valid') {
							this.couponValidList = this.couponValidList.concat(res.return_data.list);
							this.couponcout = res.return_data.total;
						} else if (this.typeClass == 'invalid') {
							this.couponinvalidList = this.couponinvalidList.concat(res.return_data.list);
						} else {
							this.couponnoinvalidList = this.couponnoinvalidList.concat(res.return_data.list);
						}
						this.pageNum = res.return_data.list.length == this.pageSize ? ++pagenum : pagenum;
					} else {
						this.couponValidList = [];
						this.couponinvalidList = [];
						this.couponnoinvalidList = [];
						uni.hideLoading()
					}
				})
			},
			//跳转详情
			jumpcoupons(e) {
				if (e.highDiscount.useScope == 3) {
					uni.navigateTo({
						url: '../../../subPages/recharge/recharge'
					})
				} else if (e.highDiscount.useScope == 4) {
					app.globalData.distinguishid = 1;
					uni.navigateTo({
						url: '/qianzhu-KFC/buffet-order/buffet-order'
					})
				} else if (e.highDiscount.useScope == 5) {
					app.globalData.distinguishid = 2;
					uni.navigateTo({
						url:  '/qianzhu-KFC/buffet-order/buffet-order'
					})
				} else if (e.highDiscount.useScope == 6) {
					uni.navigateTo({
						url: '/member-Recharge/choicepage/choicepage'
					})
				} else{
					uni.navigateTo({
						url: '../mineCouponsDestails/mineCouponsDestails?id=' + e.id
					})
				}

			},
			switchType(type) {
				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)
				if (this.typeClass == 'valid') {
					this.pageNum = 1;
					this.couponValidList = []
					this.isNoMoreData = false;
					this.getUserDiscountList(1);
				} else if (this.typeClass == 'invalid') {
					this.pageNum = 1;
					this.couponinvalidList = []
					this.isNoMoreData = false;
					this.getUserDiscountList(2);
				} else {
					this.pageNum = 1;
					this.couponnoinvalidList = []
					this.isNoMoreData = false;
					this.getUserDiscountList(0);
				}
			}
		}
	}
</script>
<style lang="scss">
	view {
		display: flex;
		flex-wrap: wrap;

	}

	page {
		position: relative;
		background-color: #f5f5f5;
	}

	.hidden {
		display: none !important;
	}

	.place {
		width: 100%;
		height: 95upx;
	}

	.tabr {
		background-color: #fff;
		width: 94%;
		height: 95upx;
		padding: 0 3%;
		border-bottom: solid 1upx #dedede;
		position: fixed;
		top: 0;
		z-index: 10;

		view {
			width: 33.3%;
			height: 90upx;
			justify-content: center;
			align-items: center;
			font-size: 32upx;
			color: #999;
		}

		.on {
			color: #0083f5;
			border-bottom: 2px solid #0083f5;
		}

	}

	.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%);
		}
	}

	@keyframes shownoInvalid {
		0% {
			transform: translateX(0);
		}

		100% {
			transform: translateX(-200%);
		}
	}

	.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;
		}

		&.shownoinvalid {
			display: flex;
			animation: shownoInvalid 0.20s linear both;
		}

		width: 100%;
		padding: 20upx 0 120upx 0;

		.tis {
			width: 100%;
			height: 60upx;
			justify-content: center;
			align-items: center;
			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;
					}

					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, #aaa, #999);

						.use {
							color: #aaa;
						}
					}

					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>
