<template>
	<view >
		<view class="top-fixed">
			<view class="search-cont">
				<view class="inner">
					<image class="icon-search" src="https://cdn.his.cdwjyyh.com/images/search.png" mode=""></image>
					<input type="text" @confirm="goSearch"   :value="form.title" placeholder="输入商品名称" placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
				</view>
			</view>
			<view class="cate-list">
			<!-- 关键字列表 -->
			<scroll-view   scroll-x="true" >
				<view class="inner">
					<view v-for="(item,index) in cates" :key="index" :class="cateId == item.dictValue?'item active':'item'" @click="choseCate(item)">
						{{ item.dictLabel }}
					</view>
				</view>
			</scroll-view>
			</view>
		</view>
		
		<mescroll-body top="204rpx" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<!-- 药品列表 -->
			<view class="goods-list" >
				<view class="item" v-for="(item,index) in dataList" :key="index" @click="showDetail(item)">
					<view class="img-box">
						<image :src="item.image" mode="aspectFill"></image>
					</view>
					<view class="info-box">
						<view class="title ellipsis2">{{item.productName}}</view>
						<view class="sku">{{item.sku}}</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 class="tui-money">
							分享赚{{item.brokerage.toFixed(2)}}
							 
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import {getStoreProductAttrValueList} from '@/api/product.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				cateId:0,
				cates:[],
				searchKey:"",
				mescroll: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://cdn.his.cdwjyyh.com/images/no_data.png',
						tip: '暂无数据'
					}
				},
				// 列表数据
				dataList: [],
			};
		},
		onLoad(options) {
			var that=this;
			this.cates=this.utils.getDict("storeProductTuiCate");
		},	
		methods: {
			choseCate(item){
				this.cateId = item.dictValue;
				this.mescroll.resetUpScroll()
			},
			goSearch(e) {
				this.searchKey=e.detail.value;
				this.mescroll.resetUpScroll()
				 
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					tuiCateId:this.cateId,
					productName:this.searchKey,
					page: page.num,
					pageSize: page.size
				};
				getStoreProductAttrValueList(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) {
				uni.navigateTo({
					url: '/pages_shopping/shopping/productDetails?productId=' + item.productId
				})
			},
		}
	}
</script>

<style lang="scss">
	.top-fixed{
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 10;
		.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;
				}
				
			}
			 
		}
		.cate-list{
			box-sizing: border-box;
			background: #fff;
			padding: 10upx 27upx;
			height: 100upx;
			.inner{
				display: flex;
			}
			.item{
				flex-shrink: 0;
				padding: 0 24upx;
				height: 64upx;
				line-height: 64upx;
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #2BC7B9;
				background: #F5FFFE;
				border: 1px solid #8AD5CE;
				border-radius: 32upx;
				margin: 0 20upx 20upx 0;
				&.active{
					color: #FFFFFF;
					background: #2BC7B9;
					border: 1px solid #2BC7B9;
				}
			}
		}
	}
	.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;
				image{
					width: 100%;
					height: 100%;
				}
			}
			.info-box{
				box-sizing: border-box;
				padding: 20upx 20upx 30upx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				position: relative;
				.title{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
					line-height: 40upx;
				}
				.sku{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #BBBBBB;
					line-height: 1.1;
				}
				.price-box{
					margin-bottom: 50rpx;
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					.now{
						display: flex;
						align-items: flex-end;
						margin-bottom: 5upx;
						.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;
					}
				}
				.tui-money{
					position: absolute;
					right:0rpx;
					bottom: 15rpx;
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFF;
					background:linear-gradient(to right, #FF6633 , #F11118); 
					padding: 6rpx 12rpx;
					border-radius: 30rpx 0rpx 0rpx 30rpx;
					
				}
			}
		}
	}
	
</style>
