<template>
	<view class="content">
		<view class="bg-img">
			<image src="https://zhibo.liuniu946.com/img/anchor11.png" mode=""></image>
		</view>
		<view class="awardBox">
			<view class="header">
				我的佣金
				<view class="toback" @click="toIndex">
					<image src="../../static/img/left4.png" mode=""></image>
				</view>
			</view>
			<!-- <view class="withdraw">
				<view class="withdraw-btn" @click="navto('/pages/wallet/record')">
					提现记录
				</view>
				
			</view> -->
			<view class="balance">
				<text class="spe">￥</text>
				<!-- {{money}} -->
				{{count}}
			</view>
		</view>
		<view class="freeze-box">
			<view class="freeze">
				<view class="freeze-item" @click="navto('/pages/wallet/myWalletDongjie')">
					<view class="price">{{ lockCommissionCount  || 0}}</view>
					<view class="title">冻结中(元)</view>
				</view>
				<view class="line"></view>
				<view class="freeze-item" @click="navto('/pages/wallet/myWalletJ')">
					<view class="price">{{ commissionCount || 0}}</view>
					<view class="title">已解冻(元)</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" class="swiper-box" duration="300" @change="changeTab"
			:style="{ height: maxheight + 'px' }">
			<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="(ls, index) in tabItem.orderList">
						<view class="order-item flex" v-if="tabCurrentIndex == 0">
							<view class="title-box">
								<view class="title">
									<text>{{ ls.title }}</text>
								</view>
								<view class="time">
									<text>{{ ls.add_time }}</text>
								</view>
							</view>
							<view class="money">
								<text>{{ '+' + ls.number }}</text>
							</view>
						</view>

						<view class="order-item flex" v-if="tabCurrentIndex == 1">
							<view class="title-box">
								<view class="title">
									<text>{{ ls.title }}</text>
								</view>
								<view class="time">
									<text>{{ ls.time }}</text>
								</view>
							</view>
							<view class="money">
								<text>{{ '-' + ls.number }}</text>
							</view>
						</view>
					</view>


					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
					<view class="immAdd" @click="toAccount">
						立即提现
					</view>


				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	// import { getCommissionInfo, userinfos } from '@/api/user.js';
	import {
		spreadCount,
		getCommissionInfo,
		commission
	} from '@/api/user.js';
	import {
		userinfos
	} from '@/api/user.js';
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from '@/components/empty';
	export default {
		components: {
			empty,
			uniLoadMore
		},
		onReady() {
			// 初始化获取页面宽度
			uni.createSelectorQuery()
				.select('.content')
				.fields({
						size: true
					},
					data => {
						// 保存头部高度
						// this.maxheight = data.height - Math.floor((data.width / 750) * 570);
						this.maxheight = data.height - Math.floor((data.width / 750) * 330) - 40 - 40;
					}
				)
				.exec();
		},
		data() {
			return {
				// 头部图高度
				maxheight: '',
				tabCurrentIndex: 0,
				count: 0, //佣金总金额
				money: '', // 现用佣金
				navList: [{
						state: 3,
						text: '返佣',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 4,
						text: '支出',
						loadingType: 'more',
						orderList: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
				],
				money: '', // 总余额
				recharge: '', // 累计充值
				orderStatusSum: '', // 累计消费
				lockCommissionCount: '', //冻结金额
				commissionCount: '', //解冻金额
			};
		},
		onLoad(options) {},
		onShow() {
			this.loadData();
		},
		methods: {
			// 页面跳转
			toIndex() {
				uni.switchTab({
					url: '/pages/user/user'
				})
			},
			navto(e) {
				console.log('=======', e)
				uni.navigateTo({
					url: e
				});
			},
			//获取佣金总金额
			async loadData(source = 'add') {
				console.log(source)
				let type = 3;
				spreadCount({}, type).then(({
					data
				}) => {
					console.log(data, 'data')
					this.count = data.count;
				});
				// 	let that = this;
				// 	userinfos().then(({data}) => {
				// 		console.log(data,'res')
				// 		this.money = data.extractPrice;
				// 		this.extractTotalPrice = data.extractTotalPrice;
				// 	});
				//这里是将订单挂载到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;
				}
				// 判断是否重新加载数据
				if (source == 'refresh') {
					navItem.orderList = [];
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';
				commission({
							page: navItem.page,
							limit: navItem.limit
						},
						state)
					.then(({
						data
					}) => {
						console.log('获取佣金明细', data);
						this.lockCommissionCount = data.lockCommissionCount;
						this.commissionCount = data.commissionCount;
						getCommissionInfo({
								page: navItem.page,
								limit: navItem.page
							}, state)
							.then(({
								data
							}) => {
								if (data.length > 0) {
									navItem.orderList = navItem.orderList.concat(data[0].list);
									console.log(navItem.orderList, "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);
					})
			},
			//获取信息

			// 提现
			toAccount() {
				if (this.count <= 0) {
					uni.showToast({
						title: '佣金不足',
						duration: 2000,
						icon: 'none',
					});
				} else {
					uni.navigateTo({
						url: '/pages/wallet/account'
					});
				}
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			}
		}
	};
</script>

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

	.content-money {
		padding-bottom: 30rpx;
		background: $page-color-base;

		// border: 2px solid #ffffff;
		.moneyTx {
			position: absolute;
			top: 150rpx;
			right: 0rpx;
			width: 150rpx;
			padding: 10rpx 30rpx;
			border: 2px solid #ffffff;
			border-top-left-radius: 99rpx;
			border-bottom-left-radius: 99rpx;
			color: #ffffff;
			line-height: 1;
			font-size: $font-base;
		}

		.buttom-box {
			background-color: #ffffff;
			text-align: center;
			margin: 0 30rpx;
			padding: 20rpx 0;
			border-radius: $border-radius-sm;
			margin-top: -60rpx;

			.buttom {
				font-size: $font-lg;
				flex-grow: 1;
			}

			.interval {
				width: 2px;
				height: 60rpx;
				background-color: #eeeeee;
			}

			.icon {
				height: 50rpx;
				width: 48rpx;
				margin: 0 auto;

				.icon-img {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.money-box {
		background: $base-color;
		padding-top: var(--status-bar-height);
		height: 410rpx;
		color: #ffffff;
		text-align: center;
		position: relative;

		.text {
			// padding-top: 80rpx;
			font-size: $font-lg;
		}

		.money {
			padding-top: 120rpx;
			font-size: 45rpx;
			font-weight: 500;
		}

		.money-accum {
			display: flex;
			align-items: center;
			justify-content: space-around;
			margin-top: 44rpx;

			.money-frame {
				.money-num {
					font-size: 45rpx;
					font-weight: 500;
					color: rgba(255, 255, 255, 1);
				}

				.mon-text {
					font-size: 30rpx;
					font-weight: 500;
					color: rgba(255, 255, 255, 1);
				}
			}
		}

		.money-loca {
			width: 140rpx;
			height: 50rpx;
			background: #FFFFFF;
			color: #FA2740;
			font-size: $font-base;
			line-height: 50rpx;
			border-radius: 10rpx 0 0 10rpx;
		}

		// 立即提现
		.draw {
			position: absolute;
			right: 0;
			top: 64rpx;
		}

		// 余额支付
		.rech {
			position: absolute;
			right: 0;
			top: 134rpx;
		}
	}

	.immAdd {
		margin: 0 auto;
		width: 674rpx;
		height: 88rpx;
		background: linear-gradient(180deg, #FD4646, #FF3535);
		border-radius: 44rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #FFFFFF;
		font-size: 36rpx;
		// margin-top: 63rpx;
		// margin-bottom: 133rpx;
		position: fixed;
		left: 38rpx;
		bottom: 50rpx;
	}

	.bg-img {
		width: 100%;
		height: 400rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.awardBox {
		width: 100%;
		height: 400rpx;
		// background-color: pink;
		position: absolute;
		left: 0;
		top: 0;

		.header {
			margin-top: var(--status-bar-height);
			// background-color: pink;
			width: 100%;
			height: 68rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 26rpx;
			// font-family: PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			position: relative;

			.toback {
				// background-color: red;
				display: flex;
				align-items: center;
				width: 44rpx;
				height: 68rpx;
				margin-left: 16rpx;
				position: absolute;
				left: 0rpx;
				top: 0rpx;

				image {
					width: 34rpx;
					height: 38rpx;
				}
			}
		}

		.withdraw {
			margin-top: 16rpx;
			width: 100%;
			display: flex;
			justify-content: flex-end;

			.withdraw-btn {
				width: 174rpx;
				height: 60rpx;
				background-color: #ffffff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30rpx;
				color: #FF0000;
			}
		}

		.balance {
			width: 100%;
			// height: 150rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 72rpx;
			color: #FFFFFF;
			margin-top: 42rpx;

			.spe {
				font-size: 48rpx;
				color: #FFFFFF;
			}
		}
	}

	.freeze-box {
		// margin-top: -44rpx;
		position: absolute;
		left: 0;
		top: 320rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;
		padding: 0 24rpx;
		// z-index: 99;

		.freeze {
			width: 100%;
			display: flex;
			align-items: center;
			background-color: #FFFFFF;
			border-radius: 10rpx;
		}

		.line {
			width: 2rpx;
			height: 54rpx;
			background-color: #EEEEEE;

		}

		.freeze-item {
			padding: 78rpx 0;
			width: 50%;
			height: 124rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.title {
				font-size: 32rpx;
				color: #333;
			}

			.price {
				margin-top: 6rpx;
				font-size: 28rpx;
				color: #666;
			}
		}
	}

	.navbar {
		margin-top: 94rpx;
		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: $font-color-spec;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $font-color-spec;
				}
			}
		}
	}

	// 列表

	.swiper-box {
		height: calc(100% - 360px);
		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: calc(100% - 100px);
	}

	.content {
		height: 100%;

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

	.sy {
		height: 44rpx;
		width: 100%;
		background-color: #FE4040;
	}
</style>