<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-one" duration="0" @change="changeTab">

			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex"
				@click="goToOrderDetail(item)">
				<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
					<!-- 空白页 -->
					<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>

					<!-- 订单列表 -->
					<view class="order-item flex" v-for="(item, index) in tabItem.orderList" :key="index">
						<view class="title-box">
							<view class="title">
								<text>{{ item.mark||"文票闪兑" }}</text>
							</view>
							<view class="time">
								<text>{{ item.add_time }}</text>
							</view>
						</view>
						<view class="money">
							<view>+{{ item.extract_ticket }}</view>
							<view v-if="item.status == 0" class="status">待发放</view>
							<view v-if="item.status == 1" class="status">已发放</view>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import empty from '@/components/empty';
	import {
		getExchangeList
	} from '@/api/quick.js';
	export default {
		components: {
			empty
		},
		data() {
			return {
				loading: false, //判断是否在点击
				tabCurrentIndex: 0, //当前选中的第二级别我要买单选中项
				navList: [{
						state: 0,
						text: '待发放',
						loadingType: 'more',
						orderList: [],
						isnew: 1, //判断是否有新的订单
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 1,
						text: '已发放',
						loadingType: 'more',
						orderList: [],
						isnew: 1, //判断是否有新的订单
						page: 1, //当前页数
						limit: 10 //每次信息条数
					}
				],
			};
		},
		onLoad(options) {
			this.tabCurrentIndex = +options.state || 0;
		},
		onShow() {
			this.loadData();
		},
		// #ifdef APP-PLUS || H5
		onBackPress(e) {
			uni.switchTab({
				url: '/pages/user/user'
			});
			return true;
		},
		// #endif
		filters: {
			moneyNum(value) {
				return +value;
			}
		},
		// 页面加载完获取swiper高度
		onReady(res) {
			var obj = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.swiper-box-one').boundingClientRect();
					query.exec(function(res) {
						console.log(res, 'ddddddddddddd');
						obj.height = resu.windowHeight - res[0].top + 'px';
						console.log('打印页面的剩余高度', obj.height);
					});
				},
				fail: res => {}
			});
		},
		methods: {
			//顶部tab点击
			tabClick(index, type) {
				this.tabCurrentIndex = index;
			},
			//获取订单列表
			loadData(source) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				// if ( navItem.loaded === true) {
				// 	//tab切换只有第一次需要加载数据
				// 	return;
				// }
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (source == 'reload') {
					navItem.orderList = [];
					navItem.page = 1;
					navItem.limit = 10;
					navItem.loadingType = 'loading';
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';

				getExchangeList({
						status: state,
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						data
					}) => {
						let arr = data.list.map(e => {
							return e;
						});
						navItem.orderList = navItem.orderList.concat(arr);

						// console.log(navItem.orderList);
						navItem.page++;
						if (navItem.limit == data.length) {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						this.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						console.log(e);
					});

			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData();
			},
		}
	};
</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: $base-color;

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

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

		.tab-content {
			height: 100%;
		}

		.order-item {
			padding: 20rpx 30rpx;
			line-height: 1.5;

			.title-box {
				.title {
					font-size: $font-lg;
					color: $font-color-base;
				}

				.time {
					font-size: $font-base;
					color: $font-color-light;
				}
			}

			.money {
				color: #fd5b23;
				font-size: $font-lg;
				text-align: right;

				.status {
					color: $font-color-light;
				}
			}
		}
	}
</style>