<template>
	<view class="container">
		<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 class="list-scroll-content" scroll-y @scrolltolower="loadData">
					<!-- 空白页 -->
					<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>
					<!-- 订单列表 -->
					<view v-for="(item, index) in tabItem.orderList" :key="index" class="commission flex">
						<view>
							<view class="commission_name">{{ item.title }}</view>
							<view class="commission_time">{{ item.add_time }}</view>
						</view>
						<view class="commission_number">+{{ item.number }}</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
import { spread_commission } from '@/api/wallet.js';
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import empty from '@/components/empty';
export default {
	components: {
		empty,
		uniLoadMore
	},
	data() {
		return {
			id: '',
			tabCurrentIndex: 0,
			navList: [
				{
					state: 0,
					text: '一级',
					loadingType: 'more',
					orderList: [
						{
							title:'充值',
							add_time:'2019-06-09 13:00:00',
							number:65
						},
						{
							title:'充值',
							add_time:'2019-06-09 13:00:00',
							number:65
						},
						{
							title:'充值',
							add_time:'2019-06-09 13:00:00',
							number:65
						}
					],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				}
				// {
				// 	state: 1,
				// 	text: '二级',
				// 	loadingType: 'more',
				// 	orderList: [],
				// 	page: 1, //当前页数
				// 	limit: 10 //每次信息条数
				// }
			]
		};
	},
	methods: {
		async loadData(source) {
			//这里是将订单挂载到tab列表下
			let index = this.tabCurrentIndex;
			let navItem = this.navList[index];
			let state = navItem.state;
			if (source === 'tabChange' && navItem.loaded === true) {
				//tab切换只有第一次需要加载数据
				return;
			}
			if (navItem.loadingType === 'loading') {
				//防止重复加载
				return;
			}
			if (navItem.loadingType === 'noMore') {
				//防止重复加载
				return;
			}
			// 修改当前对象状态为加载中
			navItem.loadingType = 'loading';
			spread_commission(
				{
					page: navItem.page,
					limit: navItem.limit
				},
				2
			)
				.then(({ data }) => {
					navItem.orderList = navItem.orderList.concat(data);
					navItem.page++;
					if (navItem.limit == data.length) {
						//判断是否还有数据， 有改为 more， 没有改为noMore
						navItem.loadingType = 'more';
						return;
					} else {
						//判断是否还有数据， 有改为 more， 没有改为noMore
						navItem.loadingType = 'noMore';
					}
					this.$set(navItem, 'loaded', true);
				})
				.catch(e => {
					console.log(e);
				});
		}
	},
	async onLoad() {
		this.loadData();
	}
};
</script>

<style lang="scss">
page {
	width: 100%;
	height: 100%;
	.container {
		height: 100%;
	}
	.swiper-box {
		height: 100%;
	}
}
.uni-swiper-item {
	height: auto;
}
.list-scroll-content {
	height: 100%;
}
.commission {
	background-color: #ffffff;
	border-top: 1px solid #f0f0f0;
	padding: 25rpx 25rpx;
}
.commission_name {
	font-size: 30rpx;
	color: #333333;
}
.commission_time {
	color: #999999;
	font-size: 22rpx;
	padding-top: 10rpx;
}
.commission_number {
	color: #FC4141;
	font-weight: bold;
	font-size: 30rpx;
}
</style>
