<template>
	<view class="bg" id="bg" v-if="info">
		<view class="swipe-box">
			<swiper class="swiper" :autoplay="true" :interval="3000" :duration="1000"
			@change="swiperChange" circular>
				<swiper-item v-for="(item, index) in info.listimg" :key="item.id">
					<view class="swiper-item">
						<image class="item-img" :src="showImg(item)" mode="aspectFill"></image>
					</view>
				</swiper-item>
			</swiper>
			
<!-- 			<view class="swiper-pointer">
				<view :class="['cricle',swiperCurrent==i?'active':'']" v-for="(item,i) in info.listimg" :key="i"></view>
			</view> -->
		</view>
		
		<view class="w-full relative">
			<view class="price-box ">
				<view class="flex flex-between" style="font-weight: 500;font-size: 24rpx;color: #666666;">
					<view >
						<text class="present-price">{{ info.price / 100 }}</text>
						 /份
					</view>
					<view>已售{{info.sales_number || 0}}份</view>
				</view>
				
				<view class="title text-overflowRows">{{ info.title }}</view>
				<view class="title text-overflowRows" style="font-weight: 500;font-size: 24rpx;color: #999999;">{{ info.subtitle }}</view>
				<view class="tag no-scrollbar" v-if="info.display_tags">
					<view class="tag-item" v-for="(item, index) in info.display_tags.split(',')" :key="index">
						{{ item }}
					</view>
				</view>
			</view>
			
			<view class="price-box" style="margin-top: 20rpx;">
				<view class="pro-title">产品详情</view>
				<view class="notice" >
					<view class="rich-text" v-html="formateRichText(info.content)"></view>
				</view>
			</view>
			
			
		</view>
		
		<view class="btn-list" >
			<view class="left-box">
				<button id="contact" open-type="contact" bindcontact="handleContact" session-from="sessionFrom">
					<view class="img-box">
						<image src="https://static.ticket.sz-trip.com/uploads/20250611/627d67e48ac41903c40c31f1613f2444.png"
							mode="aspectFill"></image>
						<view class="text">
							客服
						</view>
					</view>
				</button>
				
			</view>
			<view class="btn-post">
				<view class="left-btn-buy" @click="openPop(true)">加入购物车</view>
				<view class="right-btn-buy" @click="openPop(false)">立即购买</view>
			</view>
		</view>

		<!-- 购物车图标 -->
		<view class="add-cart-icon" @click="goCartPage()">
			<uni-badge class="uni-badge-left-margin" :text="cartNum" absolute="rightTop" :offset="[-3, -3]" size="small"
			:custom-style="{background:'#DC2525',color:'#ffffff'}">
			<image src="https://static.ticket.sz-trip.com/uploads/20250611/f8c2078ad76754a0b0251f9b65784dc2.png"></image>
			</uni-badge>
		</view>

		<uni-popup ref="popup" type="bottom" @change="changPopShow" style="position: relative;z-index: 50;">
			<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].headimg)" alt="">
					<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>
				</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.sku_name}}
							</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 style="height: 40rpx;"></view>
			<!-- <view class="btn-box">
				<view class="buy-btn" @click="order">
					下一步
				</view>
			</view> -->
		</uni-popup>
	</view>
</template>

