<template>
	<view class="bg">
		<view class="search-header" :style="{'height': height+'px','padding-top':statusBarHeight+'px'}">
			<uni-icons type="left" size="20" @click="goBack" style="flex-shrink: 0;"></uni-icons>
			<view class="input-model">
				<uni-icons style="height: 1.4rem;line-height: 1.4rem;" type="search" size="15" color="#ABAAAD" ></uni-icons>
				<input class="input-text" placeholder="请输入商品名称" v-model="searchText"></text>
			</view>
		</view>
		
		<div :style="{'height':height+'px'}"></div>
		
		
		<img :src="showImg(headImg)" class="topImg" />
		
		<view class="goods-container">
			<scroll-view class="left-container no-scrollbar" :scroll-y="true">
				<view :class="['type-item',typeIndex==i?'active':'']" 
					v-for="(item,i) in typeParam" :key="i">
					味 | WEI
					<view class="active-type-bar" v-if="typeIndex==i"></view>
				</view>
				<view style="height: 148rpx;width: 1rpx;"></view>
			</scroll-view>
			<scroll-view class="right-container no-scrollbar" :scroll-y="true">
				<view class="item" v-for="item in list" :key="item.goods.id" @click="viewDetail(item.goods)">
					<image class="item-img" :src="showImg(item.goods.image)" mode=""></image>
					<view class="content">
						<view class="title text-overflowRows">{{item.goods.title}}</view>
						<view class="bottom">
							<view class="price">
								{{item.goods.money/100}}
							</view>

							<view class="buy-cart" @click.stop="showOrderCart(item.goods)">
								<image :src="showImg('/uploads/20241104/0ac7eb782b73e60e3cfc8200b7934a4a.png')"></image>
							</view>
						</view>
					</view>
				</view>
			
				<view style="height: 148rpx;width: 1rpx;"></view>
			</scroll-view>
			
		</view>
		
		<!-- 购物车 -->
		<view class="btn-bottom" >
			<cartDataVue ref="cartDataVueRef" :paramData="paramData" @changeParamData="changeParamData" style="width: 100%;height: 100%;">
				<template class="btn-list" slot="content">

						<view class="left-box">
							<uni-badge class="uni-badge-left-margin" :text="paramData.num" absolute="rightTop" :offset="[5, 5]" size="small"
							:custom-style="{background:'#D90F01',color:'#ffffff',border:'1px solid #ffffff'}">
							<view class="img-box" @click.stop="showCartClick">
								<image :src="showImg('/uploads/20241104/88b88bf627a63dd000afe769d1392fb4.png')" mode="aspectFill"></image>
							</view>
							</uni-badge>
							<view class="bottom-price">
								<text style="color: #000000;">总计：</text>
								￥
								<view class="bottom-price-yuan">{{paramData.iNum}}</view>
								<view>.{{paramData.fNum}}</view>
							</view>
							<view class="bottom-detail-icon" @click.stop="showCartClick">
								明细 
								<view v-if="!paramData.showCart" style="transform: rotate(-90deg);"><uni-icons color="#0B898E" type="left" size="15"></uni-icons></view>
								<view v-else style="transform: rotate(90deg);"><uni-icons color="#0B898E" type="left" size="15"></uni-icons></view>
							</view>
							
						</view>
						<view class="btn-buy" @click="goCartOrder">
							去结算
						</view>

				</template>
			</cartDataVue>
		</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">
					<img :src="showImg(sku[productIndex].image)" alt="">
					<view class="right-content">
						<view class="bottom-productPrice com-price">{{(sku[productIndex].money||0)/100}}</view>
						<view class="bottom-content text-overflow">已选择：{{sku[productIndex].title}}</view>
					</view>
				</view>
				<view>
					<view style="display: flex;align-items: center;justify-content: space-between;flex-wrap: wrap;">
						<view style="position:relative;" v-for="(botItem,botIndex) in sku" :key="botIndex">
							<view :class="['botProduct','text-overflow',{'noStore':botItem.store==0},{'botProducts':productIndex==botIndex}]"
								@click="changeProduct(botItem,botIndex)">
								{{botItem.title}}
							</view>
							<view class="noStore-text" v-if="botItem.store==0">
								不可购买
							</view>
							<!-- <view class="noStore-text" v-else-if="botItem.is_post!=1">不可加购</view> -->
						</view>
					</view>
				</view>
				<view class="buy-num com-flex-tao">
					数量
					<view class="number-btn">
						<view>
							<text @click="delNumber">-</text>
						</view>
						<view style="width: 96rpx;height: 69rpx;margin: 0 14rpx;">{{ buyNum }}</view>
						<view>
							<text @click="addNumber">+</text>
						</view>
					</view>
				</view>
			</view>
			<view class="placeholder-content">
				<view style="height: 100rpx;"></view>
				<view class="btn-cover">
					<view class="btn" @click.stop="order">加入购物车</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import cartDataVue from '../../components/cartData.vue'
	export default {
		components: {cartDataVue},
		data() {
			return {
				// 导航栏参数
				height: 0,
				statusBarHeight: 0,
				headImg: null,
				
				typeParam: [
					{id: 1, title: ''},{id: 1, title: ''},{id: 1, title: ''},
					{id: 1, title: ''},{id: 1, title: ''},{id: 1, title: ''},
					{id: 1, title: ''},{id: 1, title: ''},{id: 1, title: ''},
					{id: 1, title: ''},{id: 1, title: ''},{id: 1, title: ''},
					{id: 1, title: ''},{id: 1, title: ''},{id: 1, title: ''},
				],
				typeIndex: 0,
				
				list:[],
				finished: false,
				
				paramData: {allPrice: 0,iNum:0, fNum:'00', showCart: false, num: 0},
				
				currentGoods: {},
				sku: [],
				productIndex: 0,
				buyNum: 1,
				cartDataVueShow: false,
				
				searchText: '',
				searchList: []
			}
		},
		onLoad(options) {
			this.getHeadImg("techan").then(res => {this.headImg = res})
			
		},
		onReady() {
			this.initRectInfo()
			this.getList()
		},
		methods: {
			initRectInfo () {
				const sysInfo = uni.getSystemInfoSync()
				this.statusBarHeight = sysInfo.statusBarHeight
				// 默认高度
				this.height = sysInfo.statusBarHeight + 40
			},
			
			changeType (typeName, value) {
				if (this[typeName]!==value) {
					this[typeName]=value
					
					this.list = []
					this.getList()
				}
			},
			
			// 根据产品分类获取产品列表
			getList(){
				this.Post({
					tag_id: 60,
					offset: this.list.length,
					limit: 10,
				},'/api/tag/getGoodsByTagId').then(res => {
					this.list = [...this.list, ...res.data];
					if (res.data.length < 10) {
						this.finished = true
					}
				})
			},
			viewDetail(item) {
				// this.goOtherDetail(item)
				if(!item.link_type) {
					uni.navigateTo({
						url: '/subPackages/techan/detail?id=' + item.id
					})
				}
			},
			
			showCartClick () {
				if (this.paramData.showCart) {
					this.$refs.cartDataVueRef.closePopup()
				} else {
					this.$refs.cartDataVueRef.openPop()
				}
			},
			changeParamData (data) {
				for(let key in this.paramData) {
					this.paramData[key] = data[key]
				}
			},
			
			// 购物车相关
			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({goods_id: goods_id},'/api/goods/getSpecificationsByGoodsId'
				).then(res => {
					if (res) {
						this.sku = res.data;
						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()
			},
			order() {
				let goods = this.sku[this.productIndex]
				
				if(goods.is_post!=1) {
					uni.showToast({
						title: '此规格不支持加入购物车',
						icon: 'none'
					})
					return
				}
				
				goods.buyNum = this.buyNum
				let goodsInfo = {goodsInfo:this.currentGoods, skuInfo: goods, isSelected: true}
				
				this.Post({good_id: this.currentGoods.id, specifications_id: goods.id,num: this.buyNum },
				'/api/shopping/addShopping').then(res => {
					if (res) {
						let selectedData = []
						try {
							selectedData = JSON.parse(uni.getStorageSync('cartDataInfo'));
						} catch(e) {
							selectedData = []
						}
						let currentGoods = selectedData.find(v =>v==goods.id)
						if (!currentGoods) {
							selectedData.push(goods.id)
						}
						uni.setStorageSync('cartDataInfo', JSON.stringify(selectedData));
						uni.$emit("updateDataByConnect", {msgType:'updateCartDataInfo',data:null})
						this.closePopup()
						// this.$refs.cartDataVueRef.openPop()
					}
				});
				
			},
			goCartOrder () {
				this.$refs.cartDataVueRef.goCartOrder()
			},
		
		},
		onReachBottom() {
			setTimeout(() => {
				if (!this.finished) this.getList()
			},1000)
		}
	}
</script>

<style lang="scss" scoped>
	view {
		box-sizing: border-box;
	}
	.search-header{
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		background: white;
		display: flex;
		align-items: center;
		padding-left: 10rpx;
		padding-right: 120px;
		
		.input-model{
			flex: 1;
			width: 1rpx;
			height: 64rpx;
			background: #FFFFFF;
			border-radius: 55rpx;
			border: 1rpx solid #20898D;
			margin-left: 12rpx;
			display: flex;
			align-items: center;
			font-weight: 400;
			font-size: 24rpx;
			color: #ABAAAD;
			padding-left: 24rpx;
		}
		.input-text{
			height:19px;
			padding-left: 14rpx;
			font-weight: 400;
			font-size: 24rpx;
		}
	}
	
	
	.bg {
		height: 100vh;
		padding-bottom: 26rpx;
		display: flex;
		flex-direction: column;
		background: #F8FAFB;
	}
	
	.topImg {
		width: 750rpx;
		height: 344rpx;
		flex-shrink: 0;
	}
	
	.goods-container{
		height: 1rpx;
		flex: 1;
		display: flex;
	}
	
	.left-container{
		width: 214rpx;
		flex-shrink: 0;
		.type-item{
			font-weight: 400;
			font-size: 24rpx;
			color: #000000;
			text-align: center;
			width: 216rpx;
			height: 120rpx;
			line-height: 120rpx;
			position: relative;
			.active-type-bar{
				position: absolute;
				left: 0;
				top: 0;
				bottom: 0;
				height: 100%;
				width: 12rpx;
				background: #74A5AA;
			}
		}
		.type-item.active{
			background: linear-gradient( 90deg, rgba(32,137,141,0.4) 0%, rgba(32,137,141,0) 100%);
			font-weight: 600;
			font-size: 28rpx;
		}
	}
	.right-container{
		flex: 1;
		width: 1rpx;
		background: white;
		padding-top: 32rpx;
		.item {
			width: 100%;
			padding:0 32rpx 32rpx 32rpx;
			display: flex;
			.item-img {
				width: 148rpx;
				height: 148rpx;
				border-radius: 15rpx;
				flex-shrink: 0;
			}
			.content {
				height: 148rpx;
				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: 40rpx;
					color: #FA0000;
					font-weight: 400;
				}
				.price::before {
					content: '￥';
					font-size: 24rpx;
				}
			}
			.buy-cart{
				width: 44rpx;
				height: 44rpx;
				image{
					width: 100%;
					height: 100%;
				}
			}
		}
	}
	
 .btn-list {
		z-index: 99;
		position: fixed;
		bottom: 0;
		width: 750rpx;
		height: 148rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(6, 0, 1, 0.1);
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 26rpx;
	
		.left-box {
			display: flex;
			align-items: center;
			.bottom-price{
				display: flex;
				align-items: baseline;
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 27rpx;
				color: #D70000;
				padding: 0 12rpx;
				.bottom-price-yuan{
					font-size: 40rpx;
				}
			}
			.bottom-detail-icon{
				font-size: 24rpx;
				display: flex;
				align-items: center;
				color: #0B898E;
			}
			
			.img-box {
				image {
					width: 78rpx;
					height: 78rpx;
				}
	
			}
		}
		
	
		.btn-buy {
			width: 240rpx;
			height: 78rpx;
			background: #74A5AA;
			border-radius: 40rpx;
			text-align: center;
			line-height: 78rpx;
			font-size: 32rpx;
			font-family: PingFangSC;
			font-weight: 500;
			color: #FFFFFF;
		}
	}
	
	
// 规格弹框
.popup-content {
		background-color: white;
		padding: 0rpx 39rpx 51rpx 39rpx;
		height: auto;
		border-radius: 20rpx 20rpx 0 0;
		.bottom-productImg {
			display: flex;
			margin-bottom: 48rpx;
		}
		
		.bottom-productImg img {
			width: 218rpx;
			height: 179rpx;
			background: #666666;
			border-radius: 13rpx;
		}
		
		.right-content {
			margin: 10rpx 0 0 41rpx;
		}
		
		.bottom-productPrice {
			font-size: 40rpx;
			color: #C3282E;
		
			&:before {
				content: "￥";
				font-size: 26rpx;
			}
		}
		
		.bottom-content {
			width: 331rpx;
			font-size: 27rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #666666;
		}
		
		.botProduct {
			width: 320rpx;
			// height: 78rpx;
			border-radius: 13rpx;
			background-color: #EFEFEF;
			font-size: 29rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 78rpx;
			text-align: center;
			margin-bottom: 25rpx;
			display: inline-block;
			position: relative;
			padding: 0 40rpx;
		}
		.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 {
			background: #74A5AA;
			color: #FFFFFF;
		}
		
		.buy-num {
			font-size: 29rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
			// border-top: 1rpx solid #CCCCCC;
			padding: 39rpx 0;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-top: 1px solid #CCCCCC;
		}
		
		.buy-num .number-btn {
			display: flex;
		}
		
		.buy-num .number-btn view {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 69rpx;
			height: 69rpx;
			border: 1rpx solid #CCCCCC;
			border-radius: 7rpx;
		}
		
		.buy-num .number-btn>view text {
			font-size: 46rpx;
		}
		
		.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;
		}
	}


.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: #74A5AA;
			border-radius: 40rpx;
					
			font-size: 34rpx;
			font-family: PingFangSC;
			font-weight: 500;
			color: #FFFFFF;
		}
	}
}


</style>
