<template>
	<view class="content">
		<view class="cont-box">
			<view class="top-box">
				<view class="my-integral">
					<view class="left">
						<view class="label">我的积分</view>
						<view class="integral">{{integral}}</view>
					</view>
					<view class="btn-box">
						<view class="btn" @click="loginNavTo('/pages_user/user/integralLogsList')">获得记录</view>
						<view class="btn" style="margin-bottom: 0;" @click="loginNavTo('/pages_user/user/integralOrderList')">兑换记录</view>
					</view>
				</view>
				<view class="navbox">
					<view class="navbox-item" @click="goCart()">
						<view class="box">
							<u-badge type="error" max="99" :value="cartNum" bgColor="#2BC7B9"></u-badge>
						</view>
						<image src="/static/tabbar/cart_sel.png" mode="aspectFill"></image>
						<text>购物车</text>
					</view>
					<view class="navbox-item" @click="handleOrder(0)">
						<image src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/payment.png" mode="aspectFill"></image>
						<text>全部订单</text>
					</view>
					
					<view class="navbox-item" @click="handleOrder(1)">
						<image src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/send_goods.png" mode="aspectFill"></image>
						<text>待发货</text>
					</view>
					<view class="navbox-item" @click="handleOrder(3)">
						<image src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/sou_goods.png" mode="aspectFill"></image>
						<text>已完成</text>
					</view>
					<view class="navbox-item" @click="handleOrder(4)">
						<image src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/after_sales.png" mode="aspectFill"></image>
						<text>已取消</text>
					</view>
				</view>
				<view class="tabs" v-if="tabs.length>0">
					<u-tabs
					 :current="tabIndex"
					 :scrollable="true"
					 :list="tabs"  
					 lineColor="#2BC7B9"
					@change="tabChange">
					</u-tabs>
				</view>
			</view>
			<mescroll-body :top="top+'px'" bottom="0"  ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="integral-box"  >
				<view class="item"  @click="navTo('/pages_user/user/integralGoodsDetails?goodsId='+item.goodsId)" v-for="(item,index) in dataList">
					<view class="top">
						<image :src="item.imgUrl"></image>
					</view>
					<view class="bottom">
						<view class="title ellipsis2">
							{{item.goodsName}}
						</view>
						<view class="price-box">
							<view class="price">{{item.integral}}积分</view>
							<view class="price-val" v-if="item.cash > 0">+{{item.cash.toFixed(2)}}<text style="font-size: 20rpx;font-weight: normal;">元</text></view>
							<!-- <view class="count">价值:{{item.otPrice.toFixed(2)}}元</view> -->
						</view>
					</view>
				</view>
			</view>
			</mescroll-body>
		</view>
		
	</view>
</template>

<script>
 import {getDictByKey} from '@/api/common.js'
 import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
 // import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
 import {getIntegralGoodsList,getCartCount} from '@/api/integral.js'
 import {getUserInfo} from '@/api/user'
 export default {
	mixins: [MescrollMixin], 
 	data() {
 		return {
			integral:0,
			type: null,
			typeOptions:[],
			tabIndex:0,
			tabs: [],
			mescroll:null,
			downOption: {   //下拉刷新
			 	use:true,
				auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
			},
			upOption: {
				onScroll:false,
				use: true, // 是否启用上拉加载; 默认true
				page: {
					pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					size: 10 // 每页数据的数量,默认10
				},
				noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
				textNoMore:"已经到底了",
				empty: {
					icon:'https://hdtobs.obs.cn-north-4.myhuaweicloud.com/hdt/empty_icon.png',
					tip: '暂无数据'
				}
			},
			dataList: [],
			top: 156,
			cartNum: 0
 		}
 	},
	onLoad() {
		this.getDictByKey("sys_integral_goods_type");
		this.utils.isLogin().then(
			res => {
				if(res){
					this.getUserInfo();
				}
			}
		);
	},
	onReady() {
		this.getTop()
	},
	onShow() {
		this.utils.isLogin().then(
			res => {
				if(res){
					this.getcartNum()
				}
			}
		);
	},
 	methods: {
		goCart() {
			this.utils.isLogin().then(
				res => {
					if(res){
						const url = './cart'
						uni.navigateTo({
							url: url
						})
					} else {
						uni.navigateTo({
							url: '/pages/auth/login'
						})
					}
				}
			);
		},
		getcartNum() {
			getCartCount().then(res=>{
				if(res.code == 200) {
					this.cartNum = res.data || 0
				}
			})
		},
		getTop() {
			const query = uni.createSelectorQuery().in(this);
			query
			  .select(".top-box")
			  .boundingClientRect((data) => {
			    if(data) {
					this.top = data.height + uni.upx2px(88)
				}
			  })
			  .exec();
		},
		handleOrder(current) {
			this.utils.isLogin().then(
				res => {
					if(res){
						uni.navigateTo({
							url:'/pages_user/user/integralOrderList?current='+current
						})
					} else {
						uni.navigateTo({
							url: '/pages/auth/login'
						})
					}
				}
			);
		},
		getUserInfo(){
			getUserInfo().then(
				res => {
					if(res.code==200){
						if(res.user!=null){
							this.integral=res.user.integral;
						}
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
					}
				},
				rej => {}
			);
		},
		loginNavTo(url){
			this.utils.isLogin().then(
				res => {
					if(res){
						uni.navigateTo({
							url: url
						})
					}
					else{
						uni.navigateTo({
							url:'/pages/auth/login'
						})
					}
				}
			);
			 
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			})
		},
		getDictByKey(key){
			var data={key:key}
			var that=this;
			getDictByKey(data).then(
				res => {
					if(res.code==200){
						this.typeOptions=res.data;
						this.typeOptions.forEach(function(item,index){
							var data={name:item.dictLabel};
							that.tabs.push(data);
						})
						this.tabs.unshift({name: '全部'})
						if(this.tabs.length>0){
							this.tabIndex=0
						}
						
					}
				},
				err => {
				}
			);
			
		},
		tabChange(item){
			this.tabIndex = item.index
			this.type = item.index == 0 ? null : this.typeOptions[item.index-1].dictValue;
			this.mescroll.resetUpScroll()
		},
		mescrollInit(mescroll) {
			this.mescroll = mescroll;
		},
		/*下拉刷新的回调 */
		downCallback(mescroll) {
			mescroll.resetUpScroll()
		},
		upCallback(page) {
			//联网加载数据
			var that = this;
			var data = {
				pageNum: page.num,
				pageSize: page.size
			};
			if(this.typeOptions&&this.typeOptions.length>0) {
				this.type = this.tabIndex == 0 ? null : this.typeOptions[this.tabIndex-1].dictValue;
			}
			if(this.type!=null){
				data.goodsType=this.type
			}
			getIntegralGoodsList(data).then(res => {
				if(res.code==200){
					//设置列表数据
					if (page.num == 1) {
						that.dataList = res.data.list; 
						
					} else {
						that.dataList = that.dataList.concat(res.data.list);
						 
					}
					that.mescroll.endBySize(res.data.list.length, res.data.total);
					
				}else{
					uni.showToast({
						icon:'none',
						title: "请求失败",
					});
					that.dataList = null;
					that.mescroll.endErr();
				}
			});
		}
		 
 	}
 }
 
 
