<template>
	<view>
		<u-navbar title="商品详情" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<view class="detail" style="margin-bottom: 90rpx;">
			<view class="detail-top">
				<u-swiper :list="picUrlArr" height="375" radius="0" :indicator="true" :circular="true"></u-swiper>
			</view>
			<view class="detail-title">
				<view class="txt">{{ info.title }}</view>
				<view class="coin">
					<image src="../../static/icon/bean.png" mode=""></image>
					<view>× {{ info.exchangePrice }}</view>
				</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="flex btn">
				<button type="default" @click="exChange">立即兑换</button>
			</view>
		</view>

		<!-- 兑换选择 -->
		<u-popup :show="choiceShow" mode="bottom" @close="close" :closeable="true">
			<view class="choiceShow-wrap">
				<view class="flex goods">
					<view class="flex image-wrap">
						<image :src="payInfo.picUrl" mode="aspectFill"></image>
					</view>
					<view class="info">
						<view class="info-title">{{ info.title }}</view>
						<view class="info-coin">
							<image src="../../static/icon/bean.png" mode=""></image>
							<view>× {{ payInfo.exValue }}</view>
						</view>
						<view class="info-stock">库存：{{ payInfo.quantity }}</view>
					</view>
				</view>
				<view class="sku" v-for="(item, index) in skuList" :key="index">
					<view class="sku-title">{{ item.name }}</view>
					<view class="flex sku-list">
						<view
							:class="{'action': item.actionIndex == indexs, 'sku-list-item': item.actionIndex != indexs}"
							v-for="(ele, indexs) in item.value" :key="indexs" @click="getSku(ele, item, indexs)">
							{{ ele }}
						</view>
					</view>
				</view>
				<view class="flex quantity">
					<view class="quantity-title">兑换数量</view>
					<view class="quantity-title">
						<u-number-box v-model="orderNum" :min="1" :disabledInput="true"
							@change="valChange($event, payInfo)"></u-number-box>
					</view>
				</view>
				<view class="flex btn">
					<view class="flex btn-left">
						<view class="title">应付：</view>
						<view class="flex coin">
							<image src="../../static/icon/bean.png" mode=""></image>
							<view>× {{ payInfo.exchangePrice }}</view>
						</view>
					</view>
					<view class="btn-right">
						<view class="confirm" @click="confirmPrize">立即兑换</view>
					</view>
				</view>
			</view>
		</u-popup>

		<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>
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				goodsId: '',
				boxId: '',
				picUrlArr: [],
				info: {},
				prizeList: [],
				description: '',
				choiceShow: false,
				initData: {},
				orderNum: 1,
				skuList: [],
				skuListInit: [],
				payInfo: {},
				tipShow: false
			};
		},
		onLoad(opthios) {
			this.goodsId = opthios.id
		},
		onShow() {
			this.getDetail()
		},
		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 = this.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,
								picUrl: env.filePublic + sku.picUrl
							}
							this.skuList = skuProp
						} else {
							this.payInfo = {
								...res.data,
								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.choiceShow = true
			},

			close() {
				this.choiceShow = false
			},

			valChange(e, item) {
				let value = e.value
				this.$set(item, 'exchangePrice', value * item.exValue)
				if (this.payInfo.quantity < value) {
					uni.$u.toast('库存不足');
				}
			},

			getSku(e, item, indexs) {
				this.$set(item, 'txt', `${item.name}:${e}`)
				this.$set(item, 'actionIndex', indexs)
				this.orderNum = 1
				let actionSku = this.skuList.map(item => {
					return item.txt
				}).join(';')
				let sku = this.skuListInit.find(item => {
					return item.properties == actionSku
				})
				this.payInfo = {
					...sku,
					exValue: sku.exchangePrice,
					picUrl: env.filePublic + sku.picUrl
				}
			},

			confirmPrize() {
				let flag = false
				let data = {
					goodsId: this.payInfo.goodsId,
					skuId: this.payInfo.skuId,
					orderNum: this.orderNum,
				}
				if (flag) return
				if (this.payInfo.quantity == 0) {
					uni.$u.toast('库存不足');
					return
				}
				if (this.payInfo.quantity < this.orderNum) {
					uni.$u.toast('库存不足');
					return
				}
				uni.showLoading({
					title: '兑换中'
				});
				flag = true
				$http.post('/api/v1/mp/user/exchange/submit', data).then(res => {
					uni.hideLoading();
					flag = false
					if (res.code == 0) {
						this.choiceShow = false
						this.tipShow = true
						this.getDetail()
					} else if (res.code == 1021) {
						uni.$u.toast(res.msg);
					}
				}).catch(() => {
					flag = false
					uni.hideLoading();
				})
			},
			
			toCompanyData(){
				uni.navigateTo({
					url:`/packageGoods/goods/company?goodsId=${ this.goodsId }`
				})
			},

			/**
			 * 处理富文本里的图片宽度自适应
			 * 1.去掉img标签里的style、width、height属性
			 * 2.img标签添加style属性：max-width:100%;height:auto
			 * 3.修改所有style里的width属性为max-width:100%
			 * 4.去掉<br/>标签
			 * @param html
			 * @returns {void|string|*}
			 */
			formatRichText(html) { //控制小程序中图片大小
				let newContent = html.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;
				});
				newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
					match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
						'max-width:100%;');
					return match;
				});
				newContent = newContent.replace(/<br[^>]*\/>/gi, '');
				newContent = newContent.replace(/\<img/gi,
					'<img style="max-width:100%;height:auto;font-size: 0;margin-top: -5px;"');
				return newContent;
			},
		}
	}
