<template>
	<view class="content">
		<view class="top-bg"></view>
		<view class="yue-wrap flex">
			<view class="yue-tit flex">
				<view class="yue-left">总资产(CNY)</view>
				<view class="yue-left">
					<view class="cz" @click="open()">
						发红包
						<image src="../../static/icon/dz.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="yue-num flex">
				<view class="yue">{{ userInfo.now_money || '0.00' }}</view>
				<view class="">
					<view class="cz" @click="navto('/pages/money/recharge')">
						充值
						<image src="../../static/icon/dz.png" mode="widthFix"></image>
					</view>
					<view class="tx" @click="navto('/pages/money/withdrawal')">
						提现
						<image src="../../static/icon/dz.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="lj-wrap flex">
				<view class="">累计消费：￥{{ userInfo.orderStatusSum || '0' }}</view>
				<view class="">累计充值：￥{{ userInfo.recharge || '0' }}</view>
			</view>
		</view>
		<scroll-view scroll-y="true" class="jl-wrap" :style="{ height: maxheight }" @scrolltolower="loadData">
			<view>
				<empty v-if="navList[tabCurrentIndex].orderList.length == 0 && navList[tabCurrentIndex].loaded"></empty>
				<view class="jl " v-for="item in navList[tabCurrentIndex].orderList">
					<view class="jl-tit flex">
						<view class="clamp2 tit">{{ item.title }}</view>
						<view class="price" :class="{ add: item.pm == 1, jian: item.pm == 0 }">{{ item.number * 1 }}</view>
					</view>
					<view class="jl-mark flex">
						<!-- <view class="mark">
							{{item.mark}}
						</view> -->
						<view class="" v-if="item.status == 1">{{ item.add_time }}</view>
						<view class="" v-if="item.status == 0">等待下级确认收货</view>
					</view>
				</view>
				<uni-load-more :status="navList[tabCurrentIndex].loadingType"></uni-load-more>
			</view>
		</scroll-view>
		<uni-popup ref="popuphx" class="agree-wrapper">
			<view class="hx-wrapper">
				<view class="hx-img"><image src="../../static/img/hxbg.png" mode=""></image></view>
				<view class="hx-body">
					<view class="hx-title">输入红包金额</view>
					<input type="text" v-model="num" placeholder="请输入红包金额" placeholder-class="hx-placeholder" />
					<view class="hx-btn" @click="qhx">立即发送</view>
				</view>
				<view class="hx-close" @click="close"><image src="../../static/icon/close.png" mode=""></image></view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
