<template>
	<view style="background-image: linear-gradient(#ff8e0c,#ffc555 80%,#ff8e0c); width: 100%; height: 100%;">

		<view v-if="pagesNum > 1">
			<u-navbar title="免费抽奖" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		</view>
		<view v-else>
			<u-navbar leftIconSize="0" :placeholder="true" bgColor="#fff" title="免费抽奖">
				<view class="nav-left flex" slot="left" @click="toActivity">
					<u-icon name="arrow-left" size="20" color="#333"></u-icon>
				</view>
			</u-navbar>
		</view>

		<view class="wrap">
			<view class="wrap-fakeNum">
				<view class="fakeNum">{{info.fakeNum}}人已参与</view>
			</view>
			<view v-if="info.status && JSON.parse(info.status).value == 3" class="wrap-date">
				<view class="wrap-date-on">
					<view class="wrap-date-on-line" style="right: 30rpx;"></view>距离开奖
					<view class="wrap-date-on-line" style="left: 30rpx;"></view>
				</view>
				<u-count-down :time="activityTime" format="DD:HH:mm:ss" autoStart millisecond @change="onChange">
					<view class="wrap-date-date">
						<text>{{timeData.days}}天</text>
						<text>{{ timeData.hours>=10?timeData.hours:'0'+timeData.hours }}:</text>
						<text>{{ timeData.minutes>=10?timeData.minutes:'0'+timeData.minutes }}:</text>
						<text>{{ timeData.seconds>=10?timeData.seconds:'0'+timeData.seconds }}</text>
					</view>
				</u-count-down>
			</view>
			<view v-if="info.status && JSON.parse(info.status).value == 2" class="wrap-date">
				<view class="wrap-date-on">
					<view class="wrap-date-on-line" style="right: 30rpx;"></view>距离开始
					<view class="wrap-date-on-line" style="left: 30rpx;"></view>
				</view>
				<u-count-down :time="activityTimeTwo" format="DD:HH:mm:ss" autoStart millisecond @change="onChange">
					<view class="wrap-date-date">
						<text>{{timeData.days}}天</text>
						<text>{{ timeData.hours>=10?timeData.hours:'0'+timeData.hours }}:</text>
						<text>{{ timeData.minutes>=10?timeData.minutes:'0'+timeData.minutes }}:</text>
						<text>{{ timeData.seconds>=10?timeData.seconds:'0'+timeData.seconds }}</text>
					</view>
				</u-count-down>
			</view>
			<view v-for="(item,index) in awardsList" :key="index">
				<view class="wrap-fixed" v-for="(itemTwo,index) in item.prizeList" :key="index"
					v-if="item.name == '一等奖'">
					<view class="wrap-fixed-effectstwo">
						<view class="effectsthree">
							<view class="effectsthree-width">
								<image src="../static/activity/effectsthree.png"></image>
								<view class="effectsthree-width-name">
									{{item.name}}
								</view>
							</view>
							<view class="effectsthree-position">{{item.quantity}}个名额 </view>
							<view class="effectsthree-positionTwo">
								<view>
									<image :src="itemTwo.picUrl" mode="aspectFit"></image>
								</view>
								<view class="textone ells-one"
									v-if="itemTwo.prizeType && JSON.parse(itemTwo.prizeType).value == 'coin'">
									{{itemTwo.title}}x{{itemTwo.value}}
								</view>
								<view class="textone ells-one" v-else> {{itemTwo.title}} </view>
								<view class="texttwo ells-one"
									v-if="itemTwo.prizeType && JSON.parse(itemTwo.prizeType).value != 'coin'">
									￥{{$numberFormat(itemTwo.value)}} </view>
							</view>
						</view>
						<view class="effectstwo"></view>
					</view>
				</view>
			</view>
			<view>
				<view v-for="(item,index) in awardsList" :key="index">
					<view v-if="item.name != '一等奖'">
						<view class="wrap-award">
							<view class="effectsthree">
								<view class="effectsthree-width">
									<image src="../static/activity/effectsthree.png" mode=""></image>
									<view class="effectsthree-width-name">
										{{item.name}}
									</view>
								</view>
								<view class="effectsthree-position">{{item.quantity}}个名额 </view>
								<view style="padding-top: 120rpx;"></view>
								<view class="effectsthree-positionTwo flex" v-for="(itemTwo,index) in item.prizeList"
									:key="index">
									<view>
										<image :src="itemTwo.picUrl" mode="aspectFit" class="imatwo"></image>
									</view>
									<view>
										<view class="textone ells-one"
											v-if="itemTwo.prizeType && JSON.parse(itemTwo.prizeType).value == 'coin'">
											{{itemTwo.title}}x{{itemTwo.value}}
										</view>
										<view class="textone ells-one" v-else> {{itemTwo.title}}</view>
										<view class="texttwo ells-one"
											v-if="itemTwo.prizeType && JSON.parse(itemTwo.prizeType).value != 'coin'">
											￥{{$numberFormat(itemTwo.value)}} </view>
									</view>

								</view>
							</view>
							<view v-if="item.prizeList.length > 1 " class="wrap-award-explain">以上奖品随机获得一件</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 我的抽奖码 -->
			<view class="wrap-description">
				<view class="wrap-description-code"> 我的抽奖码</view>
				<view v-if="info.codeList.length > 0" class="wrap-description-codetwo">
					<view class="codeList" v-for="(item,index) in info.codeList">
						<view class="codeList-bottom">{{item}}</view>
					</view>
				</view>
				<view v-else class="wrap-description-codethree">
					<view class="codethree">
						<image src="../static/activity/off.png" mode="" class="codeimg"></image>
					</view>
					<text class="codesix" style="">还没有抽奖码</text>
				</view>
				<view class="wrap-description-codesix">邀请好友助力，获取抽奖码</view>
				<view class="wrap-description-codesixtwo">每邀请一位好友助力成功，即可获得1个抽奖码，抽奖码越多中奖概率越大。</view>
			</view>

			<!-- 关注公众号 -->
			<view class="wrap-account" @click="toWeixin">
				<view class="wrap-account-view">
					<image src="../static/activity/logo.png" mode=""></image>
					<text>关注盲票公众号，获取中奖提醒</text>
					<view>去关注</view>
				</view>
			</view>

			<!-- 图文介绍 -->
			<view class="wrap-descriptiontwo">
				<view style="padding: 36rpx 24rpx 36rpx;">图文介绍</view>
				<view v-html="description">{{description}} </view>
			</view>

		</view>

		<view class="footer-fixed">
			<view class="flex btn">
				<button v-if="info.status && JSON.parse(info.status).value == 3" type="default" @click="exChange"
					open-type="share">获取抽奖码</button>
				<u-count-down v-else :time="activityTimeTwo" format="DD:HH:mm:ss" autoStart millisecond
					@change="onChange">
					<button class="buttoncol" type="default">
						开始倒计时：{{timeData.days}}&nbsp;天{{ timeData.hours>=10?timeData.hours:'0'+timeData.hours}}&nbsp;:{{timeData.minutes>=10?timeData.minutes:'0'+timeData.minutes}}&nbsp;:{{timeData.seconds>=10?timeData.seconds:'0'+timeData.seconds}}
					</button>
				</u-count-down>
			</view>
		</view>

		<!-- 		<u-popup :show="popupShow" mode="bottom" @close="close" closeable>
			<view class="popupcode">获取抽奖码</view>
			<view class="popupcodetwo">
				<view class="popupcodetwo-view">邀请好友助力，获取抽奖码</view>
				<text class="popupcodetwo-text">每邀请一位好友助力成功，即可获得1个抽奖码，没有上限</text>
			</view>
			<view class="popupcodethree" style="">
				<button type="default" class="popupcodethree-default" open-type="share">立即邀请好友助力</button>
			</view>
		</u-popup> -->

		<!-- <u-popup v-if="info.status && JSON.parse(info.status).value == 4"   mode="bottom" @close="close" closeable> -->
		<u-popup v-if="info.status && JSON.parse(info.status).value == 4" show="true" mode="center" @close="close"
			round="11">
			<view style="text-align: center;width: 600rpx; height: 500rpx;">
				<view style="color: #666;margin-top: 110rpx;font-size: 40rpx;">来晚了，活动已结束</view>
				<view style="color: #666;margin-top: 50rpx;font-size: 36rpx;">点击前往查看新的免费抽奖活动</view>
				<button @click="toActivity" type="default"
					style="background-color: #FF8000;color: #fff; width: 300rpx;margin-top: 70rpx;">确定</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: '', //邀请码
				activityTime: 0,
				activityTimeTwo: 0,
				pagesNum: "",
			}
		},
		onLoad(opthios) {
			this.id = opthios.id
			this.getDetail()
		},
		onShow() {
			this.pagesNum = getCurrentPages().length
		},
		methods: {
			toActivity() {
				uni.reLaunch({
					url: `/pages/activity/index?triggerStatus=0`
				})
			},
			getDetail() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post(`/api/v1/mp/user/marketing/detail/${this.id}`, {}).then(res => {
					uni.hideLoading();
					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
						// 处理富文本
						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);
						let timestamp = parseInt(new Date().getTime())
						this.activityTime = this.info.endTime - timestamp
						this.activityTimeTwo = this.info.startTime - timestamp
					}
				}).catch(() => {
					uni.hideLoading();
				})
				$http.post('/api/v1/mp/user/marketing/inviteCode', {}).then(res => {
					if (res.code == 0) {
						this.inviteCode = res.data
					}
				})
			},
			onChange(e) {
				this.timeData = e
			},
			exChange() {
				//弹出层打开
				// this.popupShow = true

				uni.requestSubscribeMessage({
					tmplIds: ['3y_My-yRmqmGd3-f-vAwNaK4LQeehzXBSNDi_5wcnFI',
						'FGcPOV1j9ApESr3VUlWjFr6E1vF4X_h0LLN82T2CLL0',
						'92xxL8XTRPLROyNUtmN04OT0kyq4iwSCH3sz-j6zbZs'
					],
					success(res) {

					}
				})
			},
			close() {
				//弹出层收起
				this.popupShow = false
			},

			//公众号
			toWeixin() {
				uni.navigateTo({
					url: `/packageOperate/activity/wxofficial`
				})
			},


			/**
			 * 处理富文本里的图片宽度自适应
			 * 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 }`,
				imageUrl: 'https://mp-public-test-1307117429.cos.ap-shanghai.myqcloud.com/70/EJ305PQR2IBE45O9AFAI',
			}
		},
	}
</script>

<style lang="scss" scoped>
	.popupcode {
		height: 98rpx;
		width: 100%;
		background-color: #F8832C;
		text-align: center;
		line-height: 98rpx;
		color: #fff;
		font-size: 32rpx;
	}

	.popupcodetwo {
		padding: 30rpx 20rpx;

		&-view {
			color: #101010;
			line-height: 50rpx;
			font-size: 30rpx;
		}

		&-text {
			color: #8c8c8c;
			font-size: 26rpx;
		}
	}

	.popupcodethree {
		padding: 0rpx 20rpx 50rpx;

		&-default {
			background-color: #FCE9DB;
			color: #fff;
			border: 2rpx solid #F8832C;
			color: #F9832C;
			border-radius: 22rpx;
		}
	}

	.wrap {
		// 设置ios刘海屏底部横线安全区域
		// padding-bottom: constant(safe-area-inset-bottom);
		// padding-bottom: env(safe-area-inset-bottom);
		padding-bottom: 200rpx;

		background: url(../static/activity/effectsone.png) no-repeat 100% 0%;
		background-size: 100%;

		&-fakeNum {
			margin-top: 46rpx;

			.fakeNum {
				line-height: 66rpx;
				text-align: center;
				width: 266rpx;
				height: 76rpx;
				background: url(../static/activity/rectangle.png) no-repeat center center;
				background-size: 266rpx 76rpx;
				margin: 0 auto;
				color: #A13D01;
				font-size: 30rpx;
			}
		}

		&-date {
			text-align: center;
			width: 100%;
			font-family: YouSheBiaoTiHei;
			color: #FFFFFF;

			&-on {
				display: inline-block;
				font-size: 76rpx;
				line-height: 68rpx;

				&-line {
					display: inline-block;
					width: 68rpx;
					height: 2rpx;
					overflow: hidden;
					background-color: #fff;
					position: relative;
					bottom: 25rpx;
				}
			}

			&-date {
				font-size: 52rpx;
			}
		}

		&-fixed {
			height: 630rpx;
			margin: 30rpx 30rpx;

			&-effectstwo {
				border-radius: 20rpx;
				width: 100%;
				height: 100%;
				background: url(../static/activity/effectstwo.png) no-repeat center center;

				.effectsthree {
					position: relative;
					height: 400rpx;
					width: 100%;
					z-index: 10;

					&-width {
						position: absolute;
						width: 175rpx;
						height: 175rpx;
						margin: 0;

						image {
							width: 100%;
							height: 100%;
						}

						&-name {
							position: relative;
							left: 10rpx;
							top: -160rpx;
							transform: rotateZ(-45deg);
							color: #fff;
							font-size: 26rpx;
						}
					}

					&-position {
						position: absolute;
						// z-index: 1;
						width: 180rpx;
						height: 62rpx;
						background-image: linear-gradient(to right, #FAE08A, #FFC45A);
						border-radius: 17rpx;
						line-height: 62rpx;
						text-align: center;
						color: #A13D01;
						font-size: 30rpx;
						top: 20rpx;
						right: 20rpx;
					}

					&-positionTwo {
						width: 100%;
						height: 100%;
						text-align: center;

						image {
							width: 350rpx;
							height: 350rpx;
							margin-top: 70rpx;
						}

						.textone {
							margin-top: 80rpx;
							color: #333333;
							font-size: 30rpx;
							line-height: 40rpx;
						}

						.texttwo {
							color: #FE2616;
							font-size: 28rpx;
							line-height: 34rpx;
						}
					}
				}

				.effectstwo {
					position: relative;
					bottom: 120rpx;
					margin-left: 17.5%;
					width: 65%;
					height: 65%;
					background: url(../static/activity/effectssix.png) no-repeat 100% center;
					background-size: 100%;
				}
			}
		}

		&-award {
			padding-bottom: 50rpx;
			// height: 2230rpx;
			border-radius: 18rpx;
			margin: 30rpx 30rpx;
			background-color: #fff;

			.effectsthree {
				position: relative;
				// height: 400rpx;
				// width: 100%;
				// z-index: 10;

				&-width {
					position: absolute;
					width: 175rpx;
					height: 175rpx;
					margin: 0;

					image {
						width: 100%;
						height: 100%;
					}

					&-name {
						position: relative;
						left: 10rpx;
						top: -160rpx;
						transform: rotateZ(-45deg);
						color: #fff;
						font-size: 26rpx;
					}
				}

				&-position {
					position: absolute;
					z-index: 1;
					width: 180rpx;
					height: 62rpx;
					background-image: linear-gradient(to right, #FAE08A, #FFC45A);
					border-radius: 17rpx;
					line-height: 62rpx;
					text-align: center;
					color: #A13D01;
					font-size: 30rpx;
					top: 20rpx;
					right: 20rpx;
				}

				&-positionTwo {
					width: 100%;
					height: 220rpx;
					// text-align: center;
					padding: 0 62rpx;
					justify-content: flex-start;



					.imatwo {
						width: 150rpx;
						height: 150rpx;

					}

					.textone {
						color: #333333;
						font-size: 30rpx;
						margin-left: 20rpx;
						line-height: 75rpx;
					}

					.texttwo {
						margin-left: 20rpx;
						color: #FE2616;
						font-size: 28rpx;
						line-height: 75rpx;
					}

					.imgone {
						width: 449rpx;
						height: 280rpx;
						position: relative;
						bottom: 80rpx;
						margin-left: 0%;
						z-index: 0;

						// width: 45%;
						// height: 45%;
					}
				}
			}

			&-explain {
				margin-top: 30rpx;
				text-align: center;
				color: #999999;
				font-size: 30rpx;
			}
		}

		&-description {
			min-height: 200rpx;
			background-color: #fff;
			margin: 30rpx 30rpx;
			border-radius: 18rpx;

			&-code {
				font-size: 30rpx;
				color: #333;
				padding: 36rpx 24rpx;
			}

			&-codetwo {
				text-align: center;
				margin-top: 20rpx;
				margin-bottom: 50rpx;

				.codeList {
					text-align: center;
					margin-bottom: 40rpx;

					&-bottom {
						margin: 0 auto;
						width: 526rpx;
						height: 94rpx;
						background-color: #F8832C;
						color: #fff;
						border-radius: 12rpx;
						font-size: 38rpx;
						line-height: 94rpx;

					}
				}
			}

			&-codethree {
				text-align: center;
				.codethree {
					margin-left: 80rpx;

					.codeimg {
						width: 200rpx;
						height: 200rpx;
					}
				}

				.codesix {
					line-height: 1rpx;
					color: #9A9FB4;
					font-size: 30rpx;
				}
			}

			&-codesix {
				padding: 10rpx 0;
				text-align: center;
				line-height: 60rpx;
				font-size: 30rpx;
				font-weight: bold;
				color: #F9822C;
			}
			&-codesixtwo {
				margin: 0 auto;
				width: 600rpx;
				font-size: 26rpx;
				color: #999;
				padding-bottom: 40rpx;
			}
		}

		&-descriptiontwo {
			margin: 30rpx;
			border-radius: 18rpx;
			overflow: hidden;
			background-color: #fff;
		}

		&-account {
			height: 100rpx;
			border-radius: 22rpx;
			background-color: #fff;
			margin: 30rpx 30rpx 0 30rpx;
			text-align: center;
			&-view {
				padding: 0 20rpx;

				image {
					width: 64rpx;
					height: 64rpx;
					vertical-align: -50%;
				}

				text {
					line-height: 100rpx;
					margin: 20rpx;
					font-size: 30rpx;
					color: #333333;
				}

				view {
					text-align: center;
					border-radius: 6rpx;
					display: inline-block;
					height: 44rpx;
					line-height: 44rpx;
					font-size: 26rpx;
					width: 112rpx;
					color: #f9822c;
					background-color: #fff;
					border: 2rpx solid #f9822c;
				}

			}


		}
	}

	.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: 34rpx;
				color: #fff;
				background-color: #FF6000;
				border: none;
				border-radius: 48rpx;
			}
		}

		.buttoncol {
			background-color: #555;
		}
	}
</style>
