<template>
	<view class="content">
		<view class="cont-box">
			<u-sticky offsetTop="0" customNavHeight="0">
				<image class="home_top_bg" src="@/static/images/integral/coin_points_mall_top_bg.png" mode="widthFix"></image>
				<view class="top-box" :style="{paddingTop: paddingTop+'px'}">
					<!-- <view class="my-integral">
						<view class="left">
							<view class="x-ac">
								<view class="label">我的芳华币</view>
								<view class="x-ac" style="margin-left: 20rpx;" @click="goCart()">
									<u-icon name="shopping-cart" color="#fff" size="28"></u-icon>
									<view class="label">购物车</view>
								</view>
								
							</view>
							<view style="display: flex;align-items: baseline;">
								<view class="integral">{{integral}}</view>
								<view class="integralbtn" @click="goIntegral">获取芳华币</view>
							</view>
						</view>
						<view class="btn-box">
							<view class="btn" @click="navTo('/pages/user/integral/integralLogsList')">获得记录</view>
							<view class="btn" @click="navTo('/pages/user/integral/integralOrderList')">芳华币订单</view>
						</view>
					</view> -->
					<view class="inner-box">
						<view class="topbox">
							<view class="left">
								<view class="align-center">
									<!-- <image class="w32 h32" src="../../static/images/user/jifen_icon.png" mode=""></image> -->
									<text>我的芳华币</text>
								</view>
								<view class="num">{{integral}}</view>
							</view>
							<view class="guize" @click="goIntegral">获取芳华币</view>
						</view>
					    <view class="bottom-box">
							<view class="ttbg"></view>
							<view class="x-bc" style="height: 100rpx;">
								<view class="t-left" @click="navTo('/pages/user/integral/integralLogsList')">
									<image src="@/static/images/integral/points_details_icon.png" mode=""></image>
									<view>获得记录</view>
								</view>
								<view class="t-right" @click="navTo('/pages/user/integral/integralOrderList')">
									<image  src="@/static/images/integral/exchange_records_icon.png" mode=""></image>
									<view>芳华币订单</view>
								</view>
							</view>
							
						</view>
					</view>
					<!-- 搜索 -->
					<view class="search-box">
						<uni-search-bar class="search" bgColor="#FFFFFF" radius="34" v-model="keyword" placeholder="搜索商品" clearButton="auto" cancelButton="none"
							@confirm="search" @clear="handleClear">
							<template v-slot:searchIcon>
								<image src="@/static/image/hall/search_gray_icon.png" mode="aspectFill"></image>
							</template>
						</uni-search-bar>
					</view>
					<view class="tabs" v-if="tabs.length>0">
						<u-tabs
						 :current="tabIndex"
						 :scrollable="true"
						 :list="tabs"  
						 lineColor="#FF5C03"
						@change="tabChange">
						</u-tabs>
					</view>
				</view>
			</u-sticky>
			
			<view class="cartbox x-c" @click.stop="goCart()">
				<view class="box">
					<u-badge type="error" max="99" :value="cartNum" bgColor="#FF5C03"></u-badge>
				</view>
				<u-icon name="shopping-cart" color="#FF5C03" size="68rpx"></u-icon>
			</view>
			
			<mescroll-body bottom="0" :top="top2 +'px'" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="integral-box">
				<view class="item"  @click="navTo('/pages/user/integral/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="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 {getIntegralGoodsList,getFsIntegralCartList} 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://cos.his.cdwjyyh.com/fs/20240423/cf4a86b913a04341bb44e34bb4d37aa2.png',
					tip: '暂无数据'
				}
			},
			dataList: [],
			keyword:"",
			top2: uni.upx2px(432),
			paddingTop: uni.getSystemInfoSync().statusBarHeight + 12,
			cartNum: 0
 		}
 	},
	onLoad() {
		this.getDictByKey("sys_integral_goods_type");
	},
	onShow(){
		this.getUserInfo();
		this.getcartNum()
	},
	onReady() {
		this.getTop()
	},
 	methods: {
		goCart() {
			uni.navigateTo({
				url: '/pages/user/integral/cart'
			})
		},
		getcartNum() {
			getFsIntegralCartList().then(res => {
				if (res.code == 200) {
					this.cartNum = res.data.total ||0
				}
			})
		},
		getTop() {
			const query = uni.createSelectorQuery().in(this);
			query
			  .select(".top-box")
			  .boundingClientRect((data) => {
			    if(data) {
					this.top2 = data.height + uni.upx2px(88)
				}
			  })
			  .exec();
		},
		handleClear() {
			// 清除搜索关键词后刷新列表
			this.keyword = '';
			this.mescroll.resetUpScroll()
		},
		getUserInfo(){
			getUserInfo().then(
				res => {
					if(res.code==200){
						if(res.user!=null){
							this.integral=res.user.integral;
							this.mescroll.resetUpScroll()
						}
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
					}
				},
				rej => {}
			);
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			})
		},
		goCart() {
			if(this.$isLogin()){
				uni.navigateTo({
					url: '/pages/user/integral/cart'
				})
			} else {
						uni.navigateTo({
							url: '/pages/auth/loginIndex'
						})
					}
		},
		goIntegral() {
			let pages = getCurrentPages();
			let url = pages[ pages.length - 2];
			if(pages.length > 1&&url&&url.route == 'pages/user/integral/points') {
				uni.navigateBack()
			} else {
				uni.navigateTo({
					url: '/pages/user/integral/points'
				})
			}
		},
		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.type = item.index == 0 ? null : this.typeOptions[item.index-1].dictValue;
			this.mescroll.resetUpScroll()
		},
		search() {
			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,
				keyword: this.keyword
			};
			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 scoped lang="scss">