<script>

	import cartDataVue from '../../components/cartData.vue'
	export default {
		components: {cartDataVue},
		data() {
			return {
				swiperCurrent: 0,
				
				id: null,
				info: null,
				sku: [],
				productIndex: 0,
				
				showLength: 0,
				buyNum: 1,
				popShow: false,
				
				cartNum: 0,
				
			};
		},
		onLoad(option) {
			this.id = option.id;
			this.getInfo();
			this.getCartList()
		},
		methods: {
			changPopShow (e) {
				this.popShow = e.show
			},
	
			getInfo() {
				this.Post({id: this.id},
					'/api/product/get_product_detail').then(res => {
					if (res.code !== 1) {
						uni.showToast({
							title: '商品不存在或已下架',
							icon: 'none'
						})
						setTimeout(() => {
							this.goBack()
						}, 2000)
						return
					}
					this.info = res.data;
					this.sku = res.data.sku || []
				});
			},

			//数量加减
			addNumber() {
				this.buyNum += 1;
			},
			delNumber() {
				if (this.buyNum <= 1) {
					return;
				}
				this.buyNum -= 1;
			},
			closePopup() {
				this.$refs.popup.close()
			},
			// false 下单 true 加入购物车
			openPop(flag) {
				if (!this.sku||this.sku.length<=0) {
					uni.showToast({
						title:'暂无可选规格',
						icon:'none'
					})
					return
				}
				
				if (!this.popShow) {
					this.$refs.popup.open()
				} else {
					if (flag) {
						this.addToCart()
					} else {
						this.order()
					}
					
				}
			},
			order(item) {
				let goods = this.sku[this.productIndex]
				goods.buyNum = this.buyNum
				
				let orderInfo = [{
					pInfo: this.info, 
					sInfo: goods,
					// post: 0,
				}]
				
				uni.setStorageSync('teChanOrder', JSON.stringify(orderInfo)); //规格
				uni.navigateTo({
					url: '/subPackages/techan/order'
				});
			},
			addToCart () {
				let goods = this.trueSku[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.$emit("updateDataByConnect", {msgType:'updateCartDataInfo',data:null})
						this.closePopup()
						// this.$refs.cartDataVueRef.openPop()
					}
				});
			},

			changeProduct(item,index) {
				if (item.store==0) {
					uni.showToast({
						title:"库存不足！",
						icon:'none'
					})
					return
				}
				this.productIndex = index
			},
			// 购物车
			getCartList () {
				this.Post({},'/api/cart/get_list').then(res=>{
					this.cartNum = (res.data || []).length
				})
			},
			
			swiperChange (e) {
				this.swiperCurrent = e.detail.current
			},
		},
		onReachBottom() {

		}
	};
</script>

