<template>
	<view class="content">
		<view class="dddd" style="height: 10rpx;"></view>
		<view class="hotgoods">
			<view class="hotgoods-item" v-for="bditem in list" :key="bditem.id"
				@click="navto('/pages/product/product?id=' + bditem.id + '&isjf=1')" style="height: 520rpx;">
				<view class="image-wrapper">
					<image class="image" :src="bditem.image" mode="scaleToFill"></image>
				</view>
				<view class="flex" style="flex-direction: column;justify-content: space-between;align-items: flex-start;height: 170rpx;">
					<view class="title clamp2">{{bditem.store_name}}</view>
					<view class="hot-price">
						<view class="price">
							<text class="font-size-sm"></text>
							<text>{{ bditem.integral * 1 }}积分</text>
							<!-- <text class="give-jf" v-if="bditem.give_consumption*1">赠{{bditem.give_consumption*1}}消费余额</text> -->
							<text class="ot-pirce">￥{{bditem.ot_price}}</text>
						</view>
						
					</view>
				</view>
		
			</view>
		</view>
		<uni-load-more :status="loadingType"></uni-load-more>
	</view>
</template>
<script>
	import { getCategoryList,getProducts } from '@/api/product.js';
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 9,
				flist: [],
				height: '',
				currentIndex: 0,
				list: [],
				page: 1,
				limit: 20,
				loadingType: 'more',
				loaded: false,
			};
		},
		onLoad() {
			this.loadData();
		},
		// 监听导航栏输入框点击事件
		onNavigationBarSearchInputClicked(e) {
			uni.navigateTo({
				url: '/pages/product/search'
			});
		},
		onReady(res) {
			
		},
		onReachBottom() {
			this.getProducts()
		},
		methods: {
			leftClick(index) {
				this.currentIndex = index
				this.getProducts('reload')
			},
			getProducts(type) {
				
				if(type == 'reload') {
					this.list = []
					this.page = 1
					this.loadingType = 'more'
					this.loaded = false
				}
				if(this.loadingType == 'loading' || this.loadingType == 'noMore') {
					return
				}
				this.loadingType = 'loading'
				getProducts({
					// cid: index,
					page: this.page,
					limit: this.limit,
					mall_type: 3
				}).then(res => {
					this.list = this.list.concat(res.data)
					this.page++
					if(this.limit == res.data.length) {
						this.loadingType = 'more'
					}else {
						this.loadingType = 'noMore'
					}
					this.loaded = true
				})
			},
			// 载入数据
			async loadData() {
				let obj = this;
				obj.getProducts()
				// getCategoryList({})
				// 	.then(({ data }) => {
				// 		obj.flist = data.map(function(s) {
				// 			return s;
				// 		});
				// 		obj.getProducts()
				// 	})
				// 	.catch(err => {
				// 		console.log(err);
				// 	});
			},
			//一级分类点击
			tabtap(item) {
				console.log(item);
				// 判断有没有初始化页面高度对象数据
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				// 获取当前点击的id
				this.currentId = item.id;
				console.log(item.top);
				this.tabScrollTop = item.top;
				console.log(this.tabScrollTop);
			},
			//右侧栏滚动
			asideScroll(e) {
				// 判断有没有初始化页面高度对象数据
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let box = 0; //列表包裹框高度初始化
				let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
				// 查询当前页面对象
				let view = uni.createSelectorQuery().select('.content');
				view.fields(
					{
						id: true,
						dataset: true,
						rect: true,
						size: true,
						scrollOffset: true
					},
					function(e) {
						// 保存包裹框高度
						box = e.height;
					}
				).exec();
				// 获取所有距离顶部大于滚轮距离页面高度的所有分类
				let tabs = this.flist.filter(item =>( item.top-10) <= scrollTop).reverse();
				if (tabs.length > 0) {
					// 判断是否已经到达滚轮底部
					if (box + scrollTop + bottom >= e.detail.scrollHeight) {
						this.currentId = this.flist[this.flist.length - 1].id;
					} else {
						this.currentId = tabs[0].id;
					}
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				this.flist.forEach(item => {
					let view = uni.createSelectorQuery().select('#main-' + item.id);
					view.fields(
						{
							size: true
						},
						data => {
							item.top = h;
							h += data.height;
							item.bottom = h;
						}
					).exec();
				});
				this.sizeCalcState = true;
			},
			navToList(sid, tid) {
				// 点击导航跳转到详细页面
				uni.navigateTo({
					url: '/pages/product/list?fid='+this.currentId+'&sid='+sid+'&tid='+tid
				});
			},
			navto(url) {
				uni.navigateTo({
					url
				})
			}
		}
	};
