<template>
	<view class="page-wrap">
		<!-- <u-navbar
			title="积分商城"
			:placeholder="true"
			:autoBack="true"
			:safeAreaInsetTop="true"
		>
		</u-navbar> -->
		<view class="header">
			<view class="inner">
				<view class="top u-flex u-row-between">
					<view class="u-flex">
						<img src="../static/img/integral-ico.png" alt="">
						<text>积分余额</text>
					</view>
					<view class="right u-flex" @click="$u.route('/pages/regulation/regulation',{regulationName:'积分规则'})">
						<u-icon name="error-circle" color="#fff" size="34rpx"></u-icon>
						<text>规则</text>
					</view>
				</view>
				<view class="num" @click="$u.route('/center/addcredits')">{{credit}}</view>
				<!-- <view class="bottom">
					<text  @click="$u.route('/credits/creditslist')">积分明细</text>
				</view> -->
			</view>
		</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>
		<view class="">
			<mescroll-body class="" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
				<view class="credit-product u-flex u-flex-wrap">
					<view class="credit-product-item" @click="goDetails(item.id)" v-for="(item,index) in dataList" :key="item.id">
						<view class="pic">
							<u--image class="image" :src="item.mainImg" width="100%" height="340rpx"></u--image>
						</view>
						<view class="text">
							<view class="til ellipsis-2">
								{{item.goodsName}}
							</view>
							<view class="price u-flex">
								<img style="margin-right: 10rpx;" src="../static/img/integral-ico-y.png" alt="">
								{{item.exchangeCredit}}积分
							</view>
						</view>
					</view>
				</view>
			</mescroll-body>
		</view>
	</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 {
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 3, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				hasAddr:false,
				staticUrl:this.$commonConfig.staticUrl,
				tabsList:[],
				credit:'',
				hasTypeId:false,
				typeId:'',
				dataList:[],
			}
		},
		onShow() {	
			this.getCredit();//积分
			// this.getAddrList();
		},
		onLoad() {
			// this.getTypeList();
		},
		methods: {
			/*下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
			downCallback(){
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调*/
			upCallback(page) {
				// 此处可以继续请求其他接口
				// if(page.num == 1){
				// 	// 请求其他接口...
				// }
			
				// 如果希望先请求其他接口,再触发upCallback,可参考以下写法
				if(!this.hasTypeId){
					this.getTypeList();
					return // 此处return,先获取xx
				}
			
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
			
				let params = {
					pageNum : page.num,
					pageSize :  page.size,
					classifyId : this.typeId
				}
				// console.log('this.params',params);
				this.$u.api.memberGoodList(params).then(data => {
					// console.log('data',JSON.parse(JSON.stringify(data)));
					// 接口返回的当前页数据列表 (数组)
					let curPageData = data.data.rows;
					// 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 = data.data.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();
			},
			getTypeList(){
				this.$u.api.goodsTypeList({parentId:0,pageNum:1,pageSize:200}).then(res=>{
					// console.log('getTypeList',res.data.rows);
					this.hasTypeId = true;
					this.tabsList = res.data.rows;
					this.tabsList.unshift({name:'全部',id:null});
					this.typeId =res.data.rows[0].id;
					this.mescroll.resetUpScroll()
				}).catch(err=>{
					console.log('goodsTypeList',err);
				})
			},
			tabsClick(item){
				this.typeId = item.id;
				this.reloadList()
				// console.log('item',item);
			},
			getCredit(){
				this.$u.api.memberCredit().then(res=>{
					this.credit = res.data.credit;
					// console.log('memberCredit',res.data);
				}).catch(err=>{
					console.log('memberCredit',err);
				})
			},
			goDetails(id){
				// if(!this.hasAddr){
				// 	uni.showModal({
				// 		title: '温馨提示',
				// 		content: '请先设置地址！',
				// 		success: res => {
				// 			if (res.confirm) {
				// 				uni.$u.route('/center/addrlist', {
				// 					from: 'cart',
				// 					backUrl:'/credits/credits'
				// 				});
				// 			}
				// 		}
				// 	})
				// 	return
				// }
				uni.$u.route('/shopping/productdetails', {
					id: id
				});
			},
			getAddrList(){
				this.$u.api.addrList().then(res=>{
					// this.dataList = res.data.rows;
					if( res.data.total>0){
						this.hasAddr = true;
					}else{
						this.hasAddr = false;
					}
				}).catch(err=>{
					console.log('getAddrList',err.data);
				})
			},
		}
	}
</script>
<style>
	page{
		background-color: #F5F9FC;
	}
</style>
<style lang="scss" scoped>
.header{
	color: #fff;
	position: relative;
	height: 260rpx;
	margin-bottom: 30rpx;
	background: url(../static/img/supermarket-top.png) no-repeat;
	background-size: 100% 100%;
	.inner{
		padding: 40rpx;
		.top{
			font-size: 26rpx;
			text{margin-left: 10rpx;}
			margin-bottom: 30rpx;
		}
		.num{
			font-size: 50rpx;
			font-weight: 600;
			color: #FFFFFF;
			line-height: 58rpx;
		}
		.bottom{
			text-align: right;
			text{
				display: inline-block;
				padding: 4rpx 20rpx;
				background: rgba(0,0,0,0.2);
				color: #fff;
				border-radius: 23rpx;
				font-size: 22rpx;
			}
		}
	}
}
.tabs-wrap{
	background-color: #fff;
	margin-bottom: 10rpx;
}
.credit-product{
	margin-top: 20rpx;
	&-item{
		width: 47%;
		background-color: #fff;
		margin-bottom: 30rpx;
		border-radius: 24rpx;
		&:nth-child(even){
			margin-left: 6%;
		}
		.til{
			font-size: 32rpx;
			height: 80rpx;
		}
		.text{
			padding: 20rpx;
			.price{
				margin-top: 17rpx;
				font-size: 36rpx;
				font-weight: 600;
				color: #FFB100;
				line-height: 50rpx;
			}
		}
		.bottom{
			font-size: 30rpx;
			font-weight: 600;
			color: #00A447;
			line-height: 42rpx;
			.btn{
				font-size: 22rpx;
				font-weight: 400;
				color: #FFFFFF;
				padding: 5rpx 18rpx;
				background: #00A447;
				border-radius: 4px;
			}
		}
	}
}
</style>