<template>
	<view class="content" >
		<view class="top-content">
			<!-- 这里是状态栏 -->
			<!-- #ifdef APP -->
			<view style="height: 80rpx;"></view>
			<view class="top-title">健康产品</view>
			<!-- #endif -->
			<!-- #ifndef APP -->
			<view class="status_bar" :style="{height: statusBarHeight}"></view>
				<view class="top-title">健康产品 </view>
			<!-- #endif -->
			<!-- 搜索框 -->
			<view class="search-cont">
				<view class="inner">
					<image class="icon-search" src="../../static/images/search.png" mode=""></image>
					<input type="text"  confirm-type="搜索" @click="toSearch" placeholder="输入商品名称"
						placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
				</view>
			</view>
		</view>
		<view :style="{height: divHeight}" class="medic-box">
			<view class="cate-list" >
				<view v-for="(item,index) in cates" :key="item.cateId" :class="cateSelect == item.cateId?'item active':'item'"
					@click="choseCate(item)">{{item.cateName }}</view>
			</view>
			<view class="medic">
				<!-- 轮播图 -->
				<view class="banner-box">
					<swiper class="swiper" :indicator-dots="true" :circular="true" :autoplay="true" :interval="3000"
						:duration="1000" indicator-color="rgba(255, 255, 255, 0.6)" indicator-active-color="#ffffff">
						<swiper-item class="swiper-item" v-for="(item,index) in advs" :key="index"
							@click="handleAdvClick(item)">
							<image :src="item.imageUrl" mode=""></image>
						</swiper-item>
					</swiper>
				</view>
				<!-- 商品列表 -->
				<view class="medic-list">
					<!-- <view class="item" v-for="(item,index) in subCates" :key="index">
							<view class="title">{{item.cateName}}</view> -->
					<scroll-view scroll-y="true" class="hb" :refresher-enabled="isEnabled" :refresher-triggered="triggered"
						refresher-background="rgba(0,0,0,0)" @refresherrefresh="pullDownRefresh"
						@refresherrestore="triggered = false" :upper-threshold="100" :lower-threshold="100"
						@refresherabort="triggered = false" @scrolltolower="reachBottom">
					<view class="inner-list">
						<view class="definite" v-for="(subItem,index) in subCates" @click="showProductList(subItem)">
							<view class="img-box">
								<image :src="subItem.image" mode="aspectFit"></image>
							</view>
							<view class="name ellipsis">{{subItem.productName}}</view>
						</view>
					</view>
					<u-loadmore :status="status" />
					</scroll-view>
					<!-- <view v-if="subCates.length==0" class="center mt20 column">
						<image src="../../static/images/no_data.png" 
						style="width: 200rpx;height: 150rpx;"></image>
						<text class="fs24">暂无商品</text>
						</view> -->
					<!-- 	</view> -->
				</view>
			</view>
		</view>
		<tabbar :actindex="3"></tabbar>
	</view>
</template>

