<template>
	<view>
		<view class="top-fixed">
			<view class="status_bar" :style="{height: statusBarHeight}"></view>

			<view class="search-cont">
				<image @click="goBack" class="w64 h64" src="https://cdn.his.cdwjyyh.com/images/back_black.png"></image>
				<view class="inner">
					<input v-model="searchValue" type="text" value="" placeholder="城口腊肉/香肠" confirm-type="搜索"
						@confirm="handleSearch" @input="handleInput" :focus='setFocus'
						placeholder-style="font-size:28rpx;color:#BBBBBB;font-family: PingFang SC;" />
					<image @click="clearInput" v-if="searchValue" class="w32 h32"
						src="https://cdn.his.cdwjyyh.com/images/search_close_gray.png">
					</image>
					<view class="line"></view>
					<view class="search" @click="handleSearch">搜索</view>
				</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://cdn.his.cdwjyyh.com/images/tab_bg.png"
								mode=""></image>
						</view>
					</view>
				</view> -->
				<view class="tab-inner">
					<view v-for="(item,index) in selectList" :key="index"
						:class="activeIndex == item.value?'item active':'item'" @click="onSelect(item)">
						<view class="text">
							<text>{{ item.name }}</text>
							<image v-if="item.value==2||item.value==3" class="tab-bg"
								src="https://cdn.his.cdwjyyh.com/images/shaixuan_moren.png" mode=""></image>
						</view>
					</view>
					<view class="icon-search">
						<image @click="showChange(2)" v-if="showType==1" src="https://cdn.his.cdwjyyh.com/images/demonstrations1.png"
							mode="">
						</image>
						<image @click="showChange(1)" v-if="showType==2" src="https://cdn.his.cdwjyyh.com/images/demonstrations2.png"
							mode="">
						</image>
						<view class="line"></view>
						<view class="filter">
							<text>筛选</text>
							<image class="ml8" @click="toFilter" src="https://cdn.his.cdwjyyh.com/images/filter_icon.png" mode="">
						</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>
					<view class="info-box">
						<view class="title ellipsis2">{{item.productName}}</view>
						<view class="intro ellipsis">{{item.productInfo}}</view>
						<view class="sale">已售 {{item.sales}} {{item.unitName}} | 惊艳度98%</view>
						<view class="lable-group">
							<view class="lable-item">9.5折</view>
							<view class="lable-item">限购1份</view>
						</view>
						<view class="prce-num">
							<view class="price">
								<text class="unit">¥</text><text
									class="bold">{{splitPrice(item.price).integer}}</text>.{{splitPrice(item.price).decimal}}
							</view>
							<view class="cart-img" @click="navgetTo('../shopping/cart')">
								<image class="w40 h40" src="https://cdn.his.cdwjyyh.com/images/add_car.png"></image>
							</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>
					<view class="info-box">
						<view class="title ellipsis2">{{item.productName}}</view>
						<view class="intro ellipsis">{{item.productInfo}}</view>
						<view class="sale">已售 {{item.sales}} {{item.unitName}} | 惊艳度98%</view>
						<view class="lable-group">
							<view class="lable-item">9.5折</view>
							<view class="lable-item">限购1份</view>
						</view>
						<view class="prce-num">
							<view class="price">
								<text class="unit">¥</text><text
									class="bold">{{splitPrice(item.price).integer}}</text>.{{splitPrice(item.price).decimal}}
							</view>
							<view class="cart-img" @click="navgetTo('../shopping/cart')">
								<image class="w40 h40" src="https://cdn.his.cdwjyyh.com/images/add_car.png"></image>
							</view>
						</view>
						<view class="card">
							<view class="card-item">
								<image class="img" src="https://cdn.his.cdwjyyh.com/images/ranking_icon.png"></image>
								<text class="ranking">健康新品热销榜</text>
								<text class="bold">top.1</text>
							</view>
							<image class="go" src="https://cdn.his.cdwjyyh.com/images/jb_arrow_right_icon.png"></image>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
		<Filter />
	</view>
