<template>
	<view class="page-wrap">
		<u-navbar
			title="积分商城"
			:placeholder="true"
			@leftClick="leftClick"
			:autoBack="false"
			:safeAreaInsetTop="true"
		>
		</u-navbar>
		<view class="header">
			<u--image class="image" :src="staticUrl+'/img/credits-header.png'" width="100%" height="260rpx"></u--image>
			<view class="inner">
				<view class="top u-flex u-row-between">
					<text>积分商城</text>
					<view class="right u-flex" @click="$u.route('/credits/regulation',{regulationName:'积分规则'})">
						<u-icon name="error-circle" color="#fff" size="34rpx"></u-icon>
						<text>规则</text>
					</view>
				</view>
				<view class="num">{{credit}}</view>
				<view class="bottom">
					<text  @click="$u.route('/credits/creditslist')">积分明细</text>
				</view>
			</view>
		</view>
		<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="">
			<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" v-if="vuex_member_info.priceType>1">
								¥{{item.salePrice}}
								<!-- <text class="vip-icon">VIP</text> -->
							</view>
							<view class="price" v-else>¥ {{ item.exchangeType==0?0:item.salePrice}}</view>
							<view class="bottom u-flex u-row-between">
								<text>+{{item.exchangeCredit}}积分</text>
								<text class="btn">兑换</text>
							</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 {
				hasAddr:false,
				staticUrl:this.$commonConfig.staticUrl,
				tabsList:[],
				credit:'',
				hasTypeId:false,
				typeId:'',
				dataList:[],
			}
		},
		onShow() {	
			this.getCredit();//积分
			this.getAddrList();
		},
		onLoad() {
			this.getTypeList();
		},
		methods: {
			leftClick(e){
				let pages = getCurrentPages();
				if(pages.length==1){
					uni.$u.route('/pages/index/index')
				}else{
					uni.navigateBack()
				};
			},
			/*下拉刷新的回调, 重置列表为第一页 (此处可删,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,
					parentTypeId : this.typeId,
					isCredit:1
				}
				// 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}).then(res=>{
					console.log('getTypeList',res.data);
					this.hasTypeId = true;
					this.tabsList = res.data;
					this.typeId =res.data[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: #F5F5F5;
	}
</style>
<style lang="scss" scoped>
.header{
	color: #fff;
	position: relative;
	height: 260rpx;
	margin-bottom: 30rpx;
	/deep/ .u-image{
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		z-index: -1;
	}
	.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;
		&:nth-child(even){
			margin-left: 6%;
		}
		.til{
			font-size: 32rpx;
			height: 80rpx;
		}
		.text{
			padding: 20rpx;
			.price{
				margin-top: 17rpx;
				font-size: 24rpx;
				font-weight: 600;
				color: #FF3C3F;
				line-height: 33rpx;
			}
		}
		.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>