<template>
	<view class="">
		<u-navbar
			title="我的业绩"
			:placeholder="true"
			:autoBack="true"
			 :safeAreaInsetTop="true"
		>
		</u-navbar>
		<view class="tabs-wrap">
			<u-tabs 
			:list="tabsList" 
			lineColor="#00A447" 
			 :activeStyle="{color:'#333','font-weight': '600','font-size':'30rpx'}"
			 :inactiveStyle="{color:'#999'}"
			@click="tabsClick"></u-tabs>
		</view>
		<view class="totalAmount">
			合计：{{totalAmount}}
		</view>
		<mescroll-body class="" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="page-wrap" v-show="dataList.length>0" >
				<view class="list">
					<view class="item u-flex u-row-between" v-for="item in dataList" :key="item.id">
						<view class="left">
							<view class="name">{{item.billTitle}}</view>
							<view class="time">{{item.createTime}}</view>
						</view>
						<text class="num plus">+{{item.earningsMoney}}</text>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				totalAmount:'',
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				tabsList:[{name:'昨日',recordType:1},{name:'本月',recordType:2},{name:'历史',recordType:3}],
				recordType:'',
				params:{
					startTime:'',
					endTime:'',
				},
				activeIndex:0,
				dataList: []
			}
		},
		onShow() {
			
		},
		onLoad() {
			this.recordType = this.tabsList[this.activeIndex].recordType;
			// console.log('1111', this.tabsList[this.activeIndex]);
		},
		methods: {
			/*下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
			downCallback(){
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调*/
			upCallback(page) {
				// 此处可以继续请求其他接口
				// if(page.num == 1){
				// 	// 请求其他接口...
				// }
			
				// 如果希望先请求其他接口,再触发upCallback,可参考以下写法
				// if(!this.params.id){
				// 	this.mescroll.endErr()
				// 	return // 此处return,先获取xx
				// }
			
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条isAsc:0//时间排序 0:降序 1:升序 (默认星级降序排序)
				console.log('recordType',this.recordType);
				if(this.recordType==1){
					var yesterday = new Date();
					yesterday.setDate(yesterday.getDate() - 1);
					var year = yesterday.getFullYear();
					var month = yesterday.getMonth() + 1;
					var date = yesterday.getDate();
					// 格式化日期，补0操作
					month = month < 10 ? '0' + month : month;
					date = date < 10 ? '0' + date : date;
					var yesterdayStr = year + '-' + month + '-' + date;
					this.params.startTime = yesterdayStr;
					this.params.endTime = yesterdayStr;
				}else if(this.recordType==2){
					const now = new Date();
					this.params.startTime =  uni.$u.timeFormat(new Date(now.getFullYear(), now.getMonth(), 1), 'yyyy-mm-dd hh:MM:ss');
					this.params.endTime =  uni.$u.timeFormat(now, 'yyyy-mm-dd hh:MM:ss');
				}else if(this.recordType==3){
					this.params.startTime = '',
					this.params.endTime =  ''
				}

				this.params = Object.assign(this.params,{pageNum:pageNum,pageSize:pageSize});
				this.$u.api.achievement(this.params).then(data => {
					console.log('data',JSON.parse(JSON.stringify(data)));
					// 接口返回的当前页数据列表 (数组)
					let curPageData = data.data.pageList.rows;
					this.totalAmount = data.data.totalAmount;
					console.log('curPageData',JSON.parse(JSON.stringify(curPageData)));
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length; 
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					// let totalPage =  data.data.data.totalPage; 
					// 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
					let totalSize = curPageData.total; 
					// 接口返回的是否有下一页 (true/false)
					// let hasNext = data.xxx; 
					// console.log('totalPage',totalPage,'curPageLen',curPageLen);
					//设置列表数据
					if(page.num == 1) this.dataList = []; //如果是第一页需手动置空列表
					this.dataList = this.dataList.concat(curPageData); //追加新数据
					// 请求成功,隐藏加载状态
					//方法一(推荐): 后台接口有返回列表的总页数 totalPage
					// this.mescroll.endByPage(curPageLen, totalPage); 
					//方法二(推荐): 后台接口有返回列表的总数据量 totalSize
					this.mescroll.endBySize(curPageLen, totalSize); 
				}).catch(err => {
					this.mescroll.endErr()
					console.log(err)
				});	
			
			},
			/*若希望重新加载列表,只需调用此方法即可(内部会自动page.num=1,再主动触发up.callback)*/
			reloadList() {
				this.mescroll.resetUpScroll();
			},
			tabsClick(item){
				this.recordType = item.recordType;
				this.reloadList()
				// console.log('item',item);
			},
		}
	}
</script>
<style>
page{
	background-color: #f5f5f5;
}
</style>
<style lang="scss" scoped>
.totalAmount{
	padding: 20rpx;
	background-color: #fff;
	border-radius: 8rpx;
	margin: 0 0 20rpx;
	font-size: 36rpx;
	font-weight: 600;
	color: #00A447;
}
.tabs-wrap{
	background-color: #fff;
	margin-bottom: 10rpx;
}
.list{
	border-radius: 8rpx;
	padding: 0 20rpx;
	background-color: #fff;
	.item{
		padding: 20rpx 0;
		background-color: #fff;
		.name{
			font-size: 30rpx;
			font-weight: 400;
			color: #333333;
			line-height: 42rpx;
			margin-bottom: 8rpx;
		}
		.time{
			font-size: 24rpx;
			font-weight: 400;
			color: #999999;
			line-height: 33rpx;
		}
		.num{
			font-size: 30rpx;
			font-weight: 600;
			color: #FFB100;
			&.plus{
				color: #00A447;
			}
		}
		&:not(:first-of-type){
			border-bottom: 0.5px solid #ddd;
			padding-bottom: 20rpx;
		}
	}
}
</style>