<template>
	<view class="content">
		<view class="content-money">
			<view class="money-box">
				<view class="bg"><image src="../../static/img/consumer.png" mode=""></image></view>
				<view class="body-title">
					<view class="goback-box" @click="toBack"><image class="goback" src="../../static/icon/fanhui.png" mode=""></image></view>
					<view class="header">我的消费券</view>
				</view>
				<view>
					<view class="money">{{ money | getMoneyStyle }}</view>
				</view>
			</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" :style="{ height: maxheight + 'px' }" 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>
</template>

<script>
import { spreadCommission, userBalance, rechargemoneys } from '@/api/wallet.js';
// import { mapState } from 'vuex';
import { getMoneyStyle } from '@/utils/rocessor.js';
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import empty from '@/components/empty';
// #ifdef H5
import { weixindata } from '@/utils/wxAuthorized';
// #endif
export default {
	filters: {
		getMoneyStyle
	},

	components: {
		empty,
		uniLoadMore
	},
	// computed: {
	// 	...mapState(['weichatObj'])
	// },
	onReady() {
		let obj = this;
		// 初始化获取页面高度
		uni.createSelectorQuery()
			.select('.content')
			.fields(
				{
					size: true
				},
				data => {
					// 初始化获取头部高度
					uni.createSelectorQuery()
						.select('.content-money')
						.fields(
							{
								size: true
							},
							es => {
								// 保存头部高度
								obj.maxheight = data.height - es.height - 40;
							}
						)
						.exec();
				}
			)
			.exec();
	},
	data() {
		return {
			// 头部图高度
			maxheight: '',
			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) {
		let bool = uni.getStorageSync('weichatBrowser');
		if (bool) {
			// 加载微信信息
			weixindata();
		}
	},
	onShow() {
		this.loadData();
		// 获取用户余额
		userBalance({}).then(({ data }) => {
			this.money = data.now_money;
		});
	},
	methods: {
		// ToChangeInto() {
		// 	// #ifdef H5
		// 	this.weichatObj.scanQRCode({
		// 		needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
		// 		scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
		// 		success: function(res) {
		// 			let result = JSON.parse(res.resultStr);

		// 			rechargemoneys({
		// 				id: result.id,
		// 				money: result.money
		// 			})
		// 				.then(({ data }) => {
		// 					uni.showModal({
		// 						title: '标题',
		// 						content: '成功',
		// 						showCancel: false
		// 					});
		// 				})
		// 				.catch(e => {
		// 					console.log(e);
		// 				});
		// 		}
		// 	});
		// 	// #endif
		// },
		// 页面跳转
		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);
				});
		},
		//返回上一页面
		toBack() {
			console.log(111);
			uni.navigateBack({});
		},
		//swiper 切换
		changeTab(e) {
			this.tabCurrentIndex = e.target.current;
			this.loadData('tabChange');
		},
		//顶部tab点击
		tabClick(index) {
			this.tabCurrentIndex = index;
		}
	}
};
</script>

<style lang="scss">
page {
	background: #ffffff;
	height: 100%;
	padding: 0;
}
.content-money {
	padding-bottom: 30rpx;
	background: $page-color-base;
}
.money-box {
	background-color: $base-color;
	padding-top: var(--status-bar-height);
	height: 468rpx;
	color: #ffffff;
	text-align: center;
	justify-content: center;
	position: relative;
	.body-title {
		height: 80rpx;
		text-align: center;
		font-size: 35rpx;
		position: relative;
		.header {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #fffeff;
			height: 80rpx;
			font-size: 32rpx;
			font-weight: 700;
			z-index: 9;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.goback-box {
			position: absolute;
			z-index: 10;
			left: 24rpx;
			top: 0;
			height: 80rpx;
			display: flex;
			align-items: center;
		}
		.goback {
			width: 30rpx;
			height: 36rpx;
		}
	}
	.bg {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		image {
			width: 100%;
			height: 100%;
		}
	}
	.money {
		position: relative;
		z-index: 10;
		font-size: 56rpx;
		height: 388rpx;
		line-height: 288rpx;
	}
}

.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: $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 {
	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;
		}
	}
}
.list-scroll-content {
	height: 100%;
}
.content {
	height: 100%;
	.empty-content {
		background-color: #ffffff;
	}
}
</style>