page{
	height: 100%;
	background: #F5F7FA !important;
}
.cartbox {
		position: fixed;
		bottom: 50rpx;
		right: 20rpx;
		z-index: 99;
		background-color: #fff;
		box-shadow: 0 2px 4px rgba(150, 150, 150, 0.6);
		padding: 10rpx;
		border-radius: 50%;
	
		.box {
			position: absolute;
			top: -10rpx;
			right: 0;
			z-index: 99;
		}
	}
.content{
	height: 100%;
	.home_top_bg {
		width: 100%;
		height: auto;
		position: absolute;
		top: 0;
		left: 0;
		z-index: -1;
	}
	.cont-box{
		.top-box{
			box-sizing: border-box;
			padding: 30rpx 30rpx 0 30rpx;
			width: 100%;
			z-index: 998;
			//background-color: #f5f5f5;
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			.my-integral{
				height: 200rpx;
				box-shadow: 0px 0px 5px 2px rgba(0,0,0,0.05);
				background: linear-gradient(#FF5C03, #E2C99E);
				border-radius: 30rpx;
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				padding: 30rpx;
				box-sizing: border-box;
				.integralbtn {
					font-size: 30rpx;
					font-weight: bold;
					font-family: PingFang SC;
					color: #fff;
					margin-left: 16rpx;
					//text-decoration: underline;
					background-color: #F5533C;
					padding: 8rpx 15rpx;
					border-radius: 30rpx;
				}
				
				.btn {
					width: 100%;
					//margin-bottom: 30rpx;
					background-color: #fff;
					border-radius: 30rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					padding: 10rpx 15rpx;
					font-size: 24upx;
					font-family: PingFang SC;
					color: #FF5C03;
				}
				.left{
					height: 100%;
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					justify-content: space-around;
					.label{
						font-size: 28upx;
						font-family: PingFang SC;
						color: #fff;
					}
					.integral{
						font-weight: bold;
						font-size: 40upx;
						font-family: PingFang SC;
						color: #fff;
					}
				}
				.btn-box{
					height: 100%;
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					justify-content: space-around;
				}
			}
		
			.tabs{
				height: 88rpx;
				background-color: #F5F7FA;
				//z-index: 999;
			}
		}
		.inner-box{
			z-index: 9;
			position: relative;
			// padding: 40upx 30upx;
			.topbox{
				padding: 30rpx;
				margin: 0 30rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				background: #FFFFFF;
				border-radius: 24rpx 24rpx 0rpx 0rpx;
				.left{
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					text{
						margin-left: 12rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 26rpx;
						color: #626468;
						text-align: left;
					}
					.num{
						font-family: Roboto, Roboto;
						font-weight: 500;
						font-size: 64rpx;
						color: #222426;
						text-align: left;
					}
				}
				.guize{
					width: 220rpx;
					height: 72rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background: linear-gradient( 90deg, #F8551F 0%, #FF9501 100%);
					border-radius: 36rpx 36rpx 36rpx 36rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #FFFFFF;
				}
			}
			.bottom-box{
				
				background: #FCF8F5;
				border-radius: 24rpx 24rpx 24rpx 24rpx;
				height: 150rpx;
				    z-index: -1;
				    position: relative;
				    margin-top: -50rpx;
					.ttbg{
						width: 100%;
						height: 50rpx;
						background: #FE9B59;
						border-radius: 24rpx 24rpx 24rpx 24rpx;
					}
				.t-left{
					flex:1;
					display: flex;
					align-items: center;
					justify-content: center;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #222222;
					border-right: 2rpx solid #FF5C03;
					image{
						width: 40rpx;
						height: 40rpx;
						margin-right: 12rpx;
					}
				}
				.t-right{
					flex:1;
					display: flex;
					align-items: center;
					justify-content: center;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #222222;
					image{
						width: 32rpx;
						height: 32rpx;
						margin-right: 12rpx;
					}
				}
			}
		
		}
		.integral-box{
			padding: 0 30rpx 30rpx 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: #FF5C03;
							border-radius: 30rpx;
							font-size: 20upx;
							font-family: PingFang SC;
							color: #ffffff;
						}
						.count{
							font-size: 24upx;
							font-family: PingFang SC;
							color: #333333;
						}
						
					}
				}
			}
		}
	}
}
.search-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-top: 22rpx;
		.search {
			padding: 0;
			flex: 1;
			image {
				width: 28rpx;
				height: 28rpx;
				padding-left: 16rpx;
			}
		}
	} 

</style>