</template>

<script>
	import Filter from '@/pages_shopping/shopping/components/filter.vue'
	import {
		getProductCateByPid,
		getProducts
	} from '@/api/product'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		components: {
			Filter
		},
		data() {
			return {
				setFocus: false,
				searchValue: '',
				activeIndex: 0,
				// 状态栏的高度
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				searchValue: "",
				showType: 2,
				selectList: [{
					name: '综合',
					value: 0
				}, {
					name: '销量',
					value: 1
				}, {
					name: '价格',
					value: 2
				}, {
					name: '惊艳度',
					value: 3
				}],
				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://cdn.his.cdwjyyh.com/images/no_data.png',
						tip: '暂无数据',

					},
					textNoMore: "已经到底了",
				},
				// 列表数据
				dataList: []
			};
		},
		onLoad(options) {
			this.cateId = options.cateId;
			this.pid = options.pid;
			console.log(this.cateId)
			//获取分类列表
			this.getProductCateByPid();

		},
		onHide() {
			// 页面隐藏时重置聚焦状态
			this.setFocus = false;
		},
		methods: { // 处理输入
			handleInput(e) {
				this.searchValue = e.detail.value;
			},
			// 清除输入内容
			clearInput() {
				this.searchValue = '';
				this.setFocus = true; // 清除后重新聚焦
			},
			goBack() {
				uni.navigateBack({
					delta: 1 // 返回的页面数，1表示返回上一页
				});
			},
			splitPrice(price) {
				const priceStr = parseFloat(price).toFixed(2).toString();
				return {
					integer: priceStr.split('.')[0],
					decimal: priceStr.split('.')[1]
				};
			},
			handleSearch(e) {
				this.searchValue = e.detail.value;
				this.mescroll.resetUpScroll();
			},
			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 => {}
				);
			},

			onSelect(item) {
				console.log("item", item)
				this.activeIndex = item.value
			},
			// tab切换
			cateChange(item) {
				this.cateId = item.cateId;
				this.mescroll.resetUpScroll()
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					productName: this.searchValue,
					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) {
				uni.navigateTo({
					url: '/pages_shopping/shopping/productDetails?productId=' + item.productId
				})
			}

		}
	}
</script>

