<template>
	<view class="content">
		<view class="top-content">
			<!-- 搜索框 -->
			<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" v-model="keyword" placeholder="输入关键字搜索" confirm-type="search" @confirm="doSearch" placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
				</view>
			</view>
			<view class="cate-list">
				<Menu :list="cates" @menuClick="menuClick" v-if="cates.length>0" style="width:100%;"></Menu>
			</view>
		</view>
		<mescroll-body  v-if="top!=null" :top="top"  bottom="0" ref="mescrollRef" @init="mescrollInit"    :down="downOption" :up="upOption" @down="downCallback" @up="upCallback">
		<view class="package-box"  >
			<view class="item" @click="navTo('/pages_index/packageDetails?packageId='+item.packageId)" v-for="(item,index) in dataList">
				<view class="top">
					<image :src="item.imgUrl"></image>
				</view>
				<view class="bottom">
					<view class="title ellipsis2">
						{{item.packageName}}
					</view>
					<view class="price-box">
						<view class="price">￥{{item.price.toFixed(2)}}元/日</view>
						<view class="count">{{item.sales}}人已购</view>
					</view>
				</view>
			</view>
		</view>
		</mescroll-body>
	</view>
</template>

<script>
	import Menu from '@/components/Menu.vue'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import {getPackageList,getPackagCateList} from '@/api/package.js'
	export default {
		components: {Menu},
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				top:null,
				cates:[],
				diseaseType:0,
				keyword: '',
				mescroll:null,
				downOption: {   //下拉刷新
				 	use:true,
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					onScroll:false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore:"已经到底了",
					empty: {
						icon:'https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/empty_icon.png',
						tip: '暂无数据'
					}
				},
				dataList: []
				
			}
		},
		onLoad() {
			this.getPackagCateList(1);
		},
		methods: {
			menuClick(item){
				this.diseaseType = item.cateCode;
				this.mescroll.resetUpScroll()
			},
			getPackagCateList(type){
				var data={type:type}
				var that=this;
				getPackagCateList(data).then(
					res => {
						if(res.code==200){
							 this.cates=res.data;
							 var query = uni.createSelectorQuery().in(that);
							 setTimeout(function(){
							 	query.select('.top-content').boundingClientRect(data => {
							 		if (data) {
							 		    console.log('View height:', data.height+"px");
							 			that.top=data.height+"px";
							 		}
							 	}).exec();
							 },500);
						}
					},
					err => {
					}
				);
				
			},
			 
			doSearch(){
				this.mescroll.resetUpScroll()
			},
			navTo(url){
				uni.navigateTo({
					url: url
				})
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback() {
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调*/
			upCallback(page) {   
				//联网加载数据
				var that = this;
				var data = {
					isShow:1,
					diseaseType:this.diseaseType,
					keyword:this.keyword,
					pageNum: page.num,
					pageSize: page.size
				};
				getPackageList(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();
					}
				});
			},
		}
	}
</script>


<style lang="scss">
	page{
		
		height: 100%;
		background: #f6f6f6;
	}
</style>
<style scoped lang="scss">
	page{
		height: 100%;
		background-color: #f5f5f5;
	}
	.content{
		height: 100%;
		.top-content{
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			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;
				}
			}
		}
		.cate-list{
			box-sizing: border-box;
			background: #fff;
		}
		.package-box{
			display: flex;
			align-items: flex-start;
			justify-content: flex-start;
			flex-wrap: wrap;
			.item{
				box-shadow: 0px 0px 5px 2px rgba(0,0,0,0.05);
				background-color: #fff;
				width: calc(50% - 30rpx);
				border-radius: 15rpx;
				margin: 15rpx;
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				justify-content: flex-start;
				&:last-child{
				}
				.top{
					width:100%;
					height:340rpx;
					image{
						border-radius: 15rpx 15rpx 0rpx 0rpx;
						width:100%;
						height:100%;
					}
					
				}
				.bottom{
					padding: 15rpx 10rpx;
					width: 100%;
					.title{
						font-weight: bold;
						font-size: 28upx;
						font-family: PingFang SC;
						color: #111111;
					}
					.price-box{
						margin-top: 10rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
						width: 100%;
						.price{
							padding: 5rpx 10rpx;
							background-color: #2583EB;
							border-radius: 30rpx;
							font-size: 20upx;
							font-family: PingFang SC;
							color: #ffffff;
						}
						.count{
							font-size: 24upx;
							font-family: PingFang SC;
							color: #333333;
						}
						
					}
				}
			}
		}
	}
	 
</style>
