<template>
	<view>
		<u-navbar title="免费抽奖" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<u-count-down :time="30 * 60 * 60 * 1000" format="DD:HH:mm:ss" autoStart millisecond @change="onChange">
			<view style="height: 98rpx;background-color: #fff;margin: 0 30rpx;line-height: 98rpx;overflow: hidden;">
				<view class="data-btn">距离开奖时间：
					<text>{{timeData.days}}&nbsp;天</text>
					<text>{{ timeData.hours>10?timeData.hours:'0'+timeData.hours}}&nbsp;:</text>
					<text>{{timeData.minutes}}&nbsp;:</text>
					<text>{{timeData.seconds}}</text>
				</view>
				<view style="float: right;"><span style="font-weight: 600;">{{info.fakeNum}}</span>人已参与</view>
			</view>
		</u-count-down>

		<view>
			<view v-for="(item,index) in awardsList" :key="index">
				<view v-if="item.name == '一等奖'">
					<view style="height: 90rpx;">
						<view style="float: left;">
							<text
								style="display: inline-block; background-color: #bbbbbb;width: 200rpx;height: 90rpx;line-height:90rpx;text-align: center;">{{item.name}}</text>
						</view>
						<view style="float: right;line-height: 90rpx;">{{item.quantity}}个名额</view>
					</view>
					<view v-for="(itemTwo,index) in item.prizeList" :key="index">
						<view style="text-align: center;">
							<image :src="itemTwo.picUrl" mode="" style="width: 400rpx;"></image>
							<view>{{itemTwo.title}}</view>
							<view>￥{{itemTwo.value / 100}}</view>
						</view>
					</view>
					<view v-if="item.prizeList.length > 1 " style="text-align: center;">以上奖品随机获得一件</view>
				</view>
			</view>
			<view v-for="(item,index) in awardsList" :key="index">
				<view v-if="item.name != '一等奖'">
					<view style="height: 60rpx;">
						<view style="float: left;"> 
							<text style="display: inline-block; background-color: #bbbbbb;width: 200rpx;height: 60rpx;line-height:60rpx;text-align: center;">{{item.name}}</text>
						</view>
						<view style="float: right;line-height: 60rpx;">{{item.quantity}}个名额</view>
					</view>
					<view v-for="(itemTwo,index) in item.prizeList" :key="index">
						<view style="overflow: hidden; width: 100%;padding: 10rpx 20rpx;">
							<view style="display: inline-block;">
								<image :src="itemTwo.picUrl" mode="" style="width: 100rpx;height: 100rpx;"></image>
							</view>
							<view style="display: inline-block;width: 60%;margin-left: 20rpx;">
								<view>{{itemTwo.title}}</view>
								<view>￥{{itemTwo.value / 100}}</view>
							</view>

						</view>
					</view>
					<view v-if="item.prizeList.length > 1 " style="text-align: center;">以上奖品随机获得一件</view>
				</view>
			</view>
		</view>
		<view>
			<view style="height: 90rpx;line-height: 90rpx;">
				<text
					style="display: inline-block; background-color: #bbbbbb;width: 200rpx;text-align: center;">图文介绍</text>
			</view>
			<view v-html="description">{{description}} </view>
		</view>
		<view v-if="false"> 关注公众号：获取抽奖码和开奖提醒 > </view>

		<view>
			<view>我的抽奖码</view>
			<view style="text-align: center;margin-top: 20rpx;margin-bottom: 50rpx;">
				<view v-if="info.codeList.length > 0">
					<view v-for="(item,index) in info.codeList">{{item}}</view>
				</view>
				<view v-else>还没有抽奖码</view>
			</view>

			<view style="text-align: center;line-height: 90rpx;border-top: 2rpx solid #555;">抽奖码越多，中奖概率越大</view>
		</view>

		<view style="height: 200rpx;"></view>
		<view class="footer-fixed">
			<view class="flex btn">
				<button type="default" @click="exChange">获取抽奖码</button>
			</view>
		</view>
		<u-popup :show="popupShow" mode="bottom" @close="close" closeable>
			<view
				style="height: 98rpx;width: 100%; border-bottom: 2rpx solid #bbbbbb;text-align: center;line-height: 98rpx;">
				获取抽奖码</view>
			<view style="padding: 30rpx 20rpx;">
				<view style="color: #101010; line-height: 50rpx;">邀请好友助力，获取抽奖码</view>
				<text style="color: #8c8c8c; font-size: 24rpx;">每邀请一位好友助力成功，即可获得1个抽奖码，没有上限</text>
			</view>
			<view style="padding: 0rpx 20rpx 50rpx ; ">
				<button type="default" style="background-color: #f7682b; color: #fff;"  open-type="share">立即邀请好友助力</button>
			</view>


		</u-popup>
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				popupShow: false,
				id: '',
				description: '', //富文本
				info: {}, //列表
				timeData: {}, //时间
				awardsList: [], //奖品
				inviteCode: '', //邀请码
			}
		},
		onLoad(opthios) {
			this.id = opthios.id
			this.getDetail()
		},
		onShow() {

		},
		methods: {
			getDetail() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post(`/api/v1/mp/user/marketing/detail/${this.id}`, {}).then(res => {
					uni.hideLoading();
					console.log(res)
					if (res.code == 0) {
						this.info = res.data
						let awardsList = res.data.awardsList
						awardsList.forEach(item => {
							item.prizeList.forEach(itemTwo => {
								itemTwo.picUrl = (env.filePublic + itemTwo.picUrl.split(',')[0] +
									'?imageView2/2/w/750')
							})
						})
						this.awardsList = awardsList
						// 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);
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},
			onChange(e) {
				this.timeData = e
			},
			exChange() {
				//弹出层打开
				this.popupShow = true
				$http.post('/api/v1/mp/user/marketing/inviteCode', {}).then(res=>{
					if(res.code == 0){
						this.inviteCode = res.data
						console.log(this.inviteCode);
					}
				})
				uni.requestSubscribeMessage({
					tmplIds:['3y_My-yRmqmGd3-f-vAwNaK4LQeehzXBSNDi_5wcnFI','FGcPOV1j9ApESr3VUlWjFr6E1vF4X_h0LLN82T2CLL0','92xxL8XTRPLROyNUtmN04OT0kyq4iwSCH3sz-j6zbZs'],
					success (res) {
						console.log(res);
					}
				})
			},
			close() {
				//弹出层收起
				this.popupShow = false
			},


			/**
			 * 处理富文本里的图片宽度自适应
			 * 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;
			},
		},
		onShareAppMessage(res) {
			return {
				title: '超值宝贝免费抽，参与活动仅需3秒',
				path: `/pages/index/index?marketingId=${ this.info.id }?inviteCode=${ this.inviteCode }`
			}
		},
	}
</script>

<style lang="scss" scoped>
	.data-btn {
		float: left;

		text {
			display: inline-block;
			width: 60rpx;
		}
	}

	.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;
			}
		}
		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}
</style>
