<template>
	<view class="content">
		<view class="navbar">
			<view v-for="(item,index) in navList" :key="index" class="nav-item"
				:class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}</view>
		</view>
		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view scroll-y="true" :style="{'height': height}">
					<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>
					<view  class="kp-item flex" :class="{'sy':tabCurrentIndex != 0}" v-for="item in tabItem.orderList"
						:key="item.id" @click="navTo('/user/page/coupon?id='+item.id)">
						<image src="https://hy.liuniu946.com/app/img/my-kq-1.png" mode="" class="kp-bg" v-if="item._type == 0">
						</image>
						<image src="https://hy.liuniu946.com/app/img/my-kq.png" mode="" class="kp-bg" v-if="item._type !=0">
						</image>
						<view class="kp-left flex">
							<view class="left-val">
								{{item.coupon_price}}
							</view>
						</view>
						<view class="kp-right flex">
							<view class="">
								<view class="right-tit clamp2">
									{{item.coupon_title}}
								</view>
								<view class="right-tip" v-if="item.use_min_price!='0.00'">
									使用条件:满{{item.use_min_price}}￥可用
								</view>
								<view class="right-tip" v-else>
									使用条件:无门槛
								</view>
								<view class="right-tip" v-if="!item.store">
									购物下单时自动抵扣
								</view>
								<view class="right-tip"  v-else>
									使用商户:{{item.store.name}}
								</view>
							</view>
							<view class="right-time">
								有效期至：{{item._end_time}}
							</view>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
				<!-- 空白页 -->

			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from '@/components/empty';
	import {
		getCouponsList
	} from '@/api/order.js'
	
	export default {
		components: {
			uniLoadMore,
			empty
		},
		data() {
			return {
				height: '',
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '全部',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
						loaded: false
					},
					{
						state: 1,
						text: '未使用',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
						loaded: false
					},
					{
						state: 2,
						text: '已使用',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10, //每次信息条数
						loaded: false
					}
				]
			}
		},
		onLoad() {
			this.getMyCoupons()
		},
		onShow() {

		},
		onReachBottom() {

		},
		onReady() {
			var obj = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.swiper-box').boundingClientRect();
					query.exec(function(res) {
						obj.height = resu.windowHeight - res[0].top + 'px';
					});
				},
				fail: res => {}
			});
		},
		methods: {
			navTo(url) {
				console.log(url);
				let pages = getCurrentPages();
				console.log(pages, 'pages');
			
				let prevPage = pages[pages.length - 2]
				console.log(prevPage, 'pages2');
			
				// if (!this.hasLogin) {
				// 	// 保存地址
				// 	saveUrl();
				// 	// 登录拦截
				// 	interceptor();
				// } else {
				uni.navigateTo({
					url
				});
				// }
			},
			getMyCoupons(type) {
				let obj = this
				let index = obj.tabCurrentIndex
				let navitem = obj.navList[index]
				if(type == 'tab' && navitem.loaded) {
					return
				}
				if (navitem.loadingType == 'noMore' || navitem.loadingType == 'loading') {
					return
				}
				navitem.loadingType = 'loading'
				getCouponsList({
					pages: navitem.page,
					limit: navitem.limit
				}, navitem.state).then(({data}) => {
					let da = data.map(e => {
						e.coupon_price = +(e.coupon_price.replace(',', ''));
						e.use_min_price = +e.use_min_price.replace(',', '');
						return e
					});
					navitem.orderList = navitem.orderList.concat(da);
					console.log(navitem.orderList);
					navitem.page++;
					if (navitem.limit == data.length) {
						// 判断是否还有数据，有改为more，没有改为noMore
						navitem.loadingType = 'more';
						return;
					}else {
						navitem.loadingType = 'noMore';
					}
					uni.hideLoading();
					this.$set(navitem, 'loaded', true);
				})
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
				this.getMyCoupons()
			},
			//swiper 切换
			changeTab(e) {
				console.log(e, 'eeeeeeeeeeeeeeeeee')
				if (e.target.current > this.navList.length - 1) {
					this.tabCurrentIndex = 0
				} else {
					this.tabCurrentIndex = e.target.current;
				}
			},
		}
	}
</script>

<style lang="scss">
	page,
	.content {
		background: $page-color-base;
		height: 100%;
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;

			&.current {
				color: #FF4C4C;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid #FF4C4C;
				}
			}
		}
	}

	.swiper-box {
		height: calc(100% - 40px);

		.tab-content {
			// padding: 25rpx 0px;
			font-size: 28rpx;
			color: #1b1b1b;
		}
	}

	.kp-item {
		width: 690rpx;
		height: 240rpx;
		margin: 0 auto 20rpx;
		position: relative;

		.kp-bg {
			width: 690rpx;
			height: 240rpx;
			position: absolute;
			top: 0;
			left: 0;
		}

		.kp-left {
			width: 221rpx;
			height: 240rpx;
			justify-content: center;
			align-items: center;
			position: relative;
			flex-shrink: 0;

			.left-val {
				font-size: 90rpx;
				font-weight: 400;
				color: #FFFFFF;
				text-shadow: 0px 10px 5px rgba(255, 125, 69, 0.3);

				&::before {
					content: '￥';
					font-size: 40rpx;
					font-weight: 400;
					color: #FFFFFF;
				}
			}
		}

		.kp-right {
			// width: 648rpx;
			flex-grow: 1;
			height: 240rpx;
			position: relative;
			// background: #000;
			flex-direction: column;
			justify-content: space-between;
			padding: 40rpx;
			align-items: flex-end;
			text-align: right;

			.right-tit {
				font-size: 40rpx;
				font-weight: 400;
				color: #FFFFFF;
			}

			.right-tip {
				font-size: 24rpx;
				font-weight: 400;
				color: #FFFFFF;
			}

			.right-time {
				font-size: 20rpx;
				font-weight: bold;
				color: #FFFFFF;
			}
		}
	}

	.sy {
		.kp-left {
			.left-val {
				text-shadow: 0px 10px 5px rgba(173, 173, 173, 0.3);
			}
		}

	}
</style>
