<template>
	<view class="content">
		<!-- <view class="navbar coupon">
			<view class="nav-item grenn" v-if="tabCurrentIndex == 0">
				一级总业绩：
				<text>{{ one_price }}</text>
			</view>
			<view class="nav-item black" v-if="tabCurrentIndex == 1">
				二级总业绩：
				<text>{{ two_price }}</text>
			</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 }}:{{item.number}}人</view>
		</view> -->
		<view class="order">
			<view class="flex views_dates">
				<view class="flex" style="width: 40%;">
					<view class="uni-list">
						<view class="uni-list-cell">
							<view class="uni-list-cell-db">
								<picker mode="date" :value="startDate" start="2001-01" fields="month" @change="bindDateChange1">
									<view class="uni-input">{{startDate}}</view>
								</picker>
							</view>
						</view>
					</view>
					--
					<view class="uni-list">
						<view class="uni-list-cell">
							<view class="uni-list-cell-db">
								<picker mode="date" :value="endDate" start="2001-01" fields="month" @change="bindDateChange2">
									<view class="uni-input">{{endDate}}<text class="iconfont iconenter"></text></view>	
								</picker>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="mony"><text style="font-size: 36rpx;">￥</text>{{ pinkNums || '0' }}</view>
			<view class="total">总金额</view>
			<view class="top-box">
				<view class="box">
					<view class="box-1">人头奖</view>
					<view class="box-2"><text class="symbool">￥</text>{{ peoNum || '0' }}</view>
				</view>
				<view class="box">
					<view class="box-1">分佣奖</view>
					<view class="box-2"><text class="symbool">￥</text>{{ num_fen || '0' }}</view>
				</view>
			</view>
		</view>
		
		<swiper :style="{ height: maxheight + 'px' }" class="swiper-box">
			<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.orderList.length === 0"></empty>
					<!-- 订单列表 -->
					<view v-for="(item, index) in tabItem.orderList" :key="index" class="order-item">
						<view class="order-tpl">
							<view class="time">{{ item.time }}</view>
							<view class="count">本月累计拼团订单：{{ item.count }}单</view>
						</view>
						<view v-for="ls in item.child" class="title-list">
							<view class="title-box">
								<view class="flex">
									<view class="order_id">订单编号：{{ ls.order_id }}</view>
									<!-- 0：待发货；1：待收货；2：已收货；3：待评价 -->
									<!-- {{ ls.status == 0 ? '待付款' : ls.status == 1 ? '待发货' : ls.status == 2 ? '待收货' : ls.status == 3 ? '待评价' : ls.status == 4 ? '已完成' :'退款' }} -->
									<view class="status" v-if="ls.refund_status == 0">
										<view v-if="ls.paid == 1">{{ls.status == 0 ? '待发货' : ls.status == 1 ? '待收货' : ls.status == 2 ? '已收货' : '待评价'}}</view>
										<view v-else>未支付</view>
									</view>
									<view class="status" v-else>{{ls.refund_status == 1 ? '退款中' :'退款成功'}}</view>
								</view>
								<view class="flex_item order-list">
									<view class="title-avatar">
										<image :src="ls.avatar"></image>
									</view>
									<view class="order-info">
										<view class="flex">
											<view class="nickname">{{ ls.nickname }}</view>
											<view class="number">
												返佣：
												<text>{{ ls.number }}</text>
											</view>
										</view>
										<view class="flex">
											<view>{{ ls.time }}</view>
											<view>订单金额：{{ ls.pay_price }}</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import { orderPink } from '@/api/team.js';
	import { mapState, mapMutations } from 'vuex';
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from '@/components/empty';
	import {
		getTime
	} from '@/utils/rocessor.js';
	// 获取当前时间的秒数
	const time = Math.floor(new Date().getTime() / 1000);
	export default {
		components: {
			empty,
			uniLoadMore
		},
		filters: {
			lastTime: function(value) {
				const day = Math.floor((time - value) / 60 / 60 / 24);
				return day == 0 ? '今天登陆' : day + '天前登陆';
			}
		},
		onReady() {
			// 初始化获取页面宽度
			uni.createSelectorQuery()
				.select('.content')
				.fields({
						size: true
					},
					data => {
						console.log(data);
						// 保存头部高度
						this.maxheight = data.height - Math.floor((data.width / 750) * 340);
						console.log(this.maxheight);
					}
				)
				.exec();
		},
		data() {
			return {
				// 头部图高度
				maxheight: '',
				tabCurrentIndex: 0,
				navList: [{
					state: 0,
					text: '一级',
					loadingType: 'more',
					orderList: [],
					number: 0,
					page: 1, //当前页数
					limit: 10 //每次信息条数
				}],
				all: '',
				list: '',
				total: '',
				totalLevel: '',
				img: 'http://kaifa.crmeb.net/uploads/attach/2019/08/20190807/723adbdd4e49a0f9394dfc700ab5dba3.png',
				// one_price: '', //一级总金额
				// two_price: '', //二级总金额
				// total_pay_price: '',
				timelist: ['全部', '今天', '昨天', '最近7天', '最近30天', '本月', '本年'],
				typelist: ['全部', '一级订单', '二级订单'],
				startDate: '开始时间',
				endDate: '结束时间',
				start: '',
				end: '',
				val1: '',
				val2: '',
				timedata: '',
				dateDay: [],	// 传的时间参数
				num_fen: '',	// 分佣奖
				peoNum: '',		// 人头奖
				pinkNums: ''	// 总金额
			};
		},
		onLoad(options) {},
		onShow() {
			this.loadData();
		},
		methods: {
			//选择开始时间
			bindDateChange1: function(e) {
				console.log(11)
				let obj = this;
				let val = e.detail.value;
				obj.val1 = e.detail.value;
				// var reg = /-/g;
				obj.startDate = e.detail.value;
				console.log(obj.startDate,965)
				var formatDate1 = new Date(obj.val1);
				var formatDate2 = new Date(obj.timedata);
				console.log(obj.val1, obj.timedata, '开始时间不得大于现在时间');
				console.log(formatDate1, formatDate2, '开始时间不得大于现在时间');
				if (formatDate1 > formatDate2) {
					console.log(11);
					uni.showToast({
						title: '开始时间不得大于现在时间',
						duration: 2000,
						position: 'top',
						icon: 'none'
					});
				} else {
					console.log(22);
				}
			},
			//选择结束时间
			bindDateChange2: function(e) {
				console.log(11)
				let obj = this;
				if (obj.val1 == '') {
					uni.showToast({
						title: '请先选择开始时间',
						duration: 2000,
						position: 'top',
						icon: 'none'
					});
				} else {
					let val = e.detail.value;
					obj.val2 = e.detail.value;
					// var reg = /-/g;
					// obj.endDate = val.replace(reg, '\/');
					 obj.endDate = e.detail.value
					console.log(obj.endDate, 22);
					var formatDate1 = new Date(obj.val1);
					var formatDate2 = new Date(obj.timedata);
					var formatDate3 = new Date(obj.val2);
					console.log(obj.val1, obj.val2, obj.timedata, '123');
					console.log(formatDate1, formatDate3, formatDate2, '456');
					
					let arr = []
					arr[0] = this.startDate;
					arr[1] = this.endDate;
					this.dateDay = arr.join(';')
					console.log(this.dateDay,664853)
					if (formatDate1 > formatDate2) {
						uni.showToast({
							title: '开始时间不得大于现在时间',
							duration: 2000,
							position: 'top',
							icon: 'none'
						});
					} else {
						if (formatDate3 < formatDate1) {
							// console.log(111)
							uni.showToast({
								title: '结束时间要大于开始时间',
								duration: 2000,
								position: 'top',
								icon: 'none'
							});
						}
						if (formatDate3 > formatDate2) {
							uni.showToast({
								title: '结束时间不得大于现在时间',
								duration: 2000,
								position: 'top',
								icon: 'none'
							});
						} else {
							let index = this.tabCurrentIndex;
							let navItem = this.navList[index];
							navItem.orderList = [];
							navItem.loadingType = 'more'
							this.loadData()
						}
					}
				}
			},
			//获取日期
			getDate() {
				let date = new Date();
				let dateYear = date.getFullYear(); //获取年
				let dateMonth = date.getMonth() + 1; //获取月
				let dateDate = date.getDate(); //获取当日
				this.timedata = dateYear + '-' + dateMonth + '-' + dateDate;
			},
			// 页面跳转
			navto(e) {
				uni.navigateTo({
					url: e
				});
			},
			//获取收入支出信息
			async loadData(source) {
				let obj = this;
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// if (source === 'tabChange' && navItem.loaded === true) {
				// 	//tab切换只有第一次需要加载数据
				// 	return;
				// }
				// if (navItem.loadingType === 'loading') {
				// 	//防止重复加载
				// 	return;
				// }
				// // 修改当前对象状态为加载中
				// navItem.loadingType = 'loading';
				orderPink({
						date: obj.dateDay
					})
					.then(({
						data
					}) => {
						// let arr = data.list.map(e => {
						// 	e.lastTimeString = getTime(e.lastTime);
						// 	return e;
						// });
						let arr = data.list;
						obj.pinkNums = data.prize_sum;	// 总金额
						obj.peoNum = data.people_prize_sum;	// 人头奖
						obj.num_fen = data.pink_prize_sum;	// 分佣奖
						navItem.orderList = navItem.orderList.concat(data.list);
						console.log(navItem.orderList, 'navItem.orderList');
						navItem.loadingType = 'noMore';
						// navItem.page++;
						// if (navItem.limit == data.length) {
						// 	//判断是否还有数据， 有改为 more， 没有改为noMore
						// 	navItem.loadingType = 'more';
						// 	return;
						// } else {
						// 	//判断是否还有数据， 有改为 more， 没有改为noMore
						// 	navItem.loadingType = 'noMore';
						// }
						// 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;
			}
		}
	};
</script>

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

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

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

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

				.money {
					font-weight: bold;
					font-size: 32rpx;
					color: #db1935;
				}
			}

			.text {
				color: #666666;
			}

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

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

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

	.money-box {
		background: #bc253a;
		height: 320rpx;
		color: #ffffff;
		text-align: center;
		font-size: 35rpx;
		padding-top: 60rpx;

		.money_img {
			width: 100%;
			height: 120rpx;
			text-align: center;
			padding-top: 50rpx;
			padding-bottom: 135rpx;

			image {
				width: 120rpx;
				height: 120rpx;
				border: 4rpx solid #fd5f6f;
				border-radius: 50%;
			}
		}

		.money_name {
			width: 100%;
			text-align: center;
			font-size: 40rpx;
			padding-bottom: 25rpx;
		}
	}

	.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: $font-color;
				background-color: #dcdfe6;
				// color: #FC2A3F;
				// &:after {
				// 	content: '';
				// 	position: absolute;
				// 	left: 50%;
				// 	bottom: 0;
				// 	transform: translateX(-50%);
				// 	width: 44px;
				// 	height: 0;
				// 	border-bottom: 2px solid #FC2A3F;
				// }
			}
		}

		.grenn {
			background: #2fbd5b;
		}

		.black {
			background: #434343;
		}

		.grenn,
		.black {
			color: #ffffff;
			text-align: left;
		}
	}

	// 列表
	.order {
		background-color: $base-color;
		text-align: center;
		height: 340rpx;
		color: #ffffff;

		.mony {
			font-size: 45rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.total {
			font-size: 30rpx;
			font-weight: 500;
		}

		.top-box {
			display: flex;
			justify-content: center;
			margin-top: 20rpx;

			.box {
				flex: 1;

				.box-1 {
					font-size: 30rpx;
					margin-bottom: 10rpx;
					font-weight: 500;
				}

				.box-2 {
					font-size: 36rpx;
					font-weight: bold;
				}
			}
		}
		.symbool {
			font-size: 30rpx;
		}
		
		.views_dates {
			font-size: 24rpx;
			padding-top: 50rpx;
			padding-bottom: 40rpx;
			height: 0;
			text-align: right;
			justify-content: flex-end;
			min-height: auto;
			.uni-list {
				line-height: 1;
			}
			.uni-input {
				display: flex;
				align-items: center;
			}
			.iconenter {
				margin-bottom: 8rpx;
			}
			.clearance {
				border: 1px solid $border-color-light;
				margin: 0 15rpx;
				width: 5rpx;
			}
		}
		
	}
	
	.swiper-box {
		font-size: 28rpx;

		.order-item {
			line-height: 1.5;

			.order-tpl {
				padding: 25rpx 0rpx;
				padding-bottom: 15rpx !important;

				.count {
					color: #999999;
				}
			}

			.title-list {
				background-color: #ffffff;
				width: 100%;
				padding: 20rpx 30rpx;
				margin-top: 15rpx;

				.title-box {
					.order_id {
						color: #999999;
						padding-bottom: 15rpx;
					}

					.status {
						color: #F9273F;
					}

					.order-list {
						.title-avatar {
							width: 100rpx;
							height: 100rpx;
							margin-right: 25rpx;

							image {
								width: 100%;
								height: 100%;
								border-radius: 100%;
							}
						}

						.order-info {
							color: #999999;
							width: 80%;
							padding-left: 25rpx;

							.nickname {
								font-size: 32rpx;
								color: #333333;
								padding-bottom: 15rpx;
								width: 50%;
							}

							.number {
								text {
									color: #fa2740;
								}
							}
						}
					}
				}
			}

			.money {
				color: #db1935;
				font-size: $font-lg;
			}
		}
	}

	.list-scroll-content {
		height: 100%;
		padding: 0rpx 25rpx;
	}

	.content {
		height: 100%;

		.empty-content {
			background-color: #ffffff;
		}
	}
</style>