import { spreadCommission, userBalance } from '@/api/wallet.js';
import { getMoneyStyle } from '@/utils/rocessor.js';
import { getUserInfo, getUser, redbag } from '@/api/user.js';
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import empty from '@/components/empty';
export default {
	filters: {
		getMoneyStyle
	},
	components: {
		empty,
		uniLoadMore
	},
	onReady() {
		// 初始化获取页面宽度
		var obj = this;
		uni.getSystemInfo({
			success: resu => {
				const query = uni.createSelectorQuery();
				query.select('.jl-wrap').boundingClientRect();
				query.exec(function(res) {
					obj.maxheight = resu.windowHeight - res[0].top + 'px';
				});
			},
			fail: res => {}
		});
	},
	data() {
		return {
			// 头部图高度
			maxheight: '',
			tabCurrentIndex: 0,
			num: '',
			navList: [
				{
					state: 0,
					text: '收入',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10, //每次信息条数
					loaded: false
				},
				{
					state: 2,
					text: '收入',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10, //每次信息条数
					loaded: false
				},
				{
					state: 1,
					text: '支出',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10, //每次信息条数
					loaded: false
				}
			],
			money: '',
			userInfo: {}
		};
	},
	onLoad(options) {},
	onShow() {
		this.loadData();
		this.getUserInfo();
	},
	methods: {
		...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
		getUserInfo() {
			getUser({}).then(({ data }) => {
				this.userInfo = data;
			});
		},
		// 页面跳转
		navto(e) {
			uni.navigateTo({
				url: e
			});
		},
		open() {
			this.$refs.popuphx.open();
		},
		close() {
			this.$refs.popuphx.close();
		},
		qhx() {
			const obj = this;
			redbag({ num: this.num }).then(e => {
				console.log(e);
				uni.navigateTo({
					url:
						'/pages/user/shareEx?shareid=' +
						e.data.uni +
						'&scene=' +
						obj.userInfo.uid +
						'&num=' +
						obj.num +
						'&logo=' +
						obj.userInfo.avatar +
						'&nickname=' +
						obj.userInfo.nickname +
						'&type=1'
				});
			});
		},
		//获取收入支出信息
		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 }) => {
					console.log(data, '获取数据');
					// if (data.count > 0) {
					// 	navItem.orderList = navItem.orderList.concat(data.list);
					// 	console.log(navItem.orderList);
					// 	navItem.page++;
					// }
					if (data.length > 0) {
						data.forEach(item => {
							console.log(item.list, '内部列表');
							navItem.orderList = navItem.orderList.concat(item.list);
						});
						// 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) {
			this.tabCurrentIndex = e.target.current;
			this.loadData('tabChange');
		},
		//顶部tab点击
		tabClick(index) {
			this.tabCurrentIndex = index;
		},
		addmoney() {
			uni.navigateTo({
				url: '/pages/money/recharge'
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #fff;
	height: auto;
	min-height: 100%;
}

.top-bg {
	background-color: #ee2f72;
	height: 180rpx;
	width: 750rpx;
}

.yue-wrap {
	width: 670rpx;
	height: 320rpx;
	margin: -160rpx auto 30rpx;
	background: linear-gradient(-70deg, #ff77a7, #ff4a8a);
	box-shadow: 0px 15rpx 22rpx 6rpx rgba(238, 47, 114, 0.1);
	border-radius: 25rpx;
	color: #fff;
	font-size: 26rpx;
	padding: 50rpx 75rpx;
	flex-direction: column;
	justify-content: space-between;
	align-items: flex-start;
	.yue-tit {
		width: 100%;
	}
	.cz {
		image {
			width: 13rpx;
			margin-left: 10rpx;
		}
	}
	.tx {
		image {
			width: 13rpx;
			margin-left: 10rpx;
		}
	}
	.yue-num {
		width: 100%;
		justify-content: space-between;

		.yue {
			font-size: 76rpx;
			font-weight: bold;
		}

		.cz {
			image {
				width: 13rpx;
				margin-left: 10rpx;
			}
		}
	}

	.lj-wrap {
		width: 100%;
		justify-content: space-between;
		font-size: 24rpx;
	}
}

.jl {
	width: 670rpx;
	margin: 0 auto 20rpx;
	padding: 35rpx 30rpx;
	background: #ffffff;
	box-shadow: 0px 0px 28px 0px rgba(48, 48, 48, 0.1);
	border-radius: 8px;

	.jl-tit {
		width: 100%;
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;

		.tit {
			max-width: 450rpx;
		}

		.price {
			font-size: 38rpx;
			font-weight: bold;
		}

		.add {
			color: #ee2f72;

			&::before {
				content: '+';
				font-size: 28rpx;
				color: #ee2f72;
			}
		}

		.jian {
			color: #808080;

			&::before {
				content: '-';
				font-size: 28rpx;
			}
		}
	}

	.jl-mark {
		padding-top: 20rpx;
		width: 100%;
		font-size: 24rpx;
		font-weight: 500;
		color: #808080;
		align-items: flex-start;

		// justify-content: flex-end;
		.mark {
			max-width: 470rpx;
		}
	}
}
.hx-wrapper {
	width: 536rpx;
	height: 630rpx;
	position: relative;

	// background-color: #fff;
	.hx-img {
		width: 536rpx;
		height: 281rpx;

		image {
			width: 536rpx;
			height: 281rpx;
		}
	}

	.hx-close {
		position: absolute;
		left: 243rpx;
		bottom: -80rpx;
		width: 52rpx;
		height: 52rpx;

		image {
			width: 52rpx;
			height: 52rpx;
		}
	}

	.hx-body {
		width: 536rpx;
		height: 349rpx;
		background-color: #fff;
		border-radius: 0 0 10rpx 10rpx;

		.hx-title {
			width: 536rpx;
			font-size: 36rpx;
			font-weight: 500;
			color: #333333;
			line-height: 1;
			padding-top: 42rpx;
			text-align: center;
		}

		input {
			width: 439rpx;
			height: 68rpx;
			background: #eeddde;
			border-radius: 10rpx;
			margin: 39rpx auto 0;
			padding-left: 26rpx;

			.hx-placeholder {
				font-size: 26rpx;
				font-weight: 500;
				color: #ee2f72;
			}
		}

		.hx-btn {
			margin: 44rpx auto 0;
			width: 353rpx;
			height: 71rpx;
			background: #ee2f72;
			border-radius: 34rpx;
			font-size: 36rpx;
			font-weight: 500;
			color: #f8f9f9;
			line-height: 71rpx;
			text-align: center;
		}
	}
}
</style>