</script>

<style lang="scss" scoped>

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

		&-top {
			height: 750rpx;
			background-color: #FFFFFF;
		}

		&-title {
			padding: 24rpx;
			background-color: #FFFFFF;
			margin-bottom: 10rpx;

			.txt {
				font-size: 32rpx;
				line-height: 44rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				margin-bottom: 24rpx;
				font-weight: bold;
			}

			.coin {
				display: flex;
				align-items: center;
				font-size: 32rpx;
				line-height: 44rpx;
				color: rgba(235, 112, 9, 100);
			}

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

		&-goods {
			height: 88rpx;
			text-align: center;
			line-height: 88rpx;
			font-weight: bold;
			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%;
			}
		}
	}

	.choiceShow-wrap {
		min-height: 400rpx;
		padding: 80rpx 20rpx 60rpx;

		.goods {
			justify-content: space-between;
			margin-bottom: 20rpx;

			.image-wrap {
				width: 220rpx;
				height: 220rpx;
				border: 1px solid rgba(236, 236, 236, 100);
				border-radius: 10rpx;

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

			.info {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				padding-left: 26rpx;
				height: 220rpx;

				&-title {
					font-size: 32rpx;
					line-height: 44rpx;
					font-weight: bold;
				}

				&-coin {
					display: flex;
					align-items: center;
					font-size: 32rpx;
					line-height: 44rpx;
					color: rgba(235, 112, 9, 100);
					font-weight: bold;

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

				&-stock {
					line-height: 44rpx;
				}
			}
		}

		.sku {
			margin-bottom: 30rpx;

			&-title {
				line-height: 42rpx;
			}

			&-list {
				justify-content: flex-start;

				&-item {
					line-height: 44rpx;
					padding: 0 20rpx;
					border: 1px solid rgba(187, 187, 187, 100);
					margin-left: 36rpx;
				}

				.action {
					line-height: 44rpx;
					padding: 0 20rpx;
					margin-left: 36rpx;
					border: 1px solid $uni-bg-color;
				}
			}
		}

		.quantity {
			justify-content: space-between;
			margin-bottom: 40rpx;
		}

		.btn {
			justify-content: space-between;

			&-left {
				.coin {
					display: flex;
					align-items: center;
					font-size: 32rpx;
					line-height: 44rpx;
					color: rgba(235, 112, 9, 100);
					font-weight: bold;
					margin-left: 20rpx;

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

			&-right {
				.confirm {
					width: 280rpx;
					height: 90rpx;
					font-size: 36rpx;
					line-height: 90rpx;
					border-radius: 10rpx;
					background-color: rgba(235, 112, 9, 100);
					color: rgba(255, 255, 255, 100);
					text-align: center;
				}
			}
		}
	}

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

		.btn {
			padding: 20rpx 0;

			/deep/ button {
				width: 640rpx;
				height: 90rpx;
				line-height: 90rpx;
				font-size: 36rpx;
				color: #fff;
				background-color: $uni-bg-color;
				border: none;
				border-radius: 20rpx;
			}
		}
	}
</style>