</script>

<style>
	page{
		height: 100%;
		background-color: #f5f5f5;
	}
</style>
<style scoped lang="scss">
@mixin u-flex($flexD, $alignI, $justifyC) {
	display: flex;
	flex-direction: $flexD;
	align-items: $alignI;
	justify-content: $justifyC;
}
.navbox {
	@include u-flex(row,center,space-between);
	padding: 26rpx;
	background: #FFFFFF;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	font-size: 24rpx;
	&:last-child {
		margin-right: 0;
	}
	&-item {
		flex:1;
		margin-right: 18rpx;
		@include u-flex(column,center,center);
		position: relative;
	}
	&-iteminfo {
		// width: 162rpx;
		// height: 162rpx;
		width: 100%;
		padding: 24rpx;
		box-sizing: border-box;
		background: rgba(255,255,255,0.7);
		border-radius: 24rpx 24rpx 24rpx 24rpx;
		@include u-flex(row,center,center);
		font-size: 26rpx;
		color: #222222;
	}
	image {
		width: 48rpx;
		height: 48rpx;
		margin-bottom: 10rpx;
	}
	.box {
		position: absolute;
		top: -10rpx;
		right: 0;
		z-index: 99;
	}
}
.content{
	height: 100%;
	.cont-box{
		.top-box{
			z-index: 999;
			padding: 30rpx 30rpx 0 30rpx;
			width: 100%;
			position: fixed;
			top: 0rpx;
			left: 0rpx;
			background-color: #f5f5f5;
			.my-integral{
				box-shadow: 0px 0px 5px 2px rgba(0,0,0,0.05);
				background: linear-gradient(#2BC7B9, #a0f6ff);
				border-radius: 30rpx;
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				padding: 30rpx;
				margin-bottom: 24rpx;
				.left{
					.label{
						font-size: 28upx;
						font-family: PingFang SC;
						color: #fff;
					}
					.integral{
						margin-top: 30rpx;
						font-weight: bold;
						font-size: 40upx;
						font-family: PingFang SC;
						color: #fff;
					}
				}
				.btn-box{
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					justify-content: flex-start;
					.btn{
						margin-bottom: 30rpx;
						background-color: #fff;
						border-radius: 30rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						padding: 10rpx 15rpx;
						font-size: 20upx;
						font-family: PingFang SC;
						color: #2BC7B9;
					}
				}
			}
			.tabs{
				height: 88rpx;
			}
		}
		.integral-box{
			padding: 30rpx;
			display: flex;
			align-items: flex-start;
			justify-content: flex-start;
			flex-wrap: wrap;
			.item{
				box-shadow: 0px 0px 5px 2px rgba(0,0,0,0.05);
				background-color: #fff;
				width: calc(50% - 20rpx);
				border-radius: 15rpx;
				margin: 10rpx;
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				justify-content: flex-start;
				&:last-child{
				}
				.top{
					width:100%;
					height:300rpx;
					image{
						border-radius: 15rpx 15rpx 0rpx 0rpx;
						width:100%;
						height:300rpx;
					}
					
				}
				.bottom{
					width: 100%;
					padding: 15rpx;
					.title{
						font-weight: bold;
						font-size: 28upx;
						font-family: PingFang SC;
						color: #111111;
					}
					.price-box{
						margin-top: 10rpx;
						display: flex;
						align-items: center;
						// justify-content: space-between;
						width: 100%;
						.price{
							padding: 5rpx 10rpx;
							background-color: #2BC7B9;
							border-radius: 30rpx;
							font-size: 20upx;
							font-family: PingFang SC;
							color: #ffffff;
						}
						.price-val {
							font-size: 28rpx;
							font-weight: bold;
							font-family: PingFang SC;
							color: #2BC7B9;
						}
						.count{
							font-size: 24upx;
							font-family: PingFang SC;
							color: #333333;
						}
						
					}
				}
			}
		}
	}
}
 

</style>
