<template>
	<view class="bg">
		<span class="iconfont topLeft" @click="goBack">&#xe660;</span>
		<img :src="showImg('/uploads/20240924/4a580d7089dbdb07f830909c25f0f751.jpg')" class="topImg" />
		<view class="type-container">
			<view class="send-type-container">
				<view :class="['type',type1==0?'active':'']" @click.stop="changeType('type1',0)">自提专区</view>
				<view :class="['type',type1==1?'active':'']" @click.stop="changeType('type1',1)">邮寄专区</view>
			</view>
			
			<view class="goods-type-container">
				<view :class="['type',type2==0?'active':'']" @click.stop="changeType('type2',0)">特产</view>
				<view :class="['type',type2==1?'active':'']" @click.stop="changeType('type2',1)">文创</view>
			</view>
		</view>
		
		<view class="box">
			<view class="item" v-for="item in list" :key="item.id" @click="viewDetail(item)">
				<image class="item-img" :src="showImg(item.image)" mode=""></image>
				<view class="content">
					<view class="title text-overflowRows">{{item.title}}</view>
					<view class="bottom">
						<view class="price">
							{{item.money/100}}
						</view>
						<view class="buy" v-show="type1==0">
							立即购买
						</view>
						<view class="buy-cart" v-show="type1==1" @click.stop="showOrderCart(item)">
							买
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view style="height: 148rpx;width: 1rpx;" v-if="type1==1"></view>
		<view class="btn-bottom" v-if="type1==1">
			<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:'#F7F7F7',color:'#F84A56',border:'1px solid #F84A56'}">
							<view class="img-box" @click.stop="showCartClick">
								<image src="https://yjdtadmin.sz-trip.com/uploads/20240726/76702dae980283c24f9d471262f0cbbe.png" mode="aspectFill"></image>
							</view>
							</uni-badge>
							<view class="bottom-price">
								￥
								<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="#000" type="left" size="15"></uni-icons></view>
								<view v-else style="transform: rotate(90deg);"><uni-icons color="#000" 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 class="sp">
						规格
					</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>
		
					</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>
			
			
			<!-- <view class="btn-box">
				<view class="buy-btn" @click="order">
					下一步
				</view>
			</view> -->
		</uni-popup>
	</view>
</template>

<script>
	import cartDataVue from '../../compoents/cartData.vue'
	export default {
		components: {cartDataVue},
		data() {
			return {
				type1:0,
				type2:0,
				list:[],
				finished: false,
				
				paramData: {allPrice: 0,iNum:0, fNum:'00', showCart: false, num: 0},
				
				currentGoods: {},
				sku: [],
				productIndex: 0,
				buyNum: 1,
				cartDataVueShow: false
			}
		},
		onReady() {
			this.getList()
		},
		methods: {
			changeType (typeName, value) {
				if (this[typeName]!==value) {
					this[typeName]=value
					
					// this.list = []
					// this.getList()
				}
			},
			
			// 根据产品分类获取产品列表
			getList(){
				this.Post({
					type_id: 19,
					offset: this.list.length,
					limit: 10,
				},'/api/goods/getGoodsByType').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(index) {
				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]
				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;
	}
	
	.bg {
		background:#F7F7F7;
		min-height: 100vh;
		padding-bottom: 26rpx;
	}
	
	.topLeft {
		position: absolute;
		left: 26rpx;
		top: 101rpx;
		font-size: 40rpx;
		z-index: 2;
	}
	
	.topImg {
		width: 750rpx;
		height: 440rpx;
	}
	
	.box {
		margin-top: 26.67rpx;
		// border-radius: 20rpx 20rpx 0rpx 0rpx;
		padding: 0 26.67rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}
	
	.item {
		margin-bottom: 22rpx;
		width: 335rpx;
		height: 497rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(153,153,153,0.38);
		border-radius: 13rpx;
	}
	
	.item-img {
		width: 100%;
		height: 342rpx;
		background: #87CD93;
		border-radius: 13rpx 13rpx 0rpx 0rpx;
	}
	
	.content {
		height: 145rpx;
		padding: 0 13rpx 13.33rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}
	
	.title {
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 29rpx;
		color: #000000;
		width: 291rpx;
	}
	
	.bottom {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.price {
		font-size: 34.67rpx;
		font-family: PingFangSC;
		font-weight: bold;
		color: #D60000;
	}
	.price::before {
		content: '￥';
		font-size: 24rpx;
	}
	.price::after {
		content: '起';
		font-size: 24rpx;
		color: #999999;
		font-weight: 500;
	}
	
	.buy {
		width: 140rpx;
		height: 42rpx;
		background: linear-gradient(-90deg, #FC5109, #FC930A);
		border-radius: 21rpx;
		text-align: center;
		line-height: 42rpx;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 27rpx;
		color: #FFFFFF;
	}
	.buy-cart{
		width: 42rpx;
		height: 42rpx;
		imgae{
			width: 100%;
			height: 100%;
		}
	}
	
	.type-container{
		width: 100%;
		height: 227rpx;
		padding: 40rpx 26rpx 0;
		background: #FFFFFF;
		
		.send-type-container{
			width: 100%;
			height: 66rpx;
			padding: 4rpx;
			background: #EAF6F9;
			border-radius: 13rpx;
			border: 1px solid #248BAA;
			
			display: flex;
			.type{
				flex: 1;
				flex-shrink: 0;
				text-align: center;
				height: 100%;
				border-radius: 13rpx;
				font-weight: 500;
				font-size: 28rpx;
				color: #248BAA;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			.type.active{
				color: #FFFFFF;
				background: #248BAA;
			}
		}
	
		.goods-type-container{
			width: 100%;
			margin-top: 64rpx;
			height: 55rpx;
			display: flex;
			.type {
				flex: 1;
				flex-shrink: 0;
				height: 100%;
				font-weight: 500;
				font-size: 31rpx;
				color: #666666;
				display: flex;
				justify-content: center;
				position: relative;
			}
			.type.active{
				color: #000000;
				font-weight: bold;
			}
			.type.active::after{
				content: '';
				width: 67rpx;
				height: 5rpx;
				background: #248BAA;
				border-radius: 3rpx;
				position: absolute;
				bottom: 0;
			}
		}
	}
	

 .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 50rpx;
	
		.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: #F84A56;
				padding: 0 33rpx;
				.bottom-price-yuan{
					font-size: 40rpx;
				}
			}
			.bottom-detail-icon{
				font-size: 24rpx;
				display: flex;
				align-items: center;
			}
			
			.img-box {
				image {
					width: 78rpx;
					height: 78rpx;
				}
	
			}
		}
		
	
		.btn-buy {
			width: 254rpx;
			height: 78rpx;
			background: linear-gradient(-90deg, #FC5109, #FC930A);
			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: 23rpx;
		}
		
		.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: #FC524B;
		
			&: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: #F5F5F5;
			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 {
			// border: 1rpx solid #00AAFF;
			// background-color: rgba(254, 180, 25, 1);
			background: #248BAA;
			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;
		}
		
		.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;
		}
		
		.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;
		}
	}


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

</style>
