<template>
	<view class="content">
		<view class="header">
			<image src="../../static/user/yongjin1.png" mode=""></image>
			<view class="integral">{{ userInfo.brokerage_price || '0.00' }}</view>
		</view>
		<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 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="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>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
		<!-- <view class="button">
			<navigator url="/pages/user/integralTransforms" class="b-left">积分转账</navigator>
			<navigator url="/pages/user/exchangeIntegral" class="b-right">转换购物积分</navigator>
		</view> -->
		<button type="default" class="button" @click="navto('/pages/money/withdraw')">立即提现</button>
	</view>
</template>

<script>
import { spreadCommission, userBalance } from '@/api/wallet.js';
import { mapState, mapMutations } from 'vuex';
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import empty from '@/components/empty';
export default {
	components: {
		empty,
		uniLoadMore
	},
	onReady() {

	},
	data() {
		return {
			tabCurrentIndex: 0,
			navList: [
				{
					state: 3,
					text: '收入',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				},
				{
					state: 4,
					text: '支出',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				},
			],
		};
	},
	onShow() {
		// 载入积分数据
		this.loadData();
	},
	computed: {
		//积分
		...mapState('user',['userInfo'])
	},
	methods: {
		// 页面跳转
		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[0].list);
						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) {
			console.log(e, '1111')
			this.tabCurrentIndex = e.target.current;
			this.loadData('tabChange');
		},
		//顶部tab点击
		tabClick(index) {
			this.tabCurrentIndex = index;
		}
	},
}
</script>

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

.header {
	image {
		width: 100%;
		height: 400rpx;
	}

	.integral {
		width: 100%;
		text-align: center;
		position: absolute;
		top: 160rpx;
		color: #fff;
		font-size: 80rpx;
		font-weight: bold;
	}

	.right {
		position: absolute;
		top: 120rpx;
		right: 0;
		background-color: #fff;
		border-radius: 10rpx 0 0 10rpx;
		color: #438bed;
		padding: 5rpx 15rpx;
		font-size: 28rpx;
	}
}

.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: #438bed;

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

// 列表

.swiper-box {
	height: calc(100% - 180rpx - 400rpx);
	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: #ef3a55;
			font-size: $font-lg;
		}
	}
}

.list-scroll-content {
	height: 100%;
}

.content {
	height: 100%;

	.empty-content {
		height: 100%;
		background-color: #ffffff;
	}
}

.button {
	position: relative;
	bottom: 0;
	bottom: 50rpx;
	width: 674rpx;
	height: 88rpx;
	background: linear-gradient(90deg, #3C82E6, #5395F5);
	border-radius: 44rpx;
	font-size: 36rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #ffffff;
}
</style>