<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 :style="{'height':height}" :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view :style="{'height':height}" class="list-scroll-content" scroll-y @scrolltolower="loadData">
					<!-- 空白页 -->
					<view class="emptyBox" v-if="tabItem.loaded === true && tabItem.orderList.length === 0"><empty ></empty></view>

					<!-- 订单列表 -->
					<view class="box" v-for="(item, index) in tabItem.orderList" :key="index" >
						<view class="i-top b-b flex">
							<view class="time">订单编号：{{ item.order_id }}</view>
						</view>
						<view class="order-item flex">
							<view class="title-box">
								<view class="title" v-if="tabCurrentIndex == 0">
									<text>转给"{{ item.to_user }}"</text>
								</view>
								<view class="title" v-if="tabCurrentIndex == 1">
									<text>"{{ item.to_user }}"转入</text>
								</view>
								<view class="time">
									<text>{{ item._add_time }}</text>
								</view>
							</view>
							<view class="money">
								<text>{{ item.money }}</text>
							</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 uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import { zhuanList } from '@/api/wallet.js';
export default {
	components: {
		empty,
		uniLoadMore
	},
	data() {
		return {
			height:'',
			tabCurrentIndex: 0,
			navList: [
				{
					state: 0,
					text: '转出',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				},
				{
					state: 1,
					text: '转入',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				}
			]
		};
	},
	onLoad(options) {},
	onShow() {
		this.loadData();
	},
	onReady(res) {
		var obj = this;
		uni.getSystemInfo({
			success: resu => {
				const query = uni.createSelectorQuery();
				query.select('.swiper-box').boundingClientRect();
				query.exec(function(res) {
					console.log(res, 'ddddddddddddd');
					obj.height = resu.windowHeight - res[0].top + 'px';
					console.log('打印页面的剩余高度', obj.height);
				});
			},
			fail: res => {}
		});
	},
	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;
			}
			// 修改当前对象状态为加载中
			navItem.loadingType = 'loading';

			zhuanList({
				page: navItem.page,
				limit: navItem.limit,
				pm: navItem.state
			})
				.then(({ data }) => {
					console.log(data.data);
					if (data.data.length > 0) {
						navItem.orderList = navItem.orderList.concat(data.data);
						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('tabChange');
		},
		//顶部tab点击
		tabClick(index) {
			this.tabCurrentIndex = index;
		}
	}
};
</script>

<style lang="scss">
page,.center {
	background: #f8f8f8;
	height: 100%;
}
.emptyBox {
	width: 100%;
	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: #d7a465;
			&:after {
				content: '';
				position: absolute;
				left: 50%;
				bottom: 0;
				transform: translateX(-50%);
				width: 44px;
				height: 0;
				border-bottom: 2px solid #d7a465;
			}
		}
	}
}
.i-top {
	background-color: #ffffff;
	width: 100%;
	height: 80rpx;
	padding: 0 30rpx;
	font-size: $font-base;
	color: $font-color-dark;
	position: relative;
	margin-top: 20rpx;
}
.box {
	background-color: #fff;
	margin-top: 10rpx;
}
.swiper-box {
	padding-top: 10rpx;
	.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;
		}
	}
}

</style>


