<template>
	<view>
		<u-navbar title="商品详情" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<view class="detail">
			<!-- 商品轮播 -->
			<view class="detail-swiper">
				<u-swiper :list="picUrlArr" height="375" radius="0" :indicator="true" :circular="true" indicatorMode="dot" indicatorActiveColor="#FA822C"></u-swiper>
			</view>
			<!-- 详情 -->
			<view class="detail-info flex">
				<view class="detail-info-left">
					<view class="detail-info-left__title ells-one">{{ info.title }}</view>
					<view class="detail-info-left__coin">
						<view class="content flex">
							<image src="../../static/public/goods_coin.png" mode=""></image>
							<view>× {{ info.exchangePrice }}</view>
						</view>
						<view class="txt" v-if="info.originPrice">原价：<text>{{ info.originPrice }}</text></view>
					</view>
					<view class="detail-info-left__price">￥{{ $numberFormat(info.value) }}</view>
				</view>
				<view class="detail-info-right" v-show="false">销量：30个</view>
			</view>
			<view class="detail-goods">产品介绍</view>
			<view class="detail-description">
				<u-parse :content="description" :selectable="true"></u-parse>
			</view>
			<view style="detail-merchant" @click="toCompanyData" v-if="info.merchantInfo?true:false">
				<view class="detail-merchant-warp">
					<view class="detail-merchant-warp-one">商家信息</view>
					<view class="detail-merchant-warp-two">
						<view style="float: left;">前往查看</view>
						<u-icon style="float: right;" name="arrow-right" size="18"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<view class="footer-fixed">
			<view class="footer-fixed-content flex">
				<!-- <button type="default">立即兑换</button> -->
				<view class="footer-fixed-content__coin flex">
					<text>我的盲豆:</text>
					<text>{{ initData.coinNum ? `${ initData.coinNum }个` : '--' }}</text>
				</view>
				<view class="footer-fixed-content__exchange flex" @click="exChange">
					<text>{{ info.exchangePrice }}个</text>
					<text>盲豆兑换</text>
				</view>
				<view class="footer-fixed-content__price flex" @click="purchase">
					<text>立即购买</text>
				</view>
			</view>
		</view>

		<!-- 兑换选择 -->
		
		
		<!-- 兑换成功 -->
		<u-popup :show="tipShow" mode="center">
			<view class="tip-show">
				<view class="flex tip-show-title">
					<u-icon name="checkmark-circle" color="#EB7009" size="24"></u-icon>
					<text>兑换成功，已放入仓库</text>
				</view>
				<view class="flex tip-show-btn">
					<view class="close" @click="tipShow = false">关闭</view>
					<navigator class="prize" :url="`/packagePrize/prize/index`" hover-class="navigator-hover"
						@click="tipShow = false">前往查看</navigator>
				</view>
			</view>
		</u-popup>
		
		<!-- 商品兑换 -->
		<exchange-popop 
			:popup-show="exchangePopupShow" 
			:detailInfo="info" 
			:popup-info="payInfo" 
			:sku-list-init="skuListInit" 
			:sku-list-popup="skuList" 
			@close="close"
			@success="exchangeSuccess"
			v-if="exchangePopupShow" 
		/>
		
		<!-- 商品购买 -->
		<purchase-popup
			:popup-show="purchasePopupShow" 
			:detailInfo="info" 
			:popup-info="payInfo" 
			:sku-list-init="skuListInit" 
			:sku-list-popup="skuList" 
			@close="close"
			@success="purchaseSuccess"
			v-if="purchasePopupShow" 
		/>
		
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	import { formatRichText } from '@/utils/util.js'
	import ExchangePopop from '../components/exchange-popup/exchange-popup.vue'
	import PurchasePopup from '../components/purchase-popup/purchase-popup.vue'
	export default {
		components: {
			ExchangePopop,
			PurchasePopup
		},
		data() {
			return {
				goodsId: '',
				boxId: '',
				picUrlArr: [],
				info: {},
				prizeList: [],
				description: '',
				choiceShow: false,
				initData: {},
				orderNum: 1,
				skuList: [],
				skuListInit: [],
				payInfo: {},
				tipShow: false,
				
				exchangePopupShow: false,
				purchasePopupShow: false
			};
		},
		onLoad(opthios) {
			this.goodsId = opthios.id
		},
		onShow() {
			this.getDetail()
			if(uni.getStorageSync('token')) {
				this.getBean()
			}
		},
		methods: {
			getDetail() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/exchange/goods/detail', {
					goodsId: this.goodsId,
					noToken: true
				}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						this.info = res.data
						let picUrlArr = res.data.picUrl.split(',')
						picUrlArr.forEach(item => {
							this.picUrlArr.push(env.filePublic + item + '?imageView2/2/w/750')
						})
						// 处理富文本
						const description = res.data.description.replaceAll(".jpg\"", ".jpg?imageView2/2/w/750\"")
							.replaceAll(".jpeg\"", ".jpeg?imageView2/2/w/750\"").replaceAll(".png\"",
								".png?imageView2/2/w/750\"");
						this.description = formatRichText(description);
						this.skuListInit = res.data.skuList
						if (res.data.skuList.length) {
							let skuProp = JSON.parse(res.data.skuProp)
							skuProp.forEach(item => {
								item.actionIndex = 0,
									item.txt = `${item.name}:${item.value[0]}`
							})
							let actionSku = skuProp.map(item => {
								return item.txt
							}).join(';')
							let sku = res.data.skuList.find(item => {
								return item.properties == actionSku
							})
							this.payInfo = {
								...sku,
								exValue: sku.exchangePrice,
								price: sku.value,
								picUrl: env.filePublic + sku.picUrl
							}
							this.skuList = skuProp
						} else {
							this.payInfo = {
								...res.data,
								price: this.info.value,
								exValue: this.info.exchangePrice,
								picUrl: this.picUrlArr[0]
							}
						}
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			getBean() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/mine/init', {}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						this.initData = res.data
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			exChange() {
				this.exchangePopupShow = true
				this.getBean()
			},
			
			purchase() {
				this.purchasePopupShow = true
			},
			
			close() {
				this.exchangePopupShow = false
				this.purchasePopupShow = false
			},
			
			exchangeSuccess() {
				this.close()
				this.tipShow = true
				this.getBean()
				this.getDetail()
			},
			
			purchaseSuccess() {
				this.close()
			},

			toCompanyData(){
				uni.navigateTo({
					url:`/packageGoods/goods/company?goodsId=${ this.goodsId }`
				})
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>
<style lang="scss" scoped>
	.detail {
		padding-bottom: 180rpx;

		// 商品轮播
		&-swiper {
			background-color: #FFFFFF;
		}

		// 详情
		&-info {
			background-color: #fff;
			justify-content: space-between;
			padding: 34rpx;
			margin-bottom: 22rpx;
			
			&-left {
				flex: 1;
				
				&__title {
					font-size: 34rpx;
					line-height: 34rpx;
					height: 34rpx;
					overflow: hidden;
					margin-bottom: 26rpx;
					font-weight: bold;
				}
				
				&__coin {
					display: flex;
					line-height: 30rpx;
					margin-bottom: 26rpx;
					font-size: 26rpx;
					
					.content {
						color: #FF2929;
						margin-right: 34rpx;
						
						image {
							width: 34rpx;
							height: 30rpx;
						}
					}
					
					.txt {
						color: #666666;
						
						text {
							text-decoration: line-through;
						}
					}
				}
				
				&__price {
					line-height: 24rpx;
					color: #666666;
				}
			}
			
			&-right {
				color: #666666;
			}
		}

		&-goods {
			height: 90rpx;
			text-align: center;
			line-height: 90rpx;
			font-weight: bold;
			font-size: 30rpx;
			background-color: #FFFFFF;
		}

		&-merchant {
			height: 88rpx;
			// text-align: center;
			line-height: 88rpx;
			font-weight: bold;
			background-color: #FFFFFF;

			&-warp {
				height: 88rpx;
				width: 100%;
				background-color: #ffffff;
				// margin-top: 10rpx;

				&-one {
					float: left;
					margin: 20rpx;
					font-weight: 600;
				}

				&-two {
					float: right;
					margin: 20rpx;
					font-weight: 600;
				}
			}
		}

		&-description {
			image {
				width: 100%;
			}
		}
	}

	.tip-show {
		width: 80vw;
		background-color: #FFFFFF;
		border-radius: 10rpx;
		padding: 60rpx 40rpx;

		&-title {
			margin-bottom: 64rpx;

			text {
				margin-left: 20rpx;
			}
		}

		&-btn {
			justify-content: space-around;

			.close {
				width: 160rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 8rpx;
				color: rgba(235, 112, 9, 100);
				font-size: 28rpx;
				text-align: center;
				border: 1px solid rgba(235, 112, 9, 100);
			}

			.prize {
				width: 160rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 8rpx;
				color: rgba(235, 112, 9, 100);
				font-size: 28rpx;
				text-align: center;
				background-color: rgba(235, 112, 9, 100);
				color: rgba(255, 255, 255, 100);
				font-size: 14px;
			}
		}
	}

	.footer-fixed {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		z-index: 11;
		box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
		background: #fff;
		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		&-content {
			justify-content: flex-start;
			height: 132rpx;
			background-color: #fff;
			
			&__coin {
				height: 100%;
				flex: 1;
				font-size: 30rpx;
				
				text:first-child {
					color: #999;
				}
			}
			
			&__exchange {
				height: 100%;
				flex-direction: column;
				padding: 0 50rpx;
				background-color: #FA822C;
				color: #fff;
				
				text {
					font-weight: normal;
					font-size: 26rpx;
				}
				
				text:last-child {
					
					font-weight: bold;
					font-size: 30rpx;
					line-height: 40rpx;
				}
			}
			
			&__price {
				font-size: 30rpx;
				font-weight: bold;
				height: 100%;
				padding: 0 50rpx;
				background-color: #FA362C;
				color: #fff;
			}
		}
	}
</style>
