<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> -->
		<view class="header">
			<image src="../../static/img/i-bg.png" mode=""></image>
			<view class="integral">
				{{ money || '0.00' }}
			</view>
			<navigator url="/pages/money/conversionIntegral"><view class="right">转换USDT</view></navigator>
		</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.mark }}</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>
	</view>
</template>

<script>
import { moneyLog, recharge, wallet } from '@/api/finance.js';
import { integrallist } from '@/api/functionalUnit.js';
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: 1,
					text: '收入',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				},
				{
					state: 0,
					text: '支出',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				},
			],
			money:'',
		};
	},
	onShow() {
		wallet({}).then(({ data }) => {
			const obj = this
			const arr = Object.keys(data.back);
			console.log(arr);
			arr.forEach(e => {
				if(e == 'SCJF'){
					obj.money = (data.back[e].money.money*1)
				}
			});
		});
		// 载入积分数据
		this.loadData();
	},
	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';

			moneyLog(
				{
					page: navItem.page,
					limit: navItem.limit,
					pm:navItem.state
				},
				'SCJF'
			)
				.then(({ data }) => {
					if (data.list.length > 0) {
						navItem.orderList = navItem.orderList.concat(data.list);
						navItem.page++;
					}
					if (navItem.limit == data.list.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;
		}
	},
	computed: {
		//积分
		integral() {
			return this.$store.state.user.userInfo.integral
		}
	},
};
</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 {
		background-color: #ffffff;
	}
}
.button {
	height: 180rpx;
	width: 100%;
	display: flex;
	position: absolute;
	padding: 40rpx 30rpx 60rpx 30rpx;
	bottom: 0;
	text-align: center;
	.b-left,.b-right {
		flex: 1;
		height: 80rpx;
		line-height: 80rpx;
		margin: 0 30rpx;
		border-radius: 50rpx;
	}
	.b-left {
		background: #FFFFFF;
		color: #438BED;
		border: solid 1rpx #438BED;
	}
	.b-right {
		background: linear-gradient(90deg, #438BED 0%,  #44BFEC 100%);
		color: #fff;
	}
}
</style>
