<template>
	<view class="bg">
		<!-- 搜索 -->
		<view  class="flex-column" style="height: 100%;">
			<view style="padding: 20rpx 26rpx 0;background: white;">
				<view class="search-box">
					<image src="https://static.ticket.sz-trip.com/taizhou/images/search.png" alt="">
					<input type="text" class="input" placeholder="请输入关键字" v-model="searchText" @confirm="search();" />
					<view class="btn" @click="search()">搜索</view>
				</view>
			</view>
			
			
			<view class="search-container" style="background: #FFFFFF;">
				<view >
					<text :class="[search_type==0?'active-search-item':'']" @click="changeSearchParm(0)">综合</text>
				</view>
				<view >
					<!-- <text :class="[[1,2].includes(search_type)?'active-search-item':'']" @click="changeSearchParm(search_type==1?2:1)">销量</text>
					<view class="search-item">
						<view :class="[search_type==1?'active-search-item':'','triangle-up']" @click="changeSearchParm(1)">▼</view>
						<view :class="[search_type==2?'active-search-item':'']" @click="changeSearchParm(2)">▼</view>
					</view> -->
					<text :class="[[1,2].includes(search_type)?'active-search-item':'']" @click="changeSearchParm(2)">销量</text>
				</view>
				<view >
					<text :class="[[3,4].includes(search_type)?'active-search-item':'']" @click="changeSearchParm(search_type==3?4:3)">价格</text>
					<view class="search-item">
						<view :class="[search_type==3?'active-search-item':'','triangle-up']" @click="changeSearchParm(3)">▼</view>
						<view :class="[search_type==4?'active-search-item':'']" @click="changeSearchParm(4)">▼</view>
					</view>
				</view>
			</view>

			<view class="no-data-zhanwei" v-if="list.length<=0">
				<image src="https://static.ticket.sz-trip.com/uploads/20250618/0c2a469b4216f8cd570822b642d0a0fe.png"></image>
				<view style="padding:50rpx 0 67rpx">暂无数据</view>
			</view>
			<scroll-view class="right-container no-scrollbar" :scroll-y="true" @scrolltolower="scrollLow()" v-else>
				<view class="search-result" >
					<view  v-for="(item,i) in list" class="search-item" :key="i" @click="viewDetail(item)">
						<image :src="showImg(item.headimg)" mode="aspectFill"></image>
						<view class="search-result-container">
							<view>
								<view class="title text-overflowRows">{{item.title}}</view>
								<view class="tags text-overflow" style="overflow-y: hidden;" v-if="item.display_tags">
									<view class="tag" v-for="(tag,tagI) in item.display_tags.split(',')" :key="tagI">
										{{tag}}
									</view>
								</view>
							</view>
							<view class="flex-between" style="padding-top: 20rpx;">
								<view>
									<text class="price">{{item.price/100}}</text>
									<text class="oldPrice">{{item.market_price/100}}</text>
								</view>
								<view class="buy-cart" style="border-radius: 8rpx;" @click.stop="showOrderCart(item)">+</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view >
		</view>
		
	
		<uni-popup ref="popup" type="bottom" :safe-area="true">
			<view class="popup-content" v-if="sku.length>0">
				<view  @click="closePopup" style="padding: 31rpx 0 0 639rpx;width: 50rpx;height: 80rpx;">
					<uni-icons  type="closeempty" size="24"></uni-icons>
				</view>
				
				<view class="bottom-productImg">
					<image :src="showImg(sku[productIndex].headimg)" alt=""></image>
					<view class="right-content">
						<view class="bottom-productPrice com-price">{{(sku[productIndex].price||0)/100}}</view>
						<view class="bottom-content">已选择：{{sku[productIndex].sku_name}}</view>
						<view class="buy-num com-flex-tao">
							<view class="number-btn">
								<view>
									<text @click="delNumber">-</text>
								</view>
								<view style="flex: 1;">{{ buyNum }}</view>
								<view>
									<text @click="addNumber">+</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view style="max-height: 600rpx;overflow-y: auto;">
					<view class="sp">规格</view>
					<view class="sp-container">
						<view style="position:relative;max-width: 100%;" v-for="(botItem,botIndex) in sku" :key="botIndex">
							<view :class="['botProduct','text-overflow',{'noStore':botItem.store==0},{'botProducts':productIndex==botIndex}]"
								@click="changeProduct(botItem,botIndex)">
								<image :src="showImg(botItem.headimg)"></image>
								<view class="sku-name">{{botItem.sku_name}}</view>
							</view>
							<view class="noStore-text" v-if="botItem.store==0">
								不可购买
							</view>
						</view>
					</view>
				</view>
				
			</view>
			<view class="placeholder-content">
				<view style="height: 100rpx;"></view>
				<view class="btn-cover">
					<view class="btn" @click.stop="addToCart">加入购物车</view>
				</view>
			</view>
		</uni-popup>
	
		
	
	</view>
