<template>
	<view class="">
		<view class="content" v-cloak>
			<view class="shop-banner">
				<view class="shop-banner" @click="showImg()">
					<swiper class="swiper" :indicator-dots="false" :circular="true" :autoplay="true" :interval="3000"
						:duration="1000" indicator-color="rgba(255, 255, 255, 0.6)" indicator-active-color="#ffffff"
						@change="swiperChange">
						<swiper-item class="swiper-item" v-for="(item,index) in  banner" :key="index">
							<image :src="item" mode="aspectFill"></image>
						</swiper-item>
					</swiper>
					<!-- 底部遮罩 -->
					<view class="banner-mask"></view>
					<!-- 数量 -->
					<view class="num-box">{{ activeBanner }}/{{ banner.length }}</view>
				</view>
			</view>
			<!-- 详细信息 -->
			<view class="det-info">
				<view class="price-box">
					<view class="price">
						<text class="label">会员价</text>
						<text class="unit">￥</text>
						<text class="num">{{goosDetail.price}}</text>
						<text class="fs24 color-text2">零售价</text>
						<text class="old">¥{{goosDetail.otPrice}}</text>
					</view>
					<text class="fs24 color-text2">月售{{goosDetail.sales}}件</text>
				</view>
				<view class="name-box">
					{{goosDetail.productName}}
				</view>

			</view>
		</view>
		<view class="guige">
			<view class="safe-box">
				<text class="text">服务</text>
				<view class="box">
					<image class="mr20" src="/static/images/safe.png" mode=""></image>
					<view class="mr30" v-for="(item,index) in serviceList" :key="index">
						<text>{{item}}</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 购买人数、库存 -->
		<view class="det-box">
			<view class="title">图文详情</view>
			<view class="inner">
				<view v-html="goosDetail.description" style="font-size:0"></view>
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="btn-foot">
			<view class="menu-box">
			</view>
			<view class="btn-box">
				<view class="btn buy" @click="addCart('buy')">{{buyText}}</view>
			</view>
		</view>

		<!-- 选择药品规格弹窗 -->
		<popupBottom ref="popup" :visible.sync="specVisible" title=" " radius="32" maxHeight="800">
			<view class="product-spec">
				<view class="pro-info">
					<view class="img-box">
						<image :src="goosDetail.image ||'/static/images/img.png'" mode="aspectFill"></image>
					</view>
					<view class="info-text">
						<view class="info-title">{{goosDetail.productName}}</view>
						<view class="price">
							<view class="label">会员价</view>
							<text class="unit">¥</text>
							<text class="num">{{ goosDetail.price}}</text>
						</view>
						<view class="desc-box">
							<text class="text">月售{{goosDetail.sales}}件</text>
						</view>
					</view>
				</view>

				<view class="price-num">
					<view class="label">数量</view>
					<u-number-box bgColor="#f3f3f3" v-model="goodsNum" @change="goodsNumChange"></u-number-box>
				</view>
				<view class="sub-btn" @click="submit">确定</view>
			</view>
		</popupBottom>
	</view>
</template>

