<template>
	<view>
		<lf-nav :spreadOut="false" :showIcon="true" bgColor="transparent!important"></lf-nav>
		<skeleton :loading="skeletonLoading" :row="12" :showAvatar="false" :showTitle="true">
			<block>
				<!-- 商品图片轮播 -->
				<swiper :current="current" :indicator-dots="2 > 1 ? false : true" :circular="true" class="swiper-box" indicator-active-color="#1998FE">
					<swiper-item v-for="(item, index) in pictures" :key="index">
						<image mode="aspectFill" :src="item" style="width: 100%; height: 100%;" @click="lookImg(index)"></image>
					</swiper-item>
				</swiper>
				<!-- 商品主要信息 -->
				<view class="head-info">
					<view class="lf-row-between">
						<lf-price :iftext="false" :price="111"></lf-price>
						<text class="lf-iconfont icon-fenxiang lf-font-40" @click="shareGraphic()"></text>
					</view>
					<view class="lf-row-between lf-font-24 lf-m-t-20 lf-p-b-20">
						<view class="lf-font-bold lf-font-32 lf-color-222">颐莲喷雾 500ml</view>
					</view>
					<view class="lf-row-between lf-font-24 lf-p-b-30">
						<view class="lf-font-28 lf-color-777">门店自提</view>
					</view>
				</view>
				<self-line></self-line>
				<view class="bg-white">
					<view class="cu-bar padding-lr">
						<view class="lf-flex">
							<text class="lf-color-777 lf-font-28">优惠</text>
							<view v-if="orderDetails.coupon == ''">
								<text class="lf-color-price lf-font-28 lf-m-l-10" v-for="(item,index) of orderDetails.coupon">{{item.tag}}</text>
							</view>
							<view v-else>
								<text class="lf-color-222 lf-font-26 lf-m-l-30">化妆品教师节满1200减200</text>
							</view>
						</view>
						<view>
							<text class="lf-font-24 lf-color-777 lf-iconfont icon-xiangyou"></text>
						</view>
					</view>
				</view>
				<self-line></self-line>
				<view class="bg-white">
					<view class="cu-bar padding-lr">
						<view class="lf-flex">
							<text class="lf-color-777 lf-font-28">服务</text>
							<view style="width: 600rpx;flex-wrap: wrap;display: flex;margin-top: 32rpx;">
								<view class="lf-m-l-30" v-for="i of 5">
									<text class="lf-iconfont icon-xuanzhong lf-font-30" style="color: #15716E;"></text>
									<text class="lf-m-l-10 lf-font-26 lf-color-black">正品保障</text>
								</view>
							</view>
						</view>
						<view>
							<text class="lf-font-24 lf-color-777 lf-iconfont icon-xiangyou"></text>
						</view>
					</view>
				</view>
				<view class="bg-white">
					<view class="cu-bar padding-lr">
						<view class="lf-flex">
							<text class="lf-color-777 lf-font-28">已选</text>
							<text class="lf-color-222 lf-font-26 lf-m-l-30">已选500ml x1</text>
						</view>
						<view>
							<text class="lf-font-24 lf-color-777 lf-iconfont icon-xiangyou"></text>
						</view>
					</view>
				</view>
				<self-line></self-line>
				<view>
					<view class="user-top">
						<view class="lf-row-between lf-w-100">
							<view class="lf-flex">
								<view class="tag-father">
									<image src="https://hainan.lanzulive.com/storage/images/v2-deb89623e0ee2a2dad34bcded6dfd1ed_1440w.png" mode="aspectFill" class="head-img"></image>
								</view>
								<view class="lf-flex-column lf-m-l-20">
									<text class="lf-font-28 lf-color-black lf-font-bold">颐莲官方店</text>
									<view class="lf-font-24 lf-color-777"><u-icon name="map" class="lf-m-r-10"></u-icon>L2</view>
								</view>
							</view>
							<view>
								<button class="head-btn"><u-icon name="chat" class="lf-m-r-10"></u-icon>客服</button>
							</view>
						</view>
					</view>
					<scroll-view class="scroll-view" :scroll-x="true">
						<view class="scroll-content">
							<view class="goods-item" v-for="(item, index) in 5" :key="index">
								<image class="goods-img"></image>
								<view class="goods-info">
									<view class="lf-line-1">猪大肠卖咯 5毛钱一斤咯</view>
									<view>￥0.5</view>
								</view>
							</view>
							<view class="goods-item">
								<view class="more-box">
									<text>查看更多</text>
									<text class="lf-iconfont icon--2 lf-text-vertical"></text>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
				<!-- 商品详情 -->
				<view class="goods-detail">
					<view class="lf-font-32 lf-font-bold lf-m-b-20">商品详情</view>
					<rich-text :nodes="formatRichText('商品详情商品详情商品详情商品详情商品详情商品详情商品详情商品详情商品详情商品详情商品详情商品详情')"></rich-text>
					<!-- <image class="goods-img" :src="item" v-for="(item, index) in goods_detail.content" :key="index" v-if="goods_detail.content_type == 'img'"></image> -->
				</view> 
				<!-- 修饰专用 -->
				<view class="extra"></view>
				<!-- 吸底操作按钮 -->
				<view class="lf-row-between fixed-bottom">
					<view class="lf-flex lf-p-t-10 lf-p-b-10">
						<view class="lf-flex-column lf-row-center icon-item" open-type="contact">
							<text class="lf-iconfont icon-shoucang icon-img"></text>
							<view class="lf-m-t-1">客服</view>
						</view>
						<view class="lf-flex-column lf-row-center icon-item" @click="switchCollect">
							<text class="lf-iconfont icon-shoucang icon-img" v-if="is_collect"></text>
							<text class="lf-iconfont icon-shoucang icon-img" v-else></text>
							<view class="lf-m-t-1">{{ is_collect ? '已收藏' : '收藏' }}</view>
						</view>
						<button class="lf-flex-column lf-row-center icon-item">
							<text class="lf-iconfont icon-shoucang icon-img"></text>
							<view class="lf-m-t-1">购物车</view>
						</button>
					</view>
					<view class="lf-flex">
						<button class="btn1" @click="toAddOrder">加入购物车</button>
						<button class="btn lf-m-l-15" @click="toAddOrder">立即购买</button>
					</view>
				</view>
				<!-- 回到顶部 -->
				<!-- <u-back-top :scroll-top="pageScrollTop" :custom-style="{background: 'rgba(51, 51 51, 0.3)'}" :icon-style="{color: '#ffffff'}"></u-back-top> -->
				<u-back-top :scroll-top="pageScrollTop" :custom-style="{background: 'rgba(51, 51 51, 0.3)'}"></u-back-top>
			</block>
		</skeleton>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				current: 0, // 轮播下标
				goods_id: 0,
				goods_detail: {},
				is_collect: false, // 1为当前收藏商品了，0为否
				skeletonLoading: false,
				pictures: [
					'https://hainan.lanzulive.com/storage/images/v2-deb89623e0ee2a2dad34bcded6dfd1ed_1440w.png',
					'https://hainan.lanzulive.com/storage/images/v2-deb89623e0ee2a2dad34bcded6dfd1ed_1440w.png',
					'https://hainan.lanzulive.com/storage/images/v2-deb89623e0ee2a2dad34bcded6dfd1ed_1440w.png'
				]
			}
		},
		onLoad(options){
			this.goods_id = options.goods_id;
			
			console.log('商品',this.goods_id)
			// this.getGoodsDetail();
		},
		computed: {
			isRight() {
				return function(val) {
					return this.$shared.isRight(val);
				}
			}
		},
		methods: {
			shareGraphic(){
				this.getCreateShareImg();
			},
			//请求h5的图片接口
			getCreateShareImg(){
				wx.showLoading({
					title:"生成中",
					mask:true
				})
				    let origin = window.location.origin;
					let share_url = origin+'/pages/store/detail/detail';
				this.$http.get({
					api:'api/distribution/createH5ShareImg',
					data:{
						goods_id:this.id,
						share_url:share_url 
					}
				}).then(res=>{
					res = res.data;
					if(res.status){
						this.qr_code_image = res.data.image;
			
						if(this.qr_code_image){
							this.share_gaphic =!this.share_gaphic;
							this.is_toggle_share = false;
						}
					} else{
						wx.showModal({
							content: res.message || '请求失败，请重试',
							showCancel: false
						});
					}
					wx.hideLoading()
				}).catch(rej=>{
					wx.hideLoading()
					wx.showModal({
						content: rej.message || '内部错误，请重试',
						showCancel: false
					});
				})
				
			},
			getGoodsDetail(){
				this.$http(this.API.API_ADVICEDETAILS, {id: this.goods_id}).then(res => {
					this.skeletonLoading = false;
					this.goods_detail = res.data;
					this.is_collect = Boolean(res.data.is_collect) || false;
				}).catch(err => {
					this.skeletonLoading = false;
					setTimeout(() => {
						this.$toBack();
					}, 1000);
				})
			},
			// 切换商品收藏
			switchCollect(){
				let userInfo = uni.getStorageSync('userinfo') || {};
				if(!userInfo.id || !userInfo.nickname || !userInfo.avatar){
					this.$url('/pages/login/index?type=userinfo');
					return;
				}
				
				if(this.is_collect) {
					this.$http(this.API.API_DELCOLLECT, {agent_product_id:this.goods_id}).then(res => {
						this.$msg('取消收藏成功!')
						this.is_collect = false
						console.log(res)
					})
				}else {
					this.$http(this.API.API_ADDCOLLECT, {agent_product_id:this.goods_id}).then(res => {
						this.$msg('添加收藏成功!')
						this.is_collect = true
						console.log(res)
					})
				}
			},
			// 拨打电话
			makePhoneCall(phoneStr){
				uni.makePhoneCall({
					phoneNumber: String(phoneStr)
				})
			},
			// 打开地图
			openMap(){
				let { address, lat, lng } = this.goods_detail?.store || {};
				uni.openLocation({
				  longitude: Number(lat),
				  latitude: Number(lng),
				  scale: 20,
				  name: address
			   });
			},
			// 跳转到确认下单页面
			toAddOrder(){
				let goods_id = this.goods_detail.id;
				this.$url('/pages/order/confirm_order?goods_id='+ goods_id);
			},
			// 预览图片
			lookImg(index){
				this.$u.throttle(() => {
					let goods_banner = this.goods_detail.banners || [];
					let banners = goods_banner.map(item => item.cover);
					uni.previewImage({
						urls: banners,
						current: index
					})
				}, 200);
			},
			// 富文本处理
			formatRichText(richText){
			  if(richText != null){
					let newRichText= richText.replace(/<img[^>]*>/gi, function(match, capture){
						match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
						match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
						match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
						return match;
					});
					newRichText = newRichText.replace(/style="[^"]+"/gi,function(match, capture){
						match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
						return match;
					});
					newRichText = newRichText.replace(/<br[^>]*\/>/gi, '');
					newRichText = newRichText.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin:10px 0;"');
					return newRichText;
				}else{
					return null;
				}
			}
		},
		onShareAppMessage(){
			let goods = this.goods_detail;
			let title = goods.name;
			let imageUrl = goods.share_cover || goods.cover;
			let path = '/pages/route/index?route=goods_detail&id='+ goods.id;
			
			return {
				title,
				path,
				imageUrl
			}
		}
	}
