<template>
	<view>
		<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="奖品详情"></u-navbar>
		<view class="detail">
			<view class="detail-top">
				<u-swiper :list="picUrlArr" height="375" radius="0" :indicator="true" :circular="true"></u-swiper>
			</view>
			<view class="detail-info">
				<view class="content" v-text="info.title"></view>
			</view>
			<view class="detail-title">商品详情</view>
			<view class="detail-description">
				<view class="" v-html="description"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				boxId: '',
				picUrlArr: [],
				info: {},
				prizeList: [],
				description:'',

				payShow: false,
				payInfo: {}
			};
		},
		onLoad(opthios) {
			this.getDetail(opthios.id)
		},
		methods: {
			getDetail(id) {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/exchange/goods/detail', {
					noToken: true,
					goodsId: id
				}).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)
						})
						// 处理富文本
						this.description = this.formatRichText(res.data.description);
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			close() {
				this.payShow = false
			},

			exchange() {
				let data = {
					couponIds: [],
					autoCoupon: 1,
					boxId: this.info.boxId,
					ticketId: this.info.ticketId,
					orderNum: 1
				}
				$http.post('/api/v1/mp/user/ticket/order/settle', data).then(res => {
					if (res.code == 0) {
						let info = {
							...res.data,
							...this.info,
							picUrl: env.filePublic + res.data.picUrl,
						}
						this.payInfo = info
						this.payShow = true
					}
				})
			},
			/**
			 * 处理富文本里的图片宽度自适应
			 * 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;margin:10rpx auto;"');
				return newContent;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.detail {

		&-info{
			padding: 50rpx 20rpx 50rpx;
			margin-bottom: 10rpx;
			background-color: #fff;
			
			.content{
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		
		&-title{
			height: 88rpx;
			text-align: center;
			line-height: 88rpx;
			font-weight: bold;
			background-color: #FFFFFF;
		}
		
		&-description {
			image {
				width: 100%;
			}
		}

	}
</style>