<style lang="scss">
	page {
		background: #F5F7FA;
	}

	.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: space-between;
			align-items: center;
			font-size: 26rpx;
			color: #666666;

			.item {
				font-size: 28upx;
				white-space: nowrap;
				line-height: 1;
				font-family: PingFang SC;

				margin-right: 48upx;
				display: flex;
				align-items: center;
				justify-content: center;

				&:last-child {
					margin-right: 0;
				}

				&.active {
					font-weight: 600;
					color: #02B176;
				}

				.text {
					position: relative;
					z-index: 1;
					display: flex;
					align-items: center;
				}

				.tab-bg {
					width: 24upx;
					height: 24upx;
					margin-left: 8rpx;
				}
			}

			.icon-search {
				display: flex;
				align-items: center;

				.line {
					width: 2rpx;
					height: 24rpx;
					background: #EEEEEE;
					border-radius: 0rpx 0rpx 0rpx 0rpx;
					margin: 0 20rpx;
				}

				image {
					width: 32upx;
					height: 32upx;
				}

				.filter {
					display: flex;
					align-items: center;
				}

			}

		}
	}



	.medic-list {
		padding: 20upx;

		.item {
			box-sizing: border-box;
			min-height: 296upx;
			background: #FFFFFF;
			border: 4upx solid #FFFFFF;
			border-radius: 16upx;
			margin-bottom: 18upx;
			padding: 24rpx;
			display: flex;

			.img-box {
				flex-shrink: 0;
				width: 248upx;
				height: 248upx;
				margin-right: 20upx;
				border-radius: 16upx;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.info-box {
				flex: 1;
				min-width: 0;

				.title {
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
					margin-bottom: 4rpx;
				}

				.intro {
					font-weight: 400;
					font-size: 22rpx;
					color: #D46C0D;
					margin-bottom: 8upx;
				}

				.sale {
					font-size: 21rpx;
					color: #999999;
				}

				.lable-group {
					display: flex;
					margin-top: 8rpx;

					.lable-item {
						margin-right: 8rpx;
						height: 30rpx;
						padding: 0 8rpx;
						border-radius: 4rpx 4rpx 4rpx 4rpx;
						border: 1rpx solid #FFA599;
						font-size: 21rpx;
						color: #FF4B33;
					}
				}

				.prce-num {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 8upx;

					.price {
						font-weight: 600;
						font-size: 26rpx;
						color: #FA341E;
						font-size: 26rpx;

						.unit {
							font-size: 20upx;
						}

						.bold {
							font-size: 36upx;
						}
					}

				}
			}
		}
	}

	.goods-list {
		padding: 20upx;
		display: flex;
		flex-wrap: wrap;

		.item {
			margin-right: 18rpx;
			margin-bottom: 18rpx;
			width: 342rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			overflow: hidden;

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

			.img-box {
				width: 100%;
				height: 342upx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.info-box {
				box-sizing: border-box;
				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;
					margin-bottom: 4rpx;
				}

				.intro {
					font-weight: 400;
					font-size: 22rpx;
					color: #D46C0D;
					margin-bottom: 8upx;
				}

				.sale {
					font-size: 21rpx;
					color: #999999;
				}

				.lable-group {
					display: flex;
					margin-top: 8rpx;

					.lable-item {
						margin-right: 8rpx;
						height: 30rpx;
						padding: 0 8rpx;
						border-radius: 4rpx 4rpx 4rpx 4rpx;
						border: 1rpx solid #FFA599;
						font-size: 21rpx;
						color: #FF4B33;
					}
				}

				.prce-num {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 8upx;

					.price {
						font-weight: 600;
						font-size: 26rpx;
						color: #FA341E;
						font-size: 26rpx;

						.unit {
							font-size: 20upx;
						}

						.bold {
							font-size: 36upx;
						}
					}
				}

				.card {
					margin-top: 20rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					background: #FFF4E8;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					width: 100%;
					height: 40rpx;
					padding: 8rpx 10rpx;
					box-sizing: border-box;

					.card-item {
						display: flex;
						align-items: center;

						.img {
							width: 24rpx;
							height: 24rpx;
							margin-right: 4rpx;
						}

						.ranking {
							font-size: 20rpx;
							color: #4D4D4D;
						}

						.bold {
							font-family: Roboto Flex, Roboto Flex;
							font-weight: 600;
							font-size: 24rpx;
							color: #F4A007;
							margin-right: 6rpx;
							transform: skewX(-8deg);
							/* 负值向左倾斜，正值向右倾斜 */
							display: inline-block;
							/* transform需要inline-block或block */
						}
					}

					.go {
						width: 16rpx;
						height: 16rpx;
					}
				}
			}
		}
	}

	// .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;
	// 		}

	// 	}
	// }

	.search-cont {
		display: flex;
		padding: 16rpx 30rpx;
		background-color: #FFFFFF;

		.inner {
			box-sizing: border-box;
			width: 436rpx;
			height: 64rpx;
			background: #F5F7FA;
			border-radius: 36rpx;
			display: flex;
			align-items: center;
			padding: 0 24rpx;
		
			image {
				padding: 16rpx;
				/* 增加点击区域 */
				margin: -16rpx;
				/* 负边距抵消padding对布局的影响 */
				box-sizing: content-box;
			}
		
			.search {
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
			}
		
			.line {
				width: 2rpx;
				height: 24rpx;
				background: #999999;
				margin: 0 26rpx;
			}
		
			input {
				height: 60rpx;
				line-height: 60rpx;
				flex: 1;
			}
		}
	}
</style>