<template>
	<view class="content">
		<uni-nav-bar class="nav" @clickLeft='back' :border='false' backgroundColor='transparent' leftIcon='left'
			color='#FFF' title="我的余额" />
		<view class="content-money">
			<view class="conetnt-box">
				<view class="money-box">
					<view class="money"><text class="tip">￥</text>{{ money | getMoneyStyle }}</view>
					<view class="text">当前余额</view>
				</view>
			</view>
		</view>
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item"
				:class="{ current: tabCurrentIndex === index }" @click="tabCurrentIndex=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 class="list-scroll-content" scroll-y @scrolltolower="loadData">
					<!-- 空白页 -->
					<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>
					
					<!-- 订单列表 -->
					<view class="itemList" v-for="(items, indexs) in tabItem.orderList" :key="indexs">
						<!-- <view class='font-size-lg padding-l-30 bg-gray padding-v-10'>
							{{items.time}}
						</view> -->
						<view v-for="(item, index) in items.list" :key="index" class="order-item flex">
							<view class="title-box">
								<view class="title">
									<text>{{ item.title }}</text>
								</view>
								<view class="time">
									<text>{{ item.add_time }}</text>
								</view>
							</view>
							<view class="money">
								<text>{{ (item.pm == 0 ? '-' : '+') + item.number }}</text>
							</view>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>

		<view class="base-buttom" @click="navto('./recharge')">立即充值</view>
	</view>
</template>

<script>
	import {
		spreadCommission,
		userBalance
	} from '@/api/wallet.js';
	import {
		getMoneyStyle
	} from '@/utils/rocessor.js';
	export default {
		filters: {
			getMoneyStyle
		},
		data() {
			return {
				// 头部图高度
				tabCurrentIndex: 0, //当前选中的
				navList: [{
						state: 1,
						text: '支出',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 2,
						text: '收入',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					}
				],
				money: ''
			};
		},
		onLoad(options) {
			this.loadData();
			console.log('进入');
		},
		onShow() {
			// 获取用户余额
			userBalance({}).then(({
				data
			}) => {
				this.money = data.now_money;
			});
		},
		// 页面到底部后加载数据
		onReachBottom() {
			this.loadData();
		},
		methods: {
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			// 返回退回
			back() {
				uni.reLaunch({
					url: '/pages/home/user'
				})
			},
			// 页面跳转
			navto(e) {
				uni.navigateTo({
					url: e
				});
			},
			//获取收入支出信息
			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';

				spreadCommission({
							page: navItem.page,
							limit: navItem.limit
						},
						state
					)
					.then(({
						data
					}) => {
						if (data.length > 0) {
							navItem.orderList = navItem.orderList.concat(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);
					});
			}
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.content-money {
		position: relative;
		padding: 20rpx;
		padding-top: var(--status-bar-height);
		background: url('../../../static/image/tg-bg.png') no-repeat;
		height: 200px;

		.money-box {
			padding-top: 80px;
			text-align: center;
			color: #FFF;

			.text {
				font-size: $font-base;
			}

			.money {
				font-weight: bold;
				font-size: 82rpx;

				.tip {
					font-size: $font-lg;
				}
			}
		}
	}



	.content {
		height: 100%;

		.nav {
			position: fixed;
			top: 0;
			right: 0;
			left: 0;
			z-index: 998;
		}
	}

	.buttom-box {
		text-align: center;
		margin: 0 30rpx;
		margin-top: 80rpx;

		.buttom {
			flex-grow: 1;
			border-radius: 100rpx;
			font-size: $font-lg;
			width: 300rpx;
			height: 80rpx;
			line-height: 80rpx;
			background-color: $color-green;
			color: #FFFFFF;

			&.geMoney {
				border: 1px solid $font-color-disabled;
				color: $font-color-disabled;
				background-color: #FFFFFF;
			}
		}
	}

	.base-buttom {
		position: fixed;
		bottom: 30rpx;
		left: 0;
		right: 0;
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		margin-top: 10px;
		background: #fff;
		position: relative;
		border-bottom: 1px solid $border-color-light;
		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 {
		height: calc(100% - 250px);
		background-color: #FFF;
		.list-scroll-content{
			height: 100%;
			padding-bottom: 150rpx;
		}
		.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>