<style lang="scss" scoped>
	.bg {
		min-height: 100vh;
		overflow-x: hidden;
		background: #F7F7F7;;
		padding-bottom: 210rpx;
	}

	view {
		box-sizing: border-box;
	}
	
	.swipe-box {
		height: 413rpx;
		position: relative;
		.swiper {
			height: 413rpx;
			position: relative;
		
			.swiper-item {
				width: 100%;
				height: 413rpx;
		
				.item-img {
					width: 750rpx;
					height: 413rpx;
				}
			}
		}
		
		.swiper-pointer{
			position: absolute;
			right: 10rpx;
			bottom: 40rpx;
			display: flex;
		}
		.cricle{
			width: 14rpx;
			height: 14rpx;
			background: rgba(255,255,255,0.3);
			border-radius: 50%;
			margin-left: 10rpx;
		}
		.cricle.active{
			background: white;
		}
	}
	

	button {
		margin: 0;
		padding: 0;
		outline: none;
		border-radius: 0;
		background-color: transparent;
		line-height: inherit;
	}

	button::after {
		border: none;
	}

	.swipe-box {
		height: 400rpx;
		position: relative;

		.swiper-item-num {
			width: 90rpx;
			height: 40rpx;
			background: rgba(0, 0, 0, 0.5);
			border-radius: 20rpx;
			font-size: 24rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #ffffff;
			text-align: center;
			line-height: 40rpx;
			position: absolute;
			right: 30rpx;
			bottom: 50rpx;
		}
	}

	.swiper {
		height: 400rpx;
		position: relative;

		.swiper-item {
			width: 100%;
			height: 400rpx;

			.item-img {
				width: 750rpx;
				height: 400rpx;
			}
		}
	}
	
	.pro-title{
		font-weight: bold;
		font-size: 35rpx;
		color: #010101;
	}
	
	.price-box {
		width: 100%;
		background: #ffffff;
		padding: 40rpx 24rpx;
		

		.present-price {
			font-size: 42rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #C3282E;
			&:before {
				content: '¥';
				display: inline-block;
				font-size: 26rpx;
			}
		}
		
		.tag {
			margin:24rpx 0 15rpx 0;
			display: flex;
			align-items: center;
			flex-wrap: nowrap;
			overflow-x: auto;

			.tag-item {
				margin-right: 14rpx;
				font-family: PingFangSC;
				padding: 7rpx 14rpx;
				flex-shrink: 0;
				border-radius: 5rpx 4rpx 4rpx 5rpx;
				border: 1px solid #6A8A27;
				font-weight: 500;
				font-size: 24rpx;
				color: #6A8A27;
			}
		}

		.title {
			margin-top: 20rpx;
			font-family: PingFang;
			font-weight: bold;
			font-size: 31rpx;
			color: #000000;
		}
	}
	
	.limit{
		border-top: 1px solid #D8D8D8;
		padding: 27rpx 24rpx;
		width: 100%;
		background: #FFFFFF;
	}
	.notice {
		padding: 35rpx 0;
		width: 100%;
	}

	.tab {
		width: 750rpx;
		height: 88rpx;
		background: #ffffff;
		box-shadow: 0px 1rpx 0px 0px rgba(227, 229, 232, 1);
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 32rpx;
		position: fixed;
		top: 0;

		.tab-item {
			height: 88rpx;
			position: relative;
			line-height: 88rpx;

			.tab-text {
				font-size: 30rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #393b3e;
			}

			.act-text {
				font-size: 30rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #000000;
			}

			.tab-line {
				width: 60rpx;
				height: 6rpx;
				background: #08c59b;
				border-radius: 3rpx;
				position: absolute;
				bottom: 0;
				left: 50%;
				transform: translate(-30rpx, 0);
			}
		}
	}

	.btn-list {
		position: fixed;
		z-index: 9999;
		bottom: 0;
		width: 750rpx;
		height: 140rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(6, 0, 1, 0.1);
		display: flex;
		justify-content: space-between;
		padding: 30rpx 50rpx 0 50rpx;

		.left-box {
			display: flex;
			align-items: flex-start;
			
			.bottom-price{
				display: flex;
				align-items: baseline;
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 27rpx;
				color: #F84A56;
				padding-top: 20rpx;
				.bottom-price-yuan{
					font-size: 40rpx;
				}
			}
			.bottom-detail-icon{
				font-size: 24rpx;
				display: flex;
				align-items: center;
			}
			

			.img-box {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				margin-right: 64rpx;

				image {
					width: 48rpx;
					height: 48rpx;
				}

				.text {
					font-size: 24rpx;
					font-family: PingFangSC;
					font-weight: 400;
					color: #666666;
				}

			}
		}

		.btn-buy {
			width: 293rpx;
			height: 78rpx;
			background: #C3282E;
			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 {
			background: #6A8A27;
			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;
		}
		
	}


.btn-post{
	font-size: 31rpx;
	font-family: PingFangSC;
	font-weight: bold;
	color: #FFFFFF;
	line-height: 60rpx;
	text-align: center;
	display: flex;
	
	.left-btn-buy{
		width: 207rpx;
		height: 60rpx;
		color: #6A8A27;
		background: #D3E8A7;
		border-radius: 11rpx 0rpx 0rpx 11rpx;
	}
	.right-btn-buy{
		width: 207rpx;
		height: 60rpx;
		background: #6A8A27;
		border-radius: 0rpx 11rpx 11rpx 0rpx;
	}
}
	.add-cart-icon{
		width: 80rpx;
		height: 80rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(0,0,0,0.2);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		bottom: 140rpx;
		right: 20rpx;
		z-index: 10;
		image{
			width: 43.33rpx;
			height: 42rpx;
		}
	}

</style>