</script>
<style lang="scss">
	.cate-wrap {
		width:750rpx;
		.left-wrap {
			width: 180rpx;
			flex-shrink: 0;
			background-color: #f2f2f2;
			.left-item {
				width: 180rpx;
				height: 100rpx;
				line-height: 100rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #666666;
				text-align: center;
			}
			.action-item {
				background-color: #fff;
				font-weight: bold;
				position: relative;
				&::before {
					content: '';
					position: absolute;
					top: 0;
					left: 0;
					width: 3rpx;
					height: 100rpx;
					background: #FF7144;
				}
			}
		}
		.right-wrap {
			flex-wrap: 1;
			background-color: #fff;
			.right-item {
				width: 520rpx;
				height: 240rpx;
				padding: 30rpx 0;
				margin: 0 auto ;
				border-bottom: 1px solid #eee;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				.good-img {
					width: 180rpx;
					height: 180rpx;
					border-radius: 20rpx;
				}
				.good-info {
					flex-direction: column;
					justify-content: space-between;
					align-items: flex-start;
					height: 100%;
					padding-left: 25rpx;
					.good-name {
						font-size: 30rpx;
						font-weight: bold;
						color: #333333;
					}
					.good-price {
						font-size: 34rpx;
						font-weight: bold;
						color: #FF4C4C;
					}
				}
			}
		}
	}
	.hotgoods {
		margin-top: 20rpx;
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		padding: 0 20rpx 30rpx;
		justify-content: space-between;
	
		.hotgoods-item {
			width: 345rpx;
			background-color: #ffffff;
			border-radius: 12rpx;
			box-shadow: 0 0 15rpx rgba(0, 0, 0, 0.2);
			margin-bottom: 15rpx;
	
			.image-wrapper {
				width: 345rpx;
				height: 345rpx;
				border-radius: 3px;
				overflow: hidden;
				position: relative;
	
				.image-bg {
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					width: 100%;
					height: 100%;
					opacity: 1;
					border-radius: 12rpx 12rpx 0 0;
					z-index: 2;
				}
	
				.image {
					width: 100%;
					height: 100%;
					opacity: 1;
					border-radius: 12rpx 12rpx 0 0;
				}
			}
	
			.title {
				margin-top: 24rpx;
				padding: 0 20rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: #333333;
			}
	
			.hot-price {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				// padding: 14rpx 0 30rpx;
	
				.hotPrice-box {
					padding: 2rpx 6rpx;
	
					background: linear-gradient(90deg, #c79a4c, #f9df7f);
					border-radius: 5rpx;
					text-align: center;
					line-height: 28rpx;
					font-size: 20rpx;
					font-family: Source Han Sans CN;
					font-weight: 400;
					color: #ffffff;
				}
	
				.price {
					margin-left: 10rpx;
					font-size: 36rpx;
					color: #ff0000;
					font-weight: 500;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					.jf {
						font-size: 20rpx;
					}
					.give-jf {
						display: inline-block;
						padding: 8rpx;
						
						background: linear-gradient(90deg, #FF834D, #FF2600);
						border-radius: 12rpx 0px 12rpx 0px;
						
						font-size: 22rpx;
						font-weight: 500;
						color: #FFFFFF;
						margin-left: 22rpx;
					}
					.ot-pirce {
						margin-left:10rpx;
						font-size: 26rpx;
						font-weight: 500;
						text-decoration: line-through;
						color: #999999;
						align-self: flex-end;
					}
					
				}
	
				.yuanPrice {
					margin-left: 10rpx;
					font-size: 20rpx;
					font-family: PingFang SC;
					font-weight: 500;
					text-decoration: line-through;
					color: #999999;
				}
	
				.cart-icon {
					image {
						width: 44rpx;
						height: 44rpx;
					}
				}
			}
		}
	}
	.ot-pirce {
		margin-left: 7rpx;
		font-size: 26rpx;
		font-weight: 500;
		text-decoration: line-through;
		color: #999999;
		align-self: flex-end;
	}
</style>