<script>
	import {
		liveGoodsDetail
	} from '@/api/living'
	import {
		liveOrderKey, // 生成订单key
	} from "@/api/order.js"
	import popupBottom from '@/components/px-popup-bottom/px-popup-bottom.vue'

	export default {
		components: {
			popupBottom
		},
		data() {
			return {
				banner: [],
				goodsId: null,
				totalNum: 1,
				orderKey: null,
				type: null,
				liveId: null,
				storeId: null,
				serviceList: ['品质保障', '药师服务', '隐私保护'],
				productId: null,
				goosDetail: {}, //商品详情
				loadding: true,
				buyText: "立即购买",
				goodsNum: 0, //商品选择数量
				attrs: [],
				values: [],
				stores: [],
				storeNames: [],
				storeIdx: 0,
				storeName: "",
				product: {
					price: 0,
					otPrice: 0,
				},
				showModal: false,
				// 当前轮播的图片
				activeBanner: 1,
				// 购物车数量
				cartCount: 0,
				// 规格弹窗
				specVisible: false,
				// // 规格数量
				// specNum: 1,
				config: null,
				showServiceFee: false,
				selectVal: "",
				// 链接带的storeId
				urlStoreId: undefined,
				// 所选店铺
				storeSelectInfo: {},
				// 保存选的规格
				choseSpecSubIndex: 0,
				choseSpecIndex: 0,
			};
		},
		onLoad(options) {
			console.log("商品详情options", options)
			if (options.productId) {
				this.productId = options.productId;
			}
			this.liveId = options.liveId
			this.goodsId = options.goodsId

			if (options.storeId) {
				this.storeId = options.storeId || ""

			} else {
				uni.showToast({
					title: "storeId不存在~",
					icon: "none"
				})
			}
		},
		mounted() {
			this.getliveGoods()
			var userInfo = uni.getStorageSync("userInfo")

		},
		methods: {
			// swiper变化事件
			swiperChange(event) {
				this.activeBanner = event.detail.current + 1
			},
			doAddCart(type) {
				if (this.totalNum == 0) {
					uni.showToast({
						icon: 'none',
						title: "库存不足",
					});
					return;
				}
				var isBuy = type == "buy" ? 1 : 0;
				if (type == "buy") {
					this.getKey()
				} else {
					this.getCartCount()
					uni.showToast({
						icon: 'success',
						title: "添加成功",
					});
				}

			},
			
			// 获得key
			getKey() {
				liveOrderKey().then(res => {
						if (res.code == 200) {
							console.log("下订单的key>>>>", res)
							this.orderKey = res.orderKey
							console.log("key>>>>", this.orderKey)
							uni.navigateTo({
								url: '/pages_shopping/live/confirmCreateOrder?&orderKey=' + this.orderKey +
									'&liveId=' + this.liveId + '&goodsId=' + this.goodsId +
									'&productId=' + this.productId + '&totalNum=' + this
									.totalNum
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);
			},
			// 选择商品数量
			goodsNumChange(e) {
				console.log('当前选择商品数量为: ' + e.value)
				this.totalNum = e.value
			},
			// 提交
			submit() {
				this.specVisible = false
				this.doAddCart(this.type);
			},
			// 加入购物车
			addCart(type) {
				this.type = type;
				this.specVisible = true
			},


			// 跳转页面
			navgetTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			// openEditMoney() {
			// 	this.editShow = true;
			// },
			// cancelEditMoney(){
			// 	this.editShow = false;
			// },
			//商品详情
			getliveGoods() {
				if (!this.productId) return;
				uni.showLoading({
					title: '加载中'
				});
				liveGoodsDetail(this.productId).then(res => {
						uni.hideLoading()
						if (res.code == 200) {
							// console.log("小黄车 商品详情>>>>", res)
							this.goosDetail = res.data
							this.banner = res.data.sliderImage.split(',');
							// console.log("轮播图",this.banner)

						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					rej => {}
				);
			},
		}
	}
</script>

<style lang="scss">
	  [v-cloak] {
	    display: none;
	  }
	.content {
		font-family: PingFang SC;
	}
	.shop-banner {
		height: 756rpx;
		background-color: #FFFFFF;
		position: relative;

		.swiper-item {
			box-sizing: border-box;
			position: relative;
		}

		.swiper,
		.swiper-item,
		.swiper-item image {
			width: 100%;
			height: 100%;
		}

		.banner-mask {
			width: 100%;
			height: 44rpx;
			position: absolute;
			left: 0;
			bottom: 0;
			z-index: 9;
			background-size: 20rpx 44rpx;
			background-repeat: repeat-x;
		}

		.num-box {
			width: 80rpx;
			height: 40rpx;
			line-height: 40rpx;
			text-align: center;
			font-size: 24rpx;
			color: #FFFFFF;
			background: rgba(0, 0, 0, .7);
			border-radius: 20rpx;
			position: absolute;
			right: 40rpx;
			bottom: 34rpx;
			z-index: 10;
		}

		.cf-box {
			position: absolute;
			z-index: 10;
			left: 0;
			right: 0;
			top: calc(50% - 200rpx);
			bottom: calc(50% - 200rpx);
			background-color: rgba(0, 0, 0, 0.3);
			backdrop-filter: blur(2rpx);
			/* 背景模糊度 */
			display: flex;
			flex-direction: column;
			flex: 1;
			justify-content: center;
			align-items: center;
			color: #EDEEEF;

			.title {
				font-size: 40rpx;
				font-weight: bold;
			}

			.subTitle {
				font-size: 28rpx;
				font-weight: bold;
				margin-top: 10rpx;
			}
		}


	}

	.guige {
		padding: 24rpx;
		border-radius: 16rpx;
		background: #fff;
		width: auto;
		font-size: 24rpx;
		color: #222426;
		margin: 24rpx;

		.guige-gg {
			.gg-text {
				color: #898E91;
				margin-right: 40rpx;
			}
		}

		.safe-box {
			display: flex;
			align-items: center;
			padding-top: 24rpx;
			font-size: 24rpx;
			color: #222426;

			.text {
				color: #999999;
				margin-right: 40rpx;
			}

			.box {
				display: flex;
				align-items: center;

				image {
					width: 28rpx;
					height: 28rpx;
					margin-right: 10rpx;
				}

				view {
					display: flex;
					align-items: center;
					margin-right: 40rpx;

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

						image {
							margin-right: 0;
						}
					}
				}
			}

		}

	}

	.det-info {
		background: #FFFFFF;
		padding: 24rpx;
		margin: 24rpx;
		border-radius: 16rpx;

		.price-box {
			display: flex;
			align-items: flex-end;
			justify-content: space-between;

			.price {
				display: flex;
				align-items: flex-end;

				.label {
					font-weight: 500;
					font-size: 24rpx;
					color: #FF5030;
					line-height: 1.3;
					margin-right: 10rpx;
				}

				.unit {
					font-size: 26rpx;
					font-weight: bold;
					color: #FF6633;
					line-height: 1.3;
				}

				.num {
					font-size: 48rpx;
					font-weight: bold;
					color: #FF5030;
					margin-right: 20rpx;
					line-height: 1;
				}

				.old {
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 400;
					text-decoration: line-through;
					color: #898E91;
					margin-left: 10rpx;
					line-height: 1.3;
				}
			}

			
			.spec {
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
				line-height: 36rpx;
			}
		}

		.name-box {
			font-size: 32rpx;
			font-weight: bold;
			color: #111111;
			line-height: 44rpx;
			margin-top: 32rpx;

			.tag {
				display: inline-block;
				padding: 2rpx 8rpx;
				height: 32rpx;
				background: #F5A623;
				border-radius: 4rpx;
				margin-right: 10rpx;
				font-weight: 400;
				font-size: 20rpx;
				color: #FFFFFF;
				line-height: 30rpx;
				float: left;
				margin-top: 7rpx;
			}
		}

		.intro {
			font-size: 26rpx;
			font-weight: 500;
			color: #999999;
			line-height: 36rpx;
			padding: 18rpx 0 23rpx;
		}
		.safe-box {
			display: flex;
			align-items: center;
			padding-top: 24rpx;

			image {
				width: 20rpx;
				height: 24rpx;
				margin-right: 20rpx;
			}

			.text {
				font-size: 22rpx;
				font-weight: 500;
				color: #999999;
				line-height: 1;
			}

			.line {
				width: 1px;
				height: 23rpx;
				background: #EDEEEF;
				margin: 0 20rpx;
			}
		}
	}

	.det-box {
		margin-top: 10rpx;
		background-color: #FFFFFF;
		padding: 24rpx;
		margin: 24rpx 24rpx 175rpx 24rpx;
		border-radius: 16rpx;

		.title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
			line-height: 60rpx;
			margin-bottom: 30rpx;
			padding-bottom: 24rpx;
			border-bottom: 1px solid #ECECEC;
		}

		.inner {
			margin-bottom: 100rpx;
		}
	}

	.btn-foot {
		box-sizing: border-box;
		width: 100%;
		height: 151rpx;
		background: #FFFFFF;
		padding: 0 24rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;

		.menu-box {
			display: flex;
			align-items: center;

			.item {
				display: flex;
				align-items: center;
				flex-direction: column;
				margin-right: 48rpx;

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

				image {
					width: 40rpx;
					height: 40rpx;
					margin-bottom: 10rpx;
				}

				.label {
					font-size: 20rpx;
					font-weight: 500;
					color: #626468;
					text-align: center;
				}
			}
		}

		.btn-box {
			display: flex;
			align-items: center;

			.btn {
				width: 200rpx;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				border-radius: 44rpx;
				margin-left: 20rpx;
				font-size: 28rpx;
				font-weight: bold;
				color: #FFFFFF;

				&:first-child {
					margin-left: 0;
				}

				&.cart {
					background: #FF5030;
				}

				&.buy {
					background: #2bc7b9;
				}
			}
		}
	}

	.product-spec {
		padding-bottom: 30rpx;

		.pro-info {
			display: flex;
			align-items: center;

			.img-box {
				width: 200rpx;
				height: 200rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				overflow: hidden;
				margin-right: 30rpx;

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

			.info-text {
				height: 200rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.info-title {
					font-family: PingFang SC;
					font-weight: 600;
					font-size: 28rpx;
					color: #222426;
					text-align: left;
				}

				.price {
					display: flex;
					align-items: flex-end;

					.label {
						font-weight: 500;
						font-size: 24rpx;
						color: #FF5030;
						line-height: 1.3;
						margin-right: 10rpx;
					}

					.unit {
						font-size: 32rpx;
						font-weight: bold;
						color: #FF6633;
						line-height: 1.2;
						margin-right: 10rpx;
					}

					.num {
						font-size: 50rpx;
						font-weight: bold;
						color: #FF6633;
						line-height: 1;
					}
				}

				.desc-box {
					display: flex;
					flex-direction: column;
					padding-bottom: 9rpx;

					.text {
						font-size: 26rpx;
						font-weight: 500;
						color: #999999;
						margin-top: 27rpx;
						line-height: 1;

						&:first-child {
							margin-top: 0;
						}
					}
				}
			}
		}


		.price-num {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 30rpx;

			.label {
				font-size: 36rpx;
				font-weight: bold;
				color: #111111;
			}
		}

		.sub-btn {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
			background: #2BC7B9;
			border-radius: 44rpx;
			margin-top: 30rpx;
		}
	}
	.loadding {
		background-color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 9999;

		image {
			border-radius: 50%;
			animation: load linear 1s infinite;
			width: 120rpx;
			height: 120rpx;
		}

		.text {
			font-size: 28rpx;
			margin-top: 20rpx;
		}
	}
</style>