<template>
	<view class="coupon-swiper">
		<u-tabs :list="couponGroupList" name="gc_name" :is-scroll="true" :current="couponCurrent" @change="couponTabsChange" active-color="#FF780F" :show-bar="false" height="88" font-size="24"  inactive-color="#333333"></u-tabs>
		<swiper :current="swiperCouponCurrent" @animationfinish="couponAnimationFinish" :style="{ height: swiperHeight }">
			<swiper-item class="swiper-coupon-item" v-for="(_, i) in couponGroupList" :key="i">
				<scroll-view scroll-y style="height: 100%;" @scrolltolower="onreachBottom" class="coupon-scroll">
					<view v-for="(coupon, index) in couponList[i]" :key="index" class="coupon-item">
						<Coupon :couponInfo="coupon" :status='0' :type="0" @exchange="exchangeCoupon($event)"></Coupon>
					</view>
					<u-empty text="暂无优惠券" mode="coupon" color="#000" v-if="!couponList[i] || !couponList[i].length"></u-empty>
					<u-loadmore :status="loadStatus" bgColor="#FFF" margin-bottom="60" v-if="couponList[i] && couponList[i].length>=pageSize" @loadmore="onreachBottom"></u-loadmore>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
import Coupon from "@/components/mine/coupon/index";
export default {
	data() {
		return {
			pageSize: 5, // 页面显示的条数
			swiperHeight: '',
			couponCurrent: 0,
			swiperCouponCurrent: 0,
			couponGroupList: [],
			couponList: [],
			page: 0,
			loadStatus: 'loadmore',
			timer: true,
		}
	},
	components: {
		Coupon
	},
	created() {
		this.setViewHeight();
		this.getGoodsClass();
	},
	watch: {
		couponCurrent(index) {
			const id = this.couponGroupList[index].gc_id;
			this.getCouponList({ gc_id: id, load: 'reload' });
		},
	},
	methods: {
		onreachBottom() {
			if(!this.timer) return false;
			this.loadStatus = "loading";
			this.page++;
			this.getCouponList({
				gc_id: this.couponGroupList[this.couponCurrent].gc_id,
				load: 'loadmore',
			}).then(length => {
				if(length == 0) {
					this.page--;
					this.loadStatus = 'nomore';
				} else {
					this.loadStatus = 'loading';
				}
			}).catch(() => {
				this.loadStatus = "nomore";
				this.page--;
			})
		},
		getGoodsClass() {
			this.$u.api.getGoodsClass().then(res => {
				if(res.errCode == 0) {
					this.couponGroupList = res.data;
					this.getCouponList({ gc_id: this.couponGroupList[0].gc_id, load: 'reload' });
				}
			})
		},
		async getCouponList({ gc_id, load }) {
			const res = await this.$u.api.getCouponList({
				page: this.page,
				gc_id: gc_id,
			})
			this.timer = true;
			if(res.errCode == 0) {
				if(load == 'reload') this.couponList[this.couponCurrent] = res.data;
				else if(load == 'loadmore') this.couponList[this.couponCurrent].push(...res.data);
			}
			this.$forceUpdate();
			return res.data.length;
		},
		exchangeCoupon(id) {
			// console.log(id);
			
		},
		couponTabsChange(index) {
			this.couponCurrent = index;
			this.swiperCouponCurrent = this.couponCurrent;
		},
		couponAnimationFinish(e) {
			const current = e.detail.current;
			this.swiperCouponCurrent = current;
			this.couponCurrent = current;
		},
		setViewHeight() {
			const res = uni.getSystemInfoSync();
			this.swiperHeight = res.windowHeight - ((88 + 10 + 88) * (res.windowWidth / 750)) + 'px';
		},
	},
};
</script>
<style lang="scss" scoped>
.coupon-swiper {
	.classify-coupon {
		white-space: nowrap;
		width: 100%;
		height: 88rpx;
		background-color: #ffffff;
		margin-bottom: 2rpx;
		.classify-item {
			display: inline-block;
			line-height: 88rpx;
			text-align: center;
			font-size: 24rpx;
			color: #333;
			padding: 0 30rpx;
		}
		.active {
			color: #FF780F;
			font-weight: 700;
		}
	}
	// .swiper-coupon-item {
	// 	box-sizing: border-box;
	// 	background-color: #ffffff;
	// 	padding: 30rpx {
	// 		top: 0;
	// 	};
	// 	.coupon-item {
	// 		margin-bottom: 20rpx;
	// 	}
	// 	.coupon-item {
	// 		padding: 30rpx;
	// 		background-color: #ffffff;
	// 		display: flex;
	// 		align-items: flex-end;
	// 		// margin-bottom: 2rpx;
	// 		> img {
	// 			width: 180rpx;
	// 			height: 160rpx;
	// 			border-radius: 10rpx;
	// 			margin-right: 30rpx;
	// 		}
	// 		.coupon-main {
	// 			.coupon-title {
	// 				font-size: 30rpx;
	// 				color: rgba(51,51,51,1);
	// 			}
	// 			.coupon-date {
	// 				display: flex;
	// 				align-items: center;
	// 				margin: 29rpx 0 20rpx;
	// 				> img {
	// 					width: 24rpx;
	// 					height: 24rpx;
	// 					margin-right: 15rpx;
	// 				}
	// 				> view {
	// 					font-size: 24rpx;
	// 					color: rgba(153,153,153,1);
	// 				}
	// 			}
	// 			.coupon-integral {
	// 				font-size: 30rpx;
	// 				font-weight: 500;
	// 				color: rgba(255,120,15,1);
	// 			}
	// 		}
	// 		.coupon-btn {
	// 			margin-left: auto;
	// 			width: 85rpx;
	// 			// height: 42rpx;
	// 			border: 2rpx solid rgba(255,120,15,1);
	// 			border-radius: 10rpx;
	// 			font-size: 26rpx;
	// 			color: rgba(255,120,15,1);
	// 			line-height: 42rpx;
	// 			text-align: center;
	// 		}
	// 	}
	// }
	.coupon-scroll {
		background-color: #ffffff;
		padding-top: 30rpx;
		.coupon-item {
			padding: 30rpx {
				top: 0;
			};
		}
	}
}
</style>