<template>
	<view>
		<view class="top-fixed">
			<view class="search-cont">
				<view class="inner">
					<image class="icon-search" src="https://kntobs.jnmyunl.com/shop/images/search.png" mode=""></image>
					<input type="text" :value="searchValue" placeholder="输入产品名称" placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
				</view>
				<view class="icon-search">
					<image @click="showChange(2)" v-if="showType==1"  src="https://kntobs.jnmyunl.com/shop/images/search1.png" mode=""></image>
					<image @click="showChange(1)" v-if="showType==2"  src="https://kntobs.jnmyunl.com/shop/images/search2.png" mode=""></image>
				</view>
			</view>
			<!-- tab切换 -->
			<view class="pub-tab-box">
				<view class="tab-inner">
					<view 
						v-for="(item,index) in cates" 
						:key="index"
						:class="cateId == item.cateId?'item active':'item'"
						@click="cateChange(item)">
						<view class="text">
							{{ item.cateName }}
							<image v-show="cateId == item.cateId" class="tab-bg" src="https://kntobs.jnmyunl.com/shop/images/tab_bg.png" mode=""></image>
						</view>
					</view>
					
				</view>
			</view>
		</view>
		<!-- 数据列表 --> 
		<mescroll-body  top="190upx" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="medic-list" v-if="showType==1">
				<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 ellipsis">{{item.productInfo}}</view>
						<view class="prce-num">
							<view class="price">
								<text class="unit">¥</text>
								<text class="num">{{item.price.toFixed(2)}} </text>
							</view>
							<view class="cart-img" @click="navgetTo('../shopping/cart')">
								<view class="sale">已售 {{item.sales}} {{item.unitName}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="goods-list" v-if="showType==2">
				<view class="item" v-for="(item,index) in dataList" :key="index" @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="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>
</template>

<script>
	import {getProductCateByPid,getProducts} from '@/api/product'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				showType:2,
				cates:[],
				cateId:null,
				defaultOrder:'desc',
				pid:null,
				//上拉加载的配置
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						icon:'https://kntobs.jnmyunl.com/shop/images/no_data.png',
						tip: '暂无数据',
						
					},
					textNoMore:"已经到底了",
				},
				// 列表数据
				dataList: []
			};
		},
		onLoad(options) {
			this.cateId = options.cateId;
			this.pid=options.pid;
			console.log(this.cateId)
			//获取分类列表
			this.getProductCateByPid();
		 
		},
		methods: {
			showChange(type){
				this.showType=type;
			},
			getProductCateByPid(){
				let data = {pid:this.pid};
				getProductCateByPid(data).then(
					res => {
						if(res.code==200){
							this.cates=res.data;
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			// tab切换
			cateChange(item) {
				this.cateId = item.cateId;
				this.mescroll.resetUpScroll()
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					cateId:this.cateId,
					defaultOrder:this.defaultOrder,
					page: page.num,
					pageSize: page.size
				};
				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;
						mescroll.endErr();
					}
				});
			},
			// 查看药品详情
			showDetail(item) {
				console.log("item.productId",item.productId)
				uni.navigateTo({
					url: '/pages/shopping/productDetails?productId='+item.productId
				})
			}
			
		}
	}
</script>

<style lang="scss">
	.top-fixed{
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 10;
	}
	.pub-tab-box{
		box-sizing: border-box;
		width: 100%;
		padding: 0 33upx;
		background-color: #FFFFFF;
		.tab-inner{
			height: 88upx;
			line-height: 88upx;
			display: flex;
			overflow-x: auto;
			justify-content: flex-start;
			align-items: center;
			
		}
		.item{
			font-size: 28upx;
			white-space: nowrap;
			line-height: 1;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
			margin-right: 60upx;
			display: flex;
			align-items: center;
			justify-content: center;
			&:last-child{
				margin-right: 0;
			}
			&.active{
				font-weight: bold;
				color: #333333;
			}
			.text{
				position: relative;
				z-index: 1;
			}
			.tab-bg{
				width: 72upx;
				height: 28upx;
				position: absolute;
				top: 17upx;
				left: 50%;
				transform: translateX(-36upx);
				z-index: -1;
			}
		}
		
	}
	 
	.medic-list{
		padding: 20upx;
		.item{
			box-sizing: border-box;
			min-height: 285upx;
			background: #FFFFFF;
			border: 4upx solid #FFFFFF;
			border-radius: 16upx;
			margin-bottom: 20upx;
			padding: 40upx 30upx;
			display: flex;
			
			.img-box{
				width: 200upx;
				height: 200upx;
				margin-right: 30upx;
				position: relative;
				image{
					width: 100%;
					height: 100%;
				}
				
			}
			
			.info-box{
				width: calc(100% - 210upx);
				.title{
					font-size: 32upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
					line-height: 40rpx;
					height: 80rpx;
				}
				.intro{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					line-height: 1;
					margin-top: 26upx;
				}
			 
				.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;
						}
						 
					}
				}
			}
		}
	}
	
	.goods-list{
		padding: 20upx;
		display: flex;
		flex-wrap: wrap;
		.item{
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			width: 345rpx;
			background: #FFFFFF;
			box-shadow: 0px 0px 10rpx 4rpx rgba(199, 199, 199, 0.22);
			border-radius: 20rpx;
			overflow: hidden;
			&:nth-child(2n) {
				margin-right: 0;
			}
			.img-box{
				width: 100%;
				height: 334upx;
				position: relative;
				image{
					width: 100%;
					height: 100%;
				}
				
			}
			
			.info-box{
				box-sizing: border-box;
				height: 182upx;
				padding: 20upx 20upx 30upx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.title{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
					line-height: 40upx;
				}
				.price-box{
					display: flex;
					align-items: flex-end;
					.now{
						display: flex;
						align-items: flex-end;
						margin-right: 20upx;
						.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;
						}
					}
					.old{
						font-size: 26upx;
						font-family: PingFang SC;
						font-weight: 500;
						text-decoration: line-through;
						color: #BBBBBB;
						line-height: 1.1;
					}
				}
			}
		}
	}
	.search-cont{
		padding: 16upx 30upx;
		background-color: #FFFFFF;
		display:flex;
		align-items: center;
		justify-content: space-between;
		.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;
			}
		}
		.icon-search{
			margin-left: 10upx;
			width: 40upx;
			height: 40upx;
			image{
				width: 40upx;
				height: 40upx;
			}
			
		}
	}
   
    .cf-box{
    	position: absolute;
    	z-index: 10;
    	left: 0;
    	right:0;
    	top: 0rpx;
    	bottom: 0rpx;
    	background-color: rgba(0,0,0, 0.3);
		backdrop-filter: blur(2rpx); /* 背景模糊度 */
    	display: flex;
    	flex-direction: column;
    	flex: 1;
    	justify-content: center;
    	align-items: center;
    	.title{
    		font-size: 34rpx;
    		font-weight: bold;
    		color: #fff;
    	}
    	.subTitle{
    		font-size: 28rpx;
    		font-weight: bold;
			margin-top: 4rpx;
    		color: #fff;
    	}
    }
    

</style>