</template>

<script>

	export default {
		data() {
			return {
				type_pid: 20,
				searchText: '',
				search_type: 0,
		
				list:[],
				finished: false,
				currentGoods: {},
				sku: [],
				productIndex: 0,
				buyNum: 1,

			}
		},
		onLoad(options) {
			if(options.keywords) {
				this.searchText = options.keywords
				this.search()
			}
		},

		methods: {
			changeSearchParm(value) {
				console.log(value)
				this.search_type = value
				this.search()
			},
			search () {
				if (this.searchText.trim().length<=0) {
					uni.showToast({
						title:'请输入搜索关键字',
						icon:'none'
					})
					return
				}
				this.pushHis(this.searchText.trim());
				this.finished = false
				this.list = []
				this.getList()
			},
			pushHis(keywords) {
				if (!keywords.trim()) {
					return
				}
				let history = JSON.parse(uni.getStorageSync('tz_trip_keyowrds')) || [];
				let index = history.findIndex(item => item == keywords);
				if (index >= 0) history.splice(index, 1);
				history.unshift(keywords);
				uni.setStorageSync('tz_trip_keyowrds',JSON.stringify(history))
			},
			
			// 根据产品分类获取产品列表
			getList(){
				let param = {}
				if (this.search_type == 1) { param = {order: 'asc',sort:'sales_number'} }
				if (this.search_type == 2) { param = {order: 'desc',sort:'sales_number'} }
				if (this.search_type == 3) { param = {order: 'asc',sort:'price'} }
				if (this.search_type == 4) { param = {order: 'desc',sort:'price'} }
				this.Post({
					title: this.searchText.trim(),
					tag_id: this.type_pid,
					offset: this.list.length,
					limit: 10,
					...param
				},'/api/product/get_product_by_tag').then(res => {
					this.list = [...this.list, ...res.data.list];
					if (res.data.list.length < 10) {
						this.finished = true
					}
					console.log(this.list)
				})
			},
			scrollLow () {
				if (!this.finished) {
					this.getList()
				}
			},
			viewDetail(item) {
				uni.navigateTo({
					url: '/subPackages/techan/detail?id=' + item.id
				})
				
			},

			
			// 购物车相关
			showOrderCart (item) {
				console.log(item)
				this.sku = []
				this.productIndex = 0
				this.buyNum = 1
				this.currentGoods = JSON.parse(JSON.stringify(item))
				this.getSpecificationsByGoodsId(item.id)
			},
			changeProduct(item,index) {
				if (item.store==0) {
					uni.showToast({
						title:"库存不足！",
						icon:'none'
					})
					return
				}
				this.productIndex = index
			},
			getSpecificationsByGoodsId(goods_id) {
				this.Post({id: goods_id},'/api/product/get_product_detail'
				).then(res => {
					if (res) {
						this.sku = res.data.sku || [];
						if (!this.sku||this.sku.length<=0) {
							uni.showToast({
								title:'暂无可选规格',
								icon:'none'
							})
							return
						}
						this.openPop()
					}
				});
			},
			//数量加减
			addNumber() {
				this.buyNum += 1;
			},
			delNumber() {
				if (this.buyNum <= 1) {
					return;
				}
				this.buyNum -= 1;
			},
			closePopup() {
				this.$refs.popup.close()
			},
			openPop(){
				this.$refs.popup.open()
			},
			addToCart() {
				let goods = this.sku[this.productIndex]
				goods.buyNum = this.buyNum
				this.Post({sku_id: goods.id,num: this.buyNum },'/api/cart/add_sku').then(res => {
					if (res.code == 1) {
						uni.showToast({title: res.msg,icon: 'none'});
						uni.$emit("updateDataByConnect", {msgType:'updateCartDataInfo',data:null})
						this.closePopup()
						// this.$refs.cartDataVueRef.openPop()
					}
				});
			},
			goCartOrder () {
				this.$refs.cartDataVueRef.goCartOrder()
			},
		},
		onReachBottom() {
			
		}
	}
