<template>
	<view class="">
	<!-- 	<u-navbar
			title="我的业绩"
			:placeholder="true"
			:autoBack="true"
			 :safeAreaInsetTop="true"
		>
		</u-navbar> -->
		<view class="page-wrap">
			<img style="width: 100%;" src="../static/img/order-banner.png" alt="">
		</view>
		<view class="tabs-wrap">
			<u-tabs 
			:list="tabsList" 
			lineColor="#009AEF" 
			 :activeStyle="{color:'#333','font-weight': '600','font-size':'30rpx'}"
			 :inactiveStyle="{color:'#999'}"
			@click="tabsClick"></u-tabs>
		</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-col-top" @click="goDetail(item)" v-for="item in dataList" :key="item.id">
						<img class="img" :src="item.mainImg" alt="">
						<view class="text">
							<view class="title ellipsis-2">{{item.goodsName}}</view>
							<view class="credit">{{item.credit}}积分</view>
							<view class="bottom u-flex u-row-between">
								<view class="quantity">兑换数量:{{item.quantity}}</view>
								<u-button v-if="item.status==0" @click.native.stop="viewCode(item)" type="primary" :customStyle="{height:'58rpx',width: 'auto','margin-right': 'unset'}" text="兑换码"></u-button>
								<u-button v-if="item.status==1" @click.native.stop="goDetail(item)" type="primary" :customStyle="{height:'58rpx',width: 'auto','margin-right': 'unset','background-color':'#d1ebfd','border-color':'#d1ebfd',color:'#009AEF'}" text="详情"></u-button>
								<u-button v-if="item.status==2" @click.native.stop="goDetail(item)" type="primary" :customStyle="{height:'58rpx',width: 'auto','margin-right': 'unset','background-color':'#d1ebfd','border-color':'#d1ebfd',color:'#009AEF'}" text="详情"></u-button>
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
		<tabbar :tabbarIndexProps='1' />
	</view>
</template>

<script>
	import tabbar from "../components/tabbar.vue";
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components:{
			tabbar,
		},
		data() {
			return {
				totalAmount:'',
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				tabsList:[{name:'未领取',status:'0'},{name:'已领取',status:1},{name:'已失效',status:2}],
				status:'',
				params:{
				},
				activeIndex:0,
				dataList: []
			}
		},
		onShow() {
			
		},
		onLoad() {
			this.params.status = this.tabsList[this.activeIndex].status;
			// 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:升序 (默认星级降序排序)

				this.params = Object.assign(this.params,{pageNum:pageNum,pageSize:pageSize});
				this.$u.api.memberExchangeRecord(this.params).then(data => {
					// console.log('data',JSON.parse(JSON.stringify(data)));
					// 接口返回的当前页数据列表 (数组)
					let curPageData = data.data.rows;
					this.totalAmount = data.data.total;
					// 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.params.status = item.status;
				this.reloadList()
				console.log('item',item);
			},
			viewCode(item){
				// console.log('viewCode',item);
				uni.$u.route('/center/paycode', {
					qrContent: item.exchangeCode,
				});
			},
			goDetail(item){
				// console.log('goDetail',item);
				uni.$u.route('/shopping/orderdetails', {
					id: item.id,
				});
			}
		}
	}
</script>
<style>
page{
	background-color: #F5F9FC;
}
</style>
<style lang="scss" scoped>
.tabs-wrap{
	background-color: #fff;
	margin-bottom: 0;
}
.list{
	border-radius: 8rpx;
	padding: 0 20rpx;
	background-color: #fff;
	.item{
		padding: 28rpx 0;
		background-color: #fff;
		.img{
			width: 200rpx;
			height: 202rpx;
		}
		.text{
			flex: 1;
			margin-left: 40rpx;
			.title{
				font-size: 30rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 42rpx;
				margin-bottom: 20rpx;
			}
			.credit{
				font-size: 32rpx;
				font-family: PingFangSC-Semibold, PingFang SC;
				font-weight: 600;
				color: #FFB100;
				line-height: 44rpx;
				margin-bottom: 20rpx;
			}
			.quantity{
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 40rpx;
			}
		}
		&:not(:last-of-type){
			border-bottom: 0.5px solid #ddd;
			padding-bottom: 20rpx;
		}
	}
}
</style>