<script>
	import {
		getProductCate,
		getProducts
	} from '@/api/product'
	import {
		getAdv
	} from '@/api/adv'
	export default {
		data() {
			return {
				divHeight: '0px',
				allCates: [],
				cates: [],
				subCates: [],
				// 状态栏的高度
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				// 选中商品分类
				cateSelect: 183,
				// 轮播图
				advs: [],
				theme:"beiliyou",
				defaultOrder:'desc',
				page: {
					num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					size: 10 // 每页数据的数量,默认10
				},
				titname:'',
				isEnabled: true,
				triggered: false,
				status:'loadmore'
			};
		},
		onLoad() {
			uni.showShareMenu({
				withShareTicket: true,
				//小程序的原生菜单中显示分享按钮，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				menus: ["shareAppMessage", "shareTimeline"] //不设置默认发送给朋友
			})
			this.getProductCate();
			// setTimeout(()=>{
				this.cateSelect=this.allCates[0].cateId
				this.getproduct()
			// },100)
			
		},
		onShow() {
			
			var that = this;
			setTimeout(function() {
				let info = uni.createSelectorQuery().select(".top-content");
				info.boundingClientRect(function(data) { //data - 各种参数
					console.log(data.height) // 获取元素宽度
					// console.log(uni.upx2px(10)) 
					that.divHeight = "calc(100% - " + data.height + "px)"
				}).exec()
			}, 500);
			this.getAdv();
		},
		methods: {
			// divHeight(){
			//    return 'height:calc(100% - ${this.top}px);'
			// },
			pullDownRefresh(){
				// 下拉
				this.triggered = true; //下拉了状态为true
				setTimeout(() => {
					this.triggered = false;
					uni.stopPullDownRefresh()
					this.page.num= 1;
					this.getproduct('refresh') //触底  不穿执行else
					// 请求接口里面需要判断是不是最后一页   是最后一页 status赋值为‘loadmore’没有更多了
					// 请求接口
				}, 1000)
			},
			reachBottom(){
				//上拉
				// status这个是加载状态
				if (this.status === 'loadmore') {
					this.status = 'loading'
					uni.showNavigationBarLoading()
					setTimeout(() => {
						this.page.num++
						this.getproduct() //触底  不穿执行else
						uni.hideNavigationBarLoading()
					}, 1000);
				}
			},
			toSearch() {
				uni.navigateTo({
					url: '../home/productSearch'
				})
			},
			handleAdvClick(item) {
				console.log(item);
				if (item.showType == 1) {
					uni.setStorageSync('url', item.advUrl);
					uni.navigateTo({
						url: "../home/h5"
					})
				} else if (item.showType == 2) {
					uni.navigateTo({
						url: item.advUrl
					})
				} else if (item.showType == 3) {
					uni.setStorageSync('content', item.content);
					uni.navigateTo({
						url: "../home/content"
					})
				}

			},
			getAdv() {
				let data = {
					advType: 2
				};
				getAdv(data).then(
					res => {
						if (res.code == 200) {
							this.advs = res.data;
						}
					},
					rej => {}
				);
			},
			getProductCate() {
				let data = {};
				getProductCate(data).then(
					res => {
						if (res.code == 200) {
							this.allCates = res.data;
							this.cates = this.allCates.filter(function(item) {
								return item.pid == 0
							});
							console.log(111,this.allCates,)
							if (this.cates != null && this.cates.length > 0) {
								this.cateSelect = this.cates[0].cateId;
								this.getproduct()
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			// 商品分类选择
			choseCate(item) {
				this.subCates=[]
				this.cateSelect = item.cateId;
				console.log(item)
				this.titname=item.cateName
				// this.getSubCate()
				this.getproduct()
			},
			getproduct(type){
				var data = {
					cateId:this.cateSelect,
					defaultOrder:this.defaultOrder,
					page: this.page.num,
					pageSize: this.page.size,
					productName:  "",
					storeId: ""
				};
				getProducts(data).then(res=>{
					if(res.code==200){
						console.log(res)
						if (type == 'refresh') {
							this.subCates=res.data.list
						} else {
							// 加载更多 当前页和下一页合并
							this.subCates = [...this.subCates, ...res.data.list]
						}
						if (this.page.num >= res.data.prePage) {
							//是否有下一页
							this.status = 'nomore'
						} else {
							this.status = 'loadmore'
						}
					}
				})
			},
			getSubCate() {
				var that = this;
				// this.subCates = this.allCates.filter(function(item) {
					// let subList = that.allCates.filter(child => {
					// 	//返回每一项的子级数组
					// 	return child.pid === item.cateId
					// });
					// subList.length > 0 ? item.children = subList : [];
				// 	return item.pid == that.cateSelect
				// });
				// this.getProductCate()
				console.log(this.subCates);
			},
			// 查看商品详情
			showProductList(item) {
				uni.navigateTo({
					url: './productList?cateId=' + item.cateId + "&pid=" + 0
				})
			},
			goSearch(e) {
				if (e.detail.value != null && e.detail.value != "") {
					this.utils.addHisSearch(e.detail.value);
				}
				console.log(e)
				// uni.navigateTo({
				// 	url: '../home/productList?searchValue=' + e.detail.value+
				// })
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		background-color: #fff;
	}

	.content {
		height: 100%;
		display: flex;
		flex-direction: column;

		.top-content {
			width: 100%;
			z-index: 10;

			.top-title {
				height: 88upx;
				line-height: 88upx;
				font-size: 42upx;
				font-family: Source Han Sans CN;
				font-weight: bold;
				color: #222222;
				padding-left: 41upx;
				background-color: #FFFFFF;
			}

			.search-cont {
				padding: 16upx 30upx;
				background-color: #FFFFFF;

				.inner {
					box-sizing: border-box;
					width: 100%;
					height: 72upx;
					background: #F7F7F7;
					border-radius: 36upx;
					display: flex;
					align-items: center;
					padding: 0 30upx;

					.icon-search {
						width: 28upx;
						height: 28upx;
						margin-right: 20upx;
					}

					input {
						height: 60upx;
						line-height: 60upx;
						flex: 1;
					}
				}
			}
		}

		.medic-box {
			display: flex;

			.cate-list {
				box-sizing: border-box;
				width: 200upx;
				background: #F2F5F9;
				display: flex;
				flex-direction: column;
				padding: 20upx 0;
				overflow-y: scroll;

				.item {
					height: 100upx;
					line-height: 100upx;
					padding-left: 30upx;
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
					position: relative;

					&.active {
						color: #018C39;

						&::after {
							content: "";
							width: 8upx;
							height: 50upx;
							background: #018C39;
							position: absolute;
							top: 25upx;
							left: 0;
						}
					}
				}
			}

			.medic {
				box-sizing: border-box;
				width: calc(100% - 200upx);
				height: 100%;
				padding: 0 30upx;

				.banner-box {
					margin-top: 30rpx;
					width: 100%;
					height: 160upx;
					border-radius: 10upx;
					overflow: hidden;

					.swiper,
					.swiper-item,
					.swiper-item image {
						width: 100%;
						height: 100%;
					}
				}

				.medic-list {
					box-sizing: border-box;
					padding: 30upx 0;
					overflow-y: auto;
					height: calc(100% - 220upx);
					position: relative;
					// .item{
					// 	.title{
					// 		font-size: 28upx;
					// 		font-family: PingFang SC;
					// 		font-weight: bold;
					// 		color: #333333;
					// 		padding-top: 20upx;
					// 		margin-bottom: 30upx;
					// 	}

					// }
					.inner-list {
						display: flex;
						flex-wrap: wrap;

						.definite {
							width: calc(33% - 20upx);
							margin-right: 30upx;
							margin-bottom: 30upx;

							.img-box {
								width: 100%;
								height: 144upx;
								background: #F5F5F5;
								border-radius: 8upx;
								overflow: hidden;
								display: flex;
								align-items: center;

								image {
									max-width: 100%;
								}
							}

							.name {
								width: 100%;
								margin-top: 20upx;
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #666666;
								text-align: center;
							}

							&:nth-child(3n) {
								margin-right: 0;
							}
						}
					}
				}
			}
		}

	}
</style>