</script>

<style lang="scss" scoped>
	view {
		box-sizing: border-box;
	}

	.search-box {
		width: 100%;
		height: 67rpx;
		background: #F2F2F2;
		border-radius: 33rpx;
		padding: 6rpx 7rpx 6rpx 26rpx;
		font-size: 26rpx;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		image {
			width: 28rpx;
			height: 30rpx;
		}
		.input {
			flex: 1;
			border: none;
			outline: none;
			background: none;
			color: #666;
			margin: 0 20rpx;
		}
		.btn {
			background-color: #6A8A27;
			border-radius: 27rpx;
			font-size: 28rpx;
			color: #FFFFFF;
			padding: 10rpx 26rpx;
		}
	}
	
	
	.bg {
		height: 100vh;
		padding-bottom: 26rpx;
		display: flex;
		flex-direction: column;
		background: #F8FAFB;
	}
	

	
	.search-container{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-weight: bold;
		font-size: 27rpx;
		color: #000000;
		height: 107rpx;
		flex-shrink: 0;
		padding: 0 32rpx;
		&>view{
			display: flex;
			min-width: 70rpx;
			align-items: center;
			justify-content: space-between;
			height: 100%;
		}
		.search-item{
			display: flex;
			flex-direction: column;
			line-height: 16rpx;
			font-size: 14rpx;
		}
	}
	
	.right-container{
		flex: 1;
		width: 100%;
		height: 1rpx;
		background: white;
		.item {
			width: 100%;
			padding:0 32rpx 32rpx 32rpx;
			display: flex;
			.item-img {
				width: 161rpx;
				height: 161rpx;
				border-radius: 15rpx;
				flex-shrink: 0;
			}
			.content {
				height: 161rpx;
				padding-left: 24rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				flex: 1;
			}
			.title {
				font-weight: 500;
				font-size: 28rpx;
				color: #000000;
				width: 100%;
			}
			.bottom {
				display: flex;
				justify-content: space-between;
				align-items: center;
				.price {
					font-size: 33.33rpx;
					color: #DC2525;
					font-weight: 400;
				}
				.price::before {
					content: '￥';
					font-size: 24rpx;
				}
				.oldPrice{
					font-weight: 500;
					font-size: 21rpx;
					color: #999999;
					text-decoration-line: line-through;
				}
			}
			
		}
	}
