<template>
	<view class="content">
		<view class="user-top">
			<view class="select-time">
				<picker mode="date" @change="bindDateChange" fields="month">
					<view class="uni-input">{{showdate}}</view>
				</picker>
				<image src="../../static/img/downjian.png" mode="widthFix"></image>
			</view>
			<view class="top-val">
				{{yg_detail.reward || '0'}}
			</view>
			<view class="top-tit">
				当前提成
			</view>
			<view class="top-btn">
				<view class="left" @click="goPage('/pagesS/yg/txxq')">
					提现记录
				</view>
				<view class="center">
					
				</view>
				<view class="right" @click="goPage('/pagesS/yg/gztq?id=' + id)">
					提成提现
				</view>
			</view>
		</view>
		
		<view class="sz-wrap">
			<view class="sz-item">
				<view class="sz-tit">
					累计收入
				</view>
				<view class="sz-val">
					{{sr|| '0'}}
				</view>
			</view>
			<view class="jg">

			</view>
			<view class="sz-item">
				<view class="sz-tit">
					累计支出
				</view>
				<view class="sz-val">
					{{zc || '0'}}
				</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.title }}</view>
		</view>
		<swiper :current="tabCurrentIndex" :style="{ height: height }" class="swiper-box" duration="300" disable-touch>
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navlist" :key="tabIndex">
				<scroll-view scroll-y="true" class="list-scroll-content" @scrolltolower="loadData"
					:style="{ height: height }">
					<!-- 空白页 -->
					<u-empty text="暂无数据" mode="list" v-if="tabItem.loaded && tabItem.list.length == 0"></u-empty>

					<!-- 订单列表 -->
					<view class="order-item" v-for="(item, index) in tabItem.list" :key="index">
						<view class="title-box">
							<view class="title">
								<!-- <text>{{ item.mark }}</text> -->
								{{item.title}}
							</view>
							<view class="time">
								<!-- <text>{{ item.create_time }}</text> -->
								{{$utils.formatDate(item.createTime)}}
							</view>
						</view>
						<view class="money">
							<!-- <view>{{ (item.pm == 0 ? '-' : '+') + item.number }}</view> -->
							{{tabCurrentIndex == 0? '+': '-'}}{{item.amount}}
						</view>
					</view>
					<u-loadmore :status="tabItem.loadingType" />
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showdate: '',
				tabCurrentIndex: 0,
				height: '',
				id: '',
				navlist: [{
						status: 5,
						title: '收入',
						list: [],
						page: 1,
						pageSize: 10,
						loaded: false,
						loadingType: 'loadmore'
					},
					{
						status: 4,
						title: '扣除',
						list: [],
						page: 1,
						pageSize: 10,
						loaded: false,
						loadingType: 'loadmore'
					}
				],
				star: '',
				end: '',
				sr: '',
				zc: '',
				yg_detail: {}
			}
		},
		onLoad(opt) {
			if (opt.id) {
				this.id = opt.id
			}else {
				this.id = this.$store.state.userInfo.staffId
			}
			
		},
		onShow() {
			this.getTime()
			this.getYgdetail()
		},
		onReachBottom() {

		},
		onReady() {
			var _this = this;
			uni.getSystemInfo({
				success: resu => {
					const query = uni.createSelectorQuery();
					query.select('.swiper-box').boundingClientRect();
					query.exec(function(res) {
						_this.height = resu.windowHeight - res[0].top + 'px';
						console.log('打印页面的剩余高度', _this.height);
					});
				},
				fail: res => {}
			});
		},
		methods: {
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
				this.loadData('tab')
			},
			// 选择年月
			bindDateChange(e) {
				let time = e.detail.value.split('-')
				this.showdate = time[0] + '年' + time[1] + '月'
				this.star =  this.getFirstDayOfMonth(time[0],time[1])
				this.end =  this.getLastDayOfMonth(time[0],time[1])
				this.loadData('reload')
			},
			// 获取当前年月
			getTime() {
				let date = new Date()
				this.showdate = date.getFullYear() + '年' + (date.getMonth() + 1) + '月'
				// console.log(this.getFirstDayOfMonth(date.getFullYear(),date.getMonth() + 1),'开始时间')
				// console.log(this.getLastDayOfMonth(date.getFullYear(),date.getMonth() + 1))
				this.star = this.getFirstDayOfMonth(date.getFullYear(),date.getMonth() + 1)
				this.end = this.getLastDayOfMonth(date.getFullYear(),date.getMonth() + 1)
				this.loadData('reload')
			},
			// 获取数据
			loadData(type) {
				let that = this
				let index = that.tabCurrentIndex
				let item = that.navlist[index]
				if (type == 'reload' || type == 'tab') {
					item.loaded = false
					item.loadingType = 'loadmore'
					item.page = 1
					item.list = []
				}
				if (item.loadingType == 'loading' || item.loadingType == 'nomore') {
					return
				}

				item.loadingType = 'loading'
				that.$u.api.getYgTc({
					staffId: that.id,
					type: item.status,
					page: item.page,
					pageSize: item.pageSize,
					star: that.star,
					end: that.end //搜索结束时间
				}).then(res => {
					item.list = item.list.concat(res.data)
					item.page++
					that.sr = res.sr
					that.zc = res.zc
					if (item.pageSize == res.data.length) {
						item.loadingType = 'loadmore'
					} else {
						item.loadingType = 'nomore'
					}
					item.loaded = true
				})

			},
			getFirstDayOfMonth(year, month) {
				// return new Date(year, month-1, 1);
				let data = new Date(year, month - 1, 1);
				return data.getTime()/1000
				// return (
				// 	data.getFullYear() +
				// 	"-" +
				// 	(data.getMonth() + 1 > 9 ?
				// 		data.getMonth() + 1 :
				// 		"0" + (data.getMonth() + 1)) +
				// 	"-" +
				// 	(data.getDate() > 9 ? data.getDate() : "0" + data.getDate())
				// );
			},
			//结束日期
			getLastDayOfMonth(year, month) {
				let data = new Date(year, month, 0);
				console.log('结束',data.getTime())
				return data.getTime()/1000
				// return (
				// 	data.getFullYear() +
				// 	"-" +
				// 	(data.getMonth() + 1 > 9 ?
				// 		data.getMonth() + 1 :
				// 		"0" + (data.getMonth() + 1)) +
				// 	"-" +
				// 	(data.getDate() > 9 ? data.getDate() : "0" + data.getDate())
				// );
			},
			getYgdetail() {
				this.$u.api.getYgdetail({
					request_id: this.id
				}).then(({data})=> {
					this.yg_detail = data
					uni.setNavigationBarTitle({
						title: this.yg_detail.staffName + '的提成'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.user-top {
		width: 100%;
		height: 400rpx;
		background-color: #4075d6;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding-bottom: 60rpx;

		// position: relative;
		.top-tit {
			font-size: 30rpx;
			font-weight: bold;
			color: #FFFFFF;
		}

		.top-val {
			font-size: 72rpx;
			font-weight: 500;
			color: #FFFFFF;
		}

		.select-time {
			position: absolute;
			top: 30rpx;
			right: 30rpx;
			font-size: 26rpx;
			font-weight: 500;
			color: #fff;
			display: flex;
			align-items: center;

			image {
				width: 17rpx;
				margin-left: 10rpx;
			}
		}
	}

	.sz-wrap {
		width: 670rpx;
		height: 186rpx;
		background: #FFFFFF;
		box-shadow: 0px 0px 20rpx 0px rgba(50, 50, 52, 0.06);
		border-radius: 20rpx;
		margin: -90rpx auto 20rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		.jg {
			width: 2rpx;
			height: 74rpx;
			background: #DCDFE6;
		}

		.sz-item {
			width: 330rpx;
			height: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 30rpx;
			font-weight: bold;

			.sz-tit {
				color: #999999;

			}

			.sz-val {
				margin-top: 30rpx;
				color: #181818;
			}
		}
	}

	.navbar {
		margin-top: 20rpx;
		display: flex;
		height: 88rpx;
		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: #999999;
			position: relative;

			&.current {
				color: #000;

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

	//列表
	.swiper-box {
		.order-item:last-child {
			margin-bottom: 60rpx;
		}

		.order-item {
			width: 100%;
			// background-color: red;
			padding: 20rpx 30rpx;
			line-height: 1.5;
			display: flex;
			align-items: center;

			.title-box {
				flex-grow: 1;
				padding-right: 50rpx;

				.title {
					font-size: 30rpx;
					color: #333333;
				}

				.time {
					font-size: 26rpx;
					color: #AEAEAE;
				}
			}

			.money {
				color: #fd5b23;
				font-size: 26rpx;
				text-align: right;

				.status {
					color: #AEAEAE;
				}
			}
		}
	}

	.list-scroll-content {
		background: #ffffff;
		height: 100%;
	}
	.top-btn {
		margin-top: 20rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;
		padding: 0 50rpx;
		font-size: 30rpx;
		font-weight: bold;
		color: #FFFFFF;
		.right {
			text-align: right;
		}
		.left {
			text-align: left;
		}
		.center {
			text-align: center;
		}
		view {
			width: 33.3%;
		}
	}
</style>
