<template>
	<view class="content">
		<image class="bg" src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/backbg.png"></image>
		<view class="header-nav"
			:style="{ height: `calc(88rpx + ${statusBarHeight}px)`, paddingTop: statusBarHeight + 'px' }">
			<view class="arrow-left-warning" @click="goBack()">
				<u-icon name="arrow-left" size='22' color="#222" bold></u-icon>
			</view>
			<view class="header-title">积分商城</view>
		</view>
		<view class="cont-box">
			<view class="top-box" :style="{ paddingTop: statusBarHeight + 44 + 'px' }">
				<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_points/integralLogsList')">
							<image
								src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/integralLogs.png">
							</image>
							获得记录
						</view>
						<view style="height: 40rpx;border: 1rpx solid #D8D8D8;"></view>
						<view class="btn" style="margin-bottom: 0;"
							@click="loginNavTo('/pages_points/integralOrderList')">
							<image
								src="https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/shop/images/exchange.png">
							</image>
							兑换订单
						</view>
					</view>
					<view class="search-cont">
						<view class="inner">
							<image class="icon-search" src="@/static/images/icon-search.png" mode=""></image>
							<input type="text" @confirm="goSearch" :value="keyWord" placeholder="搜索商品"
								placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
						</view>
					</view>
				</view>
				<!-- <view class="navbox">
					<view class="navbox-item" @click="goCart()">
						<view class="box">
							<u-badge type="error" max="99" :value="cartNum" bgColor="#FF233C"></u-badge>
						</view>
						<image src="/static/images/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="#F4F6F7"
						inactiveStyle="font-size: 40rpx;color: #222"
						activeStyle="font-size: 40rpx;font-weight:bold;color: #FF233C;" @change="tabChange">
					</u-tabs>
				</view>
			</view>
			<mescroll-body top="690rpx" bottom="0" ref="mescrollRef" @init="mescrollInit" @down="downCallback"
				@up="upCallback" :down="downOption" :up="upOption">
				<view class="integral-box">
					<view class="item" @click="navTo('/pages_points/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 { getStoreDictByKey } 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)
			},
			keyWord: '',
			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: 0,
			cartNum: 0,
			statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
		}
	},
	onLoad() {
		this.getDictByKey("sys_integral_goods_type");
		if (this.$isLogin()) {
			this.getUserInfo();
		}
	},
	onReady() {
		this.getTop()
	},
	onShow() {
		if (this.$isLogin()) {
			this.getcartNum()
		}
	},
	methods: {
		goBack() {
			uni.navigateBack();
		},
		goSearch(e) {
			this.keyWord = e.detail.value;
			this.mescroll.resetUpScroll();
		},
		goCart() {
			if (this.$isLogin()) {
				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) {
			if (this.$isLogin()) {
				uni.navigateTo({
					url: '/pages_points/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) {
			if (this.$isLogin()) {
				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;
			getStoreDictByKey(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,
				keyword: this.keyWord
			};
			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: #F4F6F7;
}
</style>
<style scoped lang="scss">
@mixin u-flex($flexD, $alignI, $justifyC) {
	display: flex;
	flex-direction: $flexD;
	align-items: $alignI;
	justify-content: $justifyC;
}

.bg {
	width: 100%;
	height: 460rpx;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 9;
}

.header-nav {
	height: 88rpx;
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	//background-color: #fff;
	box-sizing: border-box;
	position: fixed;
	width: 100%;
	z-index: 9999;

	.header-title {
		text-align: center;
		margin-right: 0;
		//padding: 0 10rpx 0 100rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 32rpx;
		color: #000;
		box-sizing: border-box;
	}
}

.arrow-left-warning {
	position: absolute;
	left: 24rpx;
	height: 88rpx;
	overflow: hidden;
	color: #888;
	font-size: 24rpx;
	z-index: 999;
	@include u-flex(column, center, center);

	image {
		flex-shrink: 0;
		height: 36rpx;
		width: 36rpx;
	}
}

.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;
	}
}

.search-cont {
	padding: 24rpx;
	width: 100%;
	background-color: #F4F6F7;
	display: flex;
	align-items: center;
	justify-content: space-between;

	.inner {
		box-sizing: border-box;
		width: 100%;
		height: 76rpx;
		background: #FFFFFF;
		border-radius: 38rpx;
		display: flex;
		align-items: center;

		//padding: 0 20rpx;
		.icon-search {
			width: 44rpx;
			height: 44rpx;
			margin-right: 12rpx;
		}

		input {
			height: 76rpx;
			line-height: 76rpx;
			font-size: 32rpx;
			flex: 1;
		}

	}

	.icon-search {
		margin-left: 20rpx;
		width: 44rpx;
		height: 44rpx;

		image {
			width: 44rpx;
			height: 44rpx;
		}

	}
}

.content {
	height: 100%;

	.cont-box {
		.top-box {
			z-index: 999;
			// padding: 24rpx 24rpx 0 24rpx;
			width: 100%;
			position: fixed;
			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;
				flex-direction: column;

				// padding: 30rpx;
				//margin-bottom: 24rpx;
				.left {
					padding: 20rpx 40rpx;

					.label {
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 40rpx;
						color: #FFFFFF;
						line-height: 56rpx;
					}

					.integral {
						margin-top: 20rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 64rpx;
						color: #FFFFFF;
						line-height: 90rpx;
					}
				}

				.btn-box {
					margin-top: 30rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: space-between;
					background-color: #fff;
					border-radius: 24rpx;
					width: calc(100vw - 48rpx);
					margin-left: 24rpx;
					padding: 30rpx 50rpx;

					.btn {
						//margin-bottom: 30rpx;
						//background-color: #fff;
						//border-radius: 30rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						//padding: 30rpx 50rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 32rpx;
						color: #000000;
						line-height: 44rpx;

						image {
							width: 100rpx;
							height: 100rpx;
							margin-right: 20rpx;
						}
					}
				}
			}

			.tabs {
				height: 88rpx;
				background: #F4F6F7;
			}
		}

		.integral-box {
			margin-top: 40rpx;
			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: #FF233C;
							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: #FF233C;
						}

						.count {
							font-size: 24upx;
							font-family: PingFang SC;
							color: #333333;
						}

					}
				}
			}
		}
	}
}
</style>