</script>

<style>
	page{
		background-color: #f5f5f5;
		overflow-x: hidden;
	}
</style>
<style lang="scss" scoped="scoped">
	//商品上去scrollview部分
	.scroll-view{
		height: 310rpx;
		width: 100%;
		// margin-top: 20rpx;
		padding: 0 30rpx 30rpx 30rpx;
		background-color: white;
		.scroll-content{
			display: flex;
			width: 100%;
			.goods-item{
				margin-right: 15rpx;
				width: 180rpx;
			}
			.goods-img{
				width: 180rpx;
				height: 180rpx;
				background-color: #EEEEEE;
			}
			.goods-info{
				text-align: center;
				color: #222222;
				&>view:nth-child(1n){
					font-size: 24rpx;
				}
				&>view:nth-child(2n){
					font-size: 28rpx;
					font-weight: bold;
					text-align: left;
					color: #F63434;
				}
			}
			.more-box{
				width: 180rpx;
				height: 180rpx;
				background-color: #EEEEEE;
				text-align: center;
				line-height: 180rpx;
				font-size: 24rpx;
			}
		}
	}
	
	//scrollview结束
	.tag-father {
		position: relative;
	}
	.head-tag {
		color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		text-align: center;
		font-size: 24rpx;
		width: 36rpx;
		height: 36rpx;
		border-radius: 50%;
		background-color: #15716E;
		border: 1rpx solid #FFFFFF;
		position: absolute;
		left: 76rpx;
		top: 86rpx;
		z-index: 99;
	}
	.qzone-img {
		width: 220rpx;
		height: 220rpx;
		border-radius: 10rpx;
		margin-right: 12rpx;
		&:nth-child(3n) {
			margin-right: 0;
		}
		&:nth-child(n + 4) {
			margin-top: 12rpx;
		}
	}
	.user-top {
		background-color: #F3F8F8;
		padding: 30rpx;
		display: flex;
		align-items: center;
		background-color: white;
	}
	.head-img {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
	}
	.head-btn {
		display: flex;
		width: max-content;
		height: 45rpx;
		background: white;
		border: 2rpx solid #15716E;
		align-items: center;
		font-size: 22rpx;
		color: #15716E;
		justify-content: space-between;
		border-radius: 35rpx;
		padding: 10rpx!important;
	}
	.swiper-box{
		width: 750rpx;
		height: 750rpx;
		background-color: #FFFFFF;
	}
	
	.head-info{
		width: 750rpx;
		height: auto;
		box-sizing: border-box;
		padding: 0 32rpx;
		padding-top: 20rpx;
		background-color: #FFFFFF;
		// .price>view:nth-of-type(1){
		// 	color: #FF0000;
		// 	margin-right: 22rpx;
		// 	font-weight: bold;
		// }
		.price>view:nth-of-type(1){
			text-decoration: line-through;
			color: #777777;
			margin-right: 22rpx;
		}
		.price>view:nth-of-type(2){
			width: max-content;
			padding: 0 18rpx;
			height: 46rpx;
			background-color: #1998FE;
			border-radius: 10rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #FFFFFF;
		}
		.label-box{
			min-height: 130rpx;
			width: 100%;
			border-top: 1rpx solid #E5E5E5;
			display: flex;
			flex-wrap: wrap;
			padding: 30rpx 0 10rpx 0;
			.label-item{
				width: max-content;
				padding: 20rpx;
				height: 70rpx;
				border-radius: 10rpx;
				border: 2rpx solid #1998FE;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 28rpx;
				color: #1998FE;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
			}
		}
	}
	
	.address-box{
		width: 750rpx;
		height: auto;
		box-sizing: border-box;
		background-color: #FFFFFF;
		padding: 32rpx;
		margin-top: 20rpx;
		.shop-img{
			width: 60rpx;
			height: 60rpx;
			border-radius: 50%;
		}
	}
	
	.goods-detail{
		width: 750rpx;
		height: auto;
		background-color: #FFFFFF;
		padding: 32rpx;
		box-sizing: border-box;
		margin-top: 20rpx;
		.goods-img{
			width: 100%;
		}
	}
	
	.extra{
		width: 100%;
		height: 110rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-sizing: content-box;
	}
	
	.fixed-bottom{
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #FFFFFF;
		width: 100%;
		height: auto;
		padding: 0 32rpx;
		border-top: 1rpx solid #e5e5e5;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		.icon-item{
			text-align: center;
			margin-right: 16rpx;
			background-color: transparent;
			margin: 0;
			line-height: initial;
			font-size: 28rpx;
			font-weight: inherit;
			margin-right: 10rpx;
			padding: 0;
			width: 88rpx;
			position: relative;
			&:first-child{
				padding-left: 0;
			}
			.icon-img{
				height: 50rpx;
				width: 50rpx;
			}
		}
		.btn1{
			margin: 0;
			width: max-content;
			height: 80rpx;
			background-color: rgba(21, 113, 110, 0.1);
			color: #15716E;
			line-height: 80rpx;
			font-size: 32rpx;
			padding: 0 20rpx;
			border-radius: 42rpx;
			font-size: 26rpx;
			border: 2rpx solid #15716E;
		}
		.btn{
			margin: 0;
			padding: 0 20rpx;
			width: max-content;
			height: 80rpx;
			background-color: #15716E;
			color: #FFFFFF;
			line-height: 80rpx;
			font-size: 26rpx;
			border-radius: 42rpx;
		}
	}
</style>