<template>
	<view class="content">
		<view class="top-content">
			<view class="status_bar" :style="{height: statusBarHeight}"></view>
			<!-- 这里是状态栏 -->
			<view class="top-title">
				<u-icon name="arrow-left" color="#fff" size="24" @click="navback()"></u-icon>
				<view class="search-cont">
					<view class="inner">
						<image class="icon-search" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/icon_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 class="storebox x-bc">
				<view class="x-f">
					<image class="logo" :src="storeInfo.logoUrl" mode="aspectFill"></image>
					<view class="storebox-r" @click="goStoreDetail">
						<view class="storename ellipsis2">{{storeInfo.storeName}}</view>
						<view class="storedesc">24小时营业  月售{{storeInfo.salesCount|| 0}}</view>
					</view>
				</view>
				<view class="storebox-btn" @click="goStoreDetail">详情</view>
			</view>
			<view class="top-fixed x-ac" style="background-color: #fff;">
				 <view style="width: 50%;">
					 <u-tabs
					  :scrollable="false"
					  :list="tabs"  
					  lineColor="#2583EB"
					  :current="current"
					   @change="tabChange">
					 </u-tabs>
				 </view>
			</view>
		</view>
		<view  :style="{height: divHeight}">
			<view v-show="current==0" style="width: 100%;">
				<tuiStoreProduct ref="tuiStoreProduct" :scrollHeight="divHeight" :storeId="storeId"></tuiStoreProduct>
			</view>
			<view v-if="current==1" style="height: 100%" 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="medic-list">
							<!-- <view class="item" v-for="(item,index) in subCates" :key="index">
								<view class="title">{{item.cateName}}</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>
						<!-- 	</view> -->
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import tuiStoreProduct from './components/tuiStoreProduct.vue'
	import qualifications from './components/qualifications.vue'
	import {getProductCate,storeDetail} from '@/api/index.js'
	// import {getAdv} from '@/api/adv'
	export default {
		components: {
			tuiStoreProduct,
			qualifications
		},
		data() {
			return {
				divHeight:'0px',
				allCates:[],
				cates:[],
				subCates:[],
				// 状态栏的高度
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				// 选中药品分类
				cateSelect: 0,
				// 轮播图
				advs: [],
				tabs:[
					{
						id:1,
						name:'推荐'
					},
					{
						id:2,
						name:'分类'
					}
				],
				current:0,
				storeInfo: {},
				storeId:''
			};
		},
		onLoad(option) {
			this.storeId = option.storeId
			uni.showShareMenu({
				withShareTicket:true,
				//小程序的原生菜单中显示分享按钮，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				menus:["shareAppMessage","shareTimeline"] //不设置默认发送给朋友
			})
			this.getProductCate();
			this.getStoreInfo()
		},
		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);'
			// },
			getStoreInfo() {
				console.log(this.storeId)
				storeDetail(this.storeId).then(res=>{
					if(res.code==200) {
						this.storeInfo =res.data || {}
					}
				})
			},
			navback() {
				uni.navigateBack()
			},
			tabChange(item) {
				this.current=item.index
			},
			toSearch() {
				uni.navigateTo({
					url: '/pages_shopping/home/productSearch'
				})
			},
			handleAdvClick(item){
				console.log(item);
				if(item.showType==1){
					uni.setStorageSync('url',item.advUrl);
					uni.navigateTo({
						url:"/pages_shopping/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:"/pages_shopping/home/content"
					 })
				}
				
			},
			getAdv(){
				let data = {advType:2};
				getAdv(data).then(
					res => {
						if(res.code==200){
							this.advs=res.data;
						}
					},
					rej => {}
				);
			},
			getProductCate(){
				let data = {
					storeId: this.storeId
				};
				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()
							}
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			// 药品分类选择
			choseCate(item) {
				this.cateSelect = item.cateId;
				this.getSubCate()
				
			},
			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
				});
				 
				console.log(this.subCates);
			},
			// 查看药品详情
			showProductList(item) {
				uni.navigateTo({
					url: '/pages_shopping/productList?cateId='+item.cateId+"&pid="+item.pid
				})
			},
			goSearch(e) {
				if(e.detail.value!=null&&e.detail.value!=""){
					this.$addHisSearch(e.detail.value);
				}
				uni.navigateTo({
					url: '/pages_shopping/home/productList?searchValue=' + e.detail.value
				})
			},
			goStoreDetail() {
				uni.navigateTo({
					url: '/pages_store/storeDetail?storeId='+this.storeId
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
		background-color: #fff;
	}
	.storebox {
		padding: 26rpx 32rpx;
		font-family: PingFang SC, PingFang SC;
		color: #FFFFFF;
		.logo {
			width: 104rpx;
			height: 104rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin-right: 26rpx;
		}
		.storename {
			font-weight: 500;
			font-size: 32rpx;
		}
		.storedesc {
			margin-top: 12rpx;
			font-weight: 400;
			font-size: 22rpx;
		}
		.storebox-r {
			flex: 1;
			overflow: hidden;
		}
		.storebox-btn {
			flex-shrink: 0;
			padding: 10rpx 16rpx;
			background: rgba(46, 218, 212, 0.50);
			font-size: 28rpx;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
		}
	}
	.content{
		height: 100%;
		display: flex;
		flex-direction: column;
		.top-content{
			width: 100%;
			z-index: 10;
			background-repeat: no-repeat;
			background-image: url('https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/index_img/home_top_bg.png');
			background-size: 100% 100%;
			.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: transparent;
				display: flex;
				align-items: center;
			}
			.search-cont{
				padding: 6upx 30upx;
				.inner{
					box-sizing: border-box;
					width: 70%;
					height: 72upx;
					background: #FFFFFF;
					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: #2583EB;
						&::after{
							content: "";
							width: 8upx;
							height: 50upx;
							background: #2583EB;
							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>