.buy-cart{
	width: 44rpx;
	height: 44rpx;
	color: #FFFFFF;
	background: #6A8A2D;
	line-height: 44rpx;
	font-size: 30rpx;
	text-align: center;
	border-radius: 50%;
}
	
	.popup-content {
		background-color: white;
		padding: 0rpx 39rpx 51rpx 39rpx;
		height: auto;
		border-radius: 20rpx 20rpx 0 0;
		.bottom-productImg {
			display: flex;
			margin-bottom: 23rpx;
			image {
				width: 173.33rpx;
				height: 173.33rpx;
				background: #666666;
				border-radius: 13rpx;
			}
		}
		
		
		.right-content {
			padding-left: 14rpx;
			flex: 1;
			width: 1rpx;
		}
		
		.bottom-productPrice {
			font-size: 40rpx;
			color: #FC524B;
		
			&:before {
				content: "￥";
				font-size: 26rpx;
			}
		}
		
		.bottom-content {
			width: 100%;
			font-size: 27rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #666666;
		}
		
		.botProduct {
			position: relative;
			min-height: 67rpx;
			border-radius: 13rpx;
			margin-bottom: 34rpx;
			max-width: 100%;
			padding-right:15rpx;
			min-width: 320rpx;
			background: #EFEFEF;
			display: flex;
			align-items: center;
			box-sizing: border-box;
			color: #333;
			width: fit-content;
			&>image{
				width: 67rpx;
				height: 67rpx;
				border-radius: 13rpx;
				flex-shrink: 0;
			}
			.sku-name{
				flex: 1;
				padding-left: 15rpx;
				white-space: normal;
				font-size: 27rpx;
				word-break: break-all;
			}
		}
		.noStore{
			background-color: rgba(239, 239, 239, 1);
			color: rgba(153, 153, 153, 1);
		}
		.noStore-text{
			width: 113rpx;
			height: 43rpx;
			background: #C0C0C0;
			border-radius: 7rpx 0rpx 7rpx 0rpx;
			text-align: center;
			line-height: 43rpx;
			position: absolute;
			right: -14rpx;
			top: -20rpx;
			font-size: 23rpx;
			font-family: PingFangSC;
			font-weight: 400;
			color: #FFFFFF;
		}
		
		
		.botProducts {
			color: #6A8A27;
			border: 1px solid #6A8A27;
		}
		
		.buy-num {
			font-weight: 500;
			font-size: 31rpx;
			color: #000000;
			color: #333333;
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 233rpx;
			height: 53rpx;
			background: #EFEFEF;
			border-radius: 13rpx;
			margin-top: 25rpx;
			.number-btn {
				display: flex;
				height: 100%;
				width: 100%;
				view {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 69rpx;
					height: 100%;
				}
			}
		}
		
		.buy-btn {
			width: 670rpx;
			height: 78rpx;
			text-align: center;
			line-height: 78rpx;
			background: linear-gradient(90deg, #F84A56, #FF9834);
			border-radius: 40rpx;
		
			font-size: 34rpx;
			font-family: PingFangSC;
			font-weight: 500;
			color: #FFFFFF;
		}
		
		.btn-box {
			width: 750rpx;
			height: 151rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(6, 0, 1, 0.1);
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.sp {
			width: 100%;
			height: 30rpx;
			font-size: 32rpx;
			font-family: PingFangSC;
			font-weight: 400;
			color: #060001;
			line-height: 30rpx;
			border-top: solid 2rpx #ccc;
			margin: 60rpx 0;
			padding-top: 30rpx;
		}
		.sp-container{
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			min-height: 400rpx;
			max-height: 600rpx;
			overflow-y: auto;
			overflow-x: hidden;
			align-content: flex-start
		}
		
	}



.placeholder-content{
	background: white;
	position: relative;
	.btn-cover{
		z-index: 200;
		position: fixed;
		bottom: 0;
		width: 750rpx;
		height: 148rpx;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(6,0,1,0.1);
		.btn{
			width: 670rpx;
			height: 78rpx;
			text-align: center;
			line-height: 78rpx;
			background: #6A8A2D;
			border-radius: 11rpx;
					
			font-size: 34rpx;
			font-family: PingFangSC;
			font-weight: 500;
			color: #FFFFFF;
		}
	}
}

.search-result{
	display: flex;
	flex-direction: column;
	width: 100%;
	flex: 1;
	flex-shrink: 0;
	padding:0 26rpx;
	.search-item{
		width: 100%;
		height: 200rpx;
		margin-bottom: 30rpx;
		display: flex;
		image{
			width: 200rpx;
			height: 200rpx;
			border-radius: 13rpx;
		}
	}
	
	.search-result-container{
		padding:0 20rpx;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		flex: 1;
		width: 1rpx;
		.title{
			font-weight: 500;
			font-size: 31rpx;
			color: #000000;
		}
		.price{
			font-weight: 400;
			font-size: 33rpx;
			color: #C3282E;
		}
		.price:before{
			content: "￥";
			font-size: 23rpx;
		}
		.oldPrice{
			font-weight: 400;
			font-size: 21rpx;
			color: #999999;
			text-decoration-line: line-through;
			padding-left: 12rpx;
		}
	
		.tags{
			display: flex;
			padding-top: 20rpx;
			.tag{
				height: 35rpx;
				line-height: 35rpx;
				border-radius: 17rpx;
				border: 1px solid #000000;
				font-weight: 400;
				font-size: 24rpx;
				color: #000000;
				padding: 0 20rpx;
				margin-right: 20rpx;
			}
		}
	}

}

.typePopup-container{
	.place-area{
		width: 100%;
		opacity: 0;
	}
	.type-container.typedata-container{
		flex-wrap: wrap;background: #EDF5DC;padding: 30rpx 40rpx 0;
		width: 100%;
		display: flex;
		height: auto;
		padding-bottom: 30rpx;
		.type-item {
			margin-bottom: 30rpx;
		}
	}
	.bottom-close{
		width: 100%;
		height: 80rpx;
		background: #FFFFFF;
		font-weight: 500;
		font-size: 28rpx;
		color: #111111;
		line-height: 80rpx;
		text-align: center;
	}
}

.triangle-up {
	transform: rotate(180deg);
	display: inline-block;
}
.active-search-item{
	color: #6A8A2D;
}


</style>
