<template>
	<view class="content">
		<view class="top-content">
			<view class="status_bar" :style="{height: statusBarHeight}"></view>
			<!-- 这里是状态栏 -->
			<!-- <view class="top-title">健康商城</view> -->
			<!-- 搜索框 -->
			<view class="search-cont" :style="{width:`calc(100vw - 80rpx - ${menuRight} - ${menuWidth})`}">
				<view class="inner">
					<image class="icon-search" src="../../static/images/search.png" mode=""></image>
					<input type="text" disabled 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="index" :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="inner-list">
					<!-- 	<view class="definite"v-for="(subItem,index) in subCates"   @click="showProductList(subItem)">
								<view class="img-box">
									<image :src="subItem.pic" mode="aspectFit"></image>
								</view>
								<view class="name ellipsis">{{subItem.cateName}}</view>
							</view> -->
					<view class="definite" v-for="(subItem,index) in subCates"
						@click="showProductList(subItem, index)">
						<view class="name ellipsis" :class="{ 'select-name': activeIndex === index }">
							{{subItem.cateName}}
						</view>
					</view>
				</view>
				<view class="line"></view>
				<!-- 药品列表 -->
				<view class="medic-list">
					<!-- <view class="item" v-for="(item,index) in subCates" :key="index">
							<view class="title">{{item.cateName}}</view> -->
					
					<!-- <view class="product-list">
						<text class="product-title"> {{ activeIndex >= 0 ? subCates[activeIndex].cateName : '' }}</text>
						</view> -->
				<!-- 数据列表 -->
				<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
					<view class="medic-list2">
						<text class="product-title"> {{ activeIndex >= 0 && subCates.length>0?subCates[activeIndex].cateName : '' }}</text>
						<view v-for="(item,index) in dataList" :key="index" class="item" @click="showDetail(item)">
							<view class="img-box">
								<image :src="item.image" mode="aspectFit"></image>
								<view class="cf-box" v-if="item.productType==2">
									<view class="title">处方药</view>
									<view class="subTitle">请在医师指导下使用</view>
								</view>
							</view>
							<view class="info-box">
								<view class="title ellipsis2">{{item.productName}}</view>
								<view class="intro">
									<view class="intro-zk"><text>9.5折</text></view>
									<view class="intro-zk"><text>限购1份</text></view>
								</view>
								<view class="price-box">
									<view class="now">
										<!-- <text class="unit">¥</text> -->
										<text class="num">{{item.price.toFixed(2)}}</text>
									</view>
									<view class="old">¥{{item.otPrice.toFixed(2)}}</view>
								</view>
							</view>
						</view>
					</view>
				</mescroll-body>
					<!-- 	</view> -->
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import {
		getProductCate,getProductCateByPid,getProducts
	} from '@/api/product'
	import {
		getAdv
	} from '@/api/adv'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				activeIndex: 0,
				divHeight: '0px',
				allCates: [],
				cates: [],
				subCates: [],
				// 右侧的胶囊距离右侧屏幕距离-px
				menuRight: uni.getStorageSync('menuInfo').menuRight,
				// 右侧的胶囊宽度-px
				menuWidth: uni.getStorageSync('menuInfo').menuWidth,
				// 状态栏的高度
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				// 选中药品分类
				cateSelect: 0,
				// 轮播图
				advs: [],
				dataList:[],
				cateId:null,
				pid:null,
				defaultOrder:'desc',
				storeId: "",
				//上拉加载的配置
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						icon:'https://user.test.ylrztop.com/images/empty_icon.png',
						tip: '暂无数据',
						
					},
					textNoMore:"已经到底了",
				}
			};
		},
		onLoad() {
			uni.showShareMenu({
				withShareTicket: true,
				//小程序的原生菜单中显示分享按钮，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				menus: ["shareAppMessage", "shareTimeline"] //不设置默认发送给朋友
			})
			
		},
		onShow() {
			this.getProductCate();
			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);'
			// },
			toSearch() {
				uni.navigateTo({
					url: '../home/productSearch'
				})
			},
			handleAdvClick(item) {
				console.log(item);
				if (item.showType == 1) {
					uni.setStorageSync('url', item.advUrl);
					uni.navigateTo({
						url: "../home/web"
					})
				} 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
							});
							if (this.cates != null && this.cates.length > 0) {
								this.cateSelect = this.cates[0].cateId ;
								this.getSubCate();
							    // this.mescroll.resetUpScroll()
								
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			// 药品分类选择
			choseCate(item) {
				this.activeIndex = 0
				this.dataList=[]
				this.cateSelect = item.cateId;
				this.getSubCate()
	            
                
			},
			getSubCate() {
				
				var that = this;
				this.subCates = this.allCates.filter(item=>item.pid == this.cateSelect);
					// let subList = that.allCates.filter(child => {
					// 	//返回每一项的子级数组
					// 	return child.pid === item.cateId
					// });
					// subList.length > 0 ? item.children = subList : [];
					
				if (this.subCates != null && this.subCates.length > 0) {
					this.cateId= this.subCates[0].cateId;
					this.mescroll.resetUpScroll()
				}else{
					this.cateId=0
					this.mescroll.resetUpScroll()
				}
				
			},
			// 查看药品详情
			showProductList(item, index) {
				// console.log("item>>>>>", item)
				this.activeIndex = index;
                this.cateId = item.cateId;
                this.mescroll.resetUpScroll()
				// uni.navigateTo({
				// 	url: './productList?cateId='+item.cateId+"&pid="+item.pid
				// })
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				this.mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					cateId:this.cateId,
					defaultOrder:this.defaultOrder,
					page: page.num,
					pageSize: page.size,
					productName: this.productName || "",
					storeId: this.storeId
				};
				if(this.cateId!=null){
					this.getProducts(page,data)
				}
				
			},
			//查询商品列表
			getProducts(page,data) {
				var that = this;
				getProducts(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();
					}
				});
			},
			// 查看药品详情
			showDetail(item) {
				if(this.from == 'company') {
					uni.navigateTo({
						url: '/pages_company/order/productDetails?productId='+item.productId +`${this.storeId ? '&storeId='+this.storeId : ''}`
					})
				} else {
					uni.navigateTo({
						url: './productDetails?productId='+item.productId +`${this.storeId ? '&storeId='+this.storeId : ''}`
					})
				}
			},
			goSearch(e) {
				if (e.detail.value != null && e.detail.value != "") {
					this.utils.addHisSearch(e.detail.value);
				}
				uni.navigateTo({
					url: '../home/productList?searchValue=' + e.detail.value
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		background-color: #fff;
	}
    .mescroll-body{
		min-height: calc(100% - 330upx) !important;
	}
	.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;
                margin-top: 30rpx;
				.item {
					height: 100upx;
					line-height: 100upx;
					padding-left: 30upx;
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
					position: relative;
					 // overflow: hidden;
					&.active {
						background: #FFFFFF;
						font-weight: 600;
						// box-shadow: 12px 0 12px 0 #ffffff, -12px 0 12px 0 #ffffff;
						// border-radius: 12px 12px 0 0;
                        
						&::after {
							content: "";
							width: 6upx;
							height: 28upx;
							background: #008FD3;
							position: absolute;
							top: 50%;
							left: 0;
							transform: translateY(-50%);
							border-radius: 0upx 6upx 6upx 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%;
					}
				}
	.inner-list {
						display: flex;
						flex-wrap: wrap;
                        padding-top: 30upx;
						.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;
								padding: 10rpx 16rpx;
							}

							.select-name {
								background: #EFF3F7;
								color: #222426;
								border-radius:10upx ;
							}

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

					.line {
						width: 100%;
						height: 0rpx;
						border-top: 1rpx solid #ECECEC;
					}

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

					// }
				
					.product-list {
						padding: 32rpx 0 0;
						.product-title {
							font-weight: 500;
							font-size: 28rpx;
							color: #222426;
						}
					}
			.medic-list2{
				// padding: 20upx;
				.product-title{
					font-size: 28upx;
					display: block;
					// padding-top: 30upx;
				}
				.item{
					box-sizing: border-box;
					min-height: 200upx;
					background: #FFFFFF;
					// border: 4upx solid #FFFFFF;
					border-radius: 20upx;
					// margin-bottom: 20upx;
					// padding: 40upx 0upx;
					padding-top: 30upx;
					display: flex;
					
					.img-box{
						width: 200upx;
						height: 200upx;
						margin-right: 30upx;
						position: relative;
						border-radius: 20upx;
						    overflow: hidden;
						image{
							width: 100%;
							height: 100%;
						}
						
					}
					
					.info-box{
						width: calc(100% - 210upx);
						display: grid;
						    justify-items: left;
						.title{
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
							line-height: 40upx;
							height: 80upx;
						}
						.intro{
							// font-size: 26upx;
							// font-family: PingFang SC;
							// font-weight: 500;
							// color: #999999;
							// line-height: 1;
							// margin-top: 26upx;
							display: flex;
							.intro-zk{
								border: 1px solid #FF4B33;
								padding:0 8rpx ;
								border-radius: 8rpx;
								margin-right: 10rpx;
								line-height: 5rpx;
								display: flex;
								text{
									font-size:24rpx;
									color:#FF4B33 ;
									display: flex;
									    align-items: center;
								}
							}
						}
					 .price-box{
					 	display: flex;
					 	align-items: flex-end;
					 	.now{
					 		display: flex;
					 		align-items: flex-end;
					 		margin-right: 20upx;
					 		.num{
					 			font-size: 36upx;
					 			font-family: PingFang SC;
					 			font-weight: bold;
					 			color: #FF5030;
					 			line-height: 1;
					 		}
					 	}
					 	.old{
					 		font-size: 24upx;
					 		font-family: PingFang SC;
					 		// font-weight: 500;
					 		text-decoration: line-through;
					 		color: #BBBBBB;
					 		line-height: 1.1;
					 	}
					 }
						.prce-num{
							display: flex;
							align-items: center;
							justify-content: space-between;
							margin-top: 30upx;
							.price{
								display: flex;
								align-items: flex-end;
								.unit{
									font-size: 24upx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #FF6633;
									line-height: 1.2;
									margin-right: 4upx;
								}
								.num{
									font-size: 36upx;
									font-family: PingFang SC;
									font-weight: bold;
									color: #FF6633;
									line-height: 1;
								}
							}
							.cart-img{
								.sale{
									font-size: 20upx;
									font-family: PingFang SC;
									color: #999999;
								}
								 
							}
						}
					}
				}
			}
				}
			}
		}

	}
</style>