<template>
	<view class="vote-detail">
		<!-- 分享海报 -->
		<hch-poster ref="hchPoster" @cancel="handleCancel" :posterData.sync="posterData"
			@previewImage='previewImage' />
		<!-- 活动查询 -->
		<view class="title">
			<image style="width: 158rpx;height: 158rpx;border-radius: 50%; " :src="voteDetail.logo" mode="">
			</image>
			<text class="title-text">{{voteDetail.project_name}}</text>
			<text class="title-btn">{{voteDetail.activity.title}}</text>
		</view>
		<!-- 分割线 -->
		<view style="width: 92%; height: 2rpx; background-color:#F0F0F0;margin: 40rpx auto;  "></view>
		<!-- 排名 -->
		<view class="rank">
			<view class="vote-detail-btn">
				<view class="vote-detail-btn-item">
					<text class="vote-detail-btn-item-num">{{voteDetail.project_no}}</text>
					<text class="vote-detail-btn-item-text">编号</text>
				</view>
				<view style="width: 2rpx;height: 52rpx;background-color: #ccc;"></view>
				<view class="vote-detail-btn-item">
					<text class="vote-detail-btn-item-num">{{voteDetail.ticket_num}}</text>
					<text class="vote-detail-btn-item-text">票数</text>
				</view>
				<view style="width: 2rpx;height: 52rpx;background-color: #ccc;"></view>
				<view class="vote-detail-btn-item">
					<text class="vote-detail-btn-item-num">{{voteDetail.rank}}</text>
					<text class="vote-detail-btn-item-text">排名</text>
				</view>
				<view style="width: 2rpx;height: 52rpx;background-color: #ccc;"></view>
				<view class="vote-detail-btn-item">
					<text class="vote-detail-btn-item-num">{{voteDetail.next_rank_ticket_num}}</text>
					<text class="vote-detail-btn-item-text">距上名</text>
				</view>
			</view>
		</view>
		<!-- 轮播图 -->
		<view style="background-color: #f9f9f9;">
			<view class="swiper">
				<view class="home-swiper">
					<uni-swiper-dot :info="info" :current="current1" :mode="mode" :dots-styles="dotsStyles">
						<swiper class="swiper-box" @change="change1" circular>
							<swiper-item v-for="(item ,index) in info" :key="index">
								<view class="swiper-item">
									<image style="width: 100%;height:576rpx; border-radius: 12rpx; " :src="item.img"
										mode="aspectFill"></image>
								</view>
							</swiper-item>
						</swiper>
					</uni-swiper-dot>
				</view>
			</view>
			<!-- 投票 -->
			<view class="btn" @click="goVote(activity_project_id)">
				<view v-if="spending || isStart" class="btn-vote"
					:class="spending?'btn-vote': isStart?'btn-vote-fasle' :'' ">
					<text>投票</text>
				</view>
				<view class="btn-vote-fasle" v-if="isEnd">
					<text>投票结束</text>
				</view>
			</view>
			<!-- 帮我拉票 -->
			<view class="help">
				<view class="help-btn" @click="getPoster">
					<text>帮我拉票</text>
				</view>
				
			</view>

			<movable-area class="movableArea">
				<movable-view class="movableView" direction="all" x="630rpx" y="700rpx">
					<view class="img-IHg" @click="goJoin">
						<image style="width: 64rpx; height: 58rpx; " src="/static/icon/vip.png" mode=""></image>
					</view>
				</movable-view>
			</movable-area>


			<!-- 分割线 -->
			<view style="width: 92%; height: 2rpx; background-color:#F0F0F0;margin: 64rpx auto;  "></view>
			<!-- 介绍 -->
			<view class="introduce">
				<view class="introduce-title">
					<text>参赛介绍</text>
				</view>
				<view class="introduce-text">
					{{voteDetail.intro}}
				</view>
			</view>

			<!-- 预约 -->
			<view class="reserve">
				<view class="reserve-left" @click="goBooking">
					<image style="width: 36rpx;height: 36rpx; margin-right: 10rpx;" src="/static/icon/reserve02.png"
						mode=""></image>
					<text>预约</text>
				</view>
				<view class="reserve-right" @click="goJoin">
					<image style="width: 60rpx;height: 56rpx; margin-right: 4rpx; " src="/static/icon/reserve01.png"
						mode=""></image>
					<text>加入IHG会员</text>
				</view>
			</view>
		</view>

		<!-- 弹出层 -->
		<uni-popup ref="popup" type="center">
			<view class="pop">
				<view class="title"><text>投票成功</text></view>
				<view class="img">
					<image :src="voteDetail.activity.success_img"></image>
				</view>
				<view class="textfont">
					<text v-if="!isVoteNum">今日投票数已用完
						点击 加入IHG会员 可享受额外X票数</text>
					<text v-if="isVoteNum">今日投票数还有 {{userCanVoteNum}} 次
						点击再次投票可再次为本项目投票也可为其他项目投票</text>
					<!-- <text v-if="true">恭喜你中奖了！
						获得了XXX的奖品，请尽快领取</text> -->
				</view>
				<view class="btn">
					<view class="cancel" @click="closeVote"><text>{{isVoteNum?'再次投票':'关闭'}}</text></view>
					<view class="download" @click="goJoin"><text>{{isVoteNum?'其他项目':'加入IHG会员'}}</text></view>
					<!-- <view class="download" @click="goJoin" v-if="false"><text>去领取</text></view> -->
				</view>
			</view>

			

		</uni-popup>
	</view>
</template>

<script>
	import HchPoster from "@/components/hch-poster/hch-poster.vue"

	export default {
		components: {
			HchPoster
		},
		data() {
			return {
				// 海报模板数据
				posterData: {
					poster: {
						//根据屏幕大小自动生成海报背景大小
						url: "https://huangchunhongzz.gitee.io/imgs/poster/poster_bg_3.png", //图片地址
						r: 10, //圆角半径
						w: 300, //海报宽度
						h: 480, //海报高度
						p: 20 //海报内边距padding
					},
					mainImg: {
						//海报主商品图
						url: "https://huangchunhongzz.gitee.io/imgs/poster/product.png", //图片地址
						r: 10, //圆角半径
						w: 250, //宽度
						h: 200 //高度
					},
					title: {
						//商品标题
						text: "今日上新水果，牛奶草莓，颗粒饱满，每盒 200g", //文本
						fontSize: 16, //字体大小
						color: "#000", //颜色
						lineHeight: 25, //行高
						mt: 20 //margin-top
					},
					codeImg: {
				 	//小程序码
						url: "https://huangchunhongzz.gitee.io/imgs/poster/code.png", //图片地址
						w: 100, //宽度
						h: 100, //高度
						mt: 20, //margin-top
						r: 50 //圆角半径
					},
					tips: [
						//提示信息
						{
							text: "记忆之王", //文本
							fontSize: 14, //字体大小
							color: "#2f1709", //字体颜色
							align: "center", //对齐方式
							lineHeight: 25, //行高
							mt: 20 //margin-top
						},
				  {
							text: "长按/扫描识别查看商品", //文本
							fontSize: 12, //字体大小
							color: "#2f1709", //字体颜色
							align: "center", //对齐方式
							lineHeight: 25, //行高
							mt: 20 //margin-top
						}
					]
				},

				//跳转vip小程序获得投票数
				vipRewardTicketNum: '',
				//活动id
				sourceId: '',
				//活动可以使用的投票数
				userCanVoteNum: '',
				isVoteNum: false,
				//判断活动是否正在进行中
				spending: false,
				//判断活动是否开始
				isStart: false,
				//判断活动是否结束
				isEnd: false,
				//活动开始时间
				startTime: '',
				//活动结束时间
				endTime: "",
				// 活动项目id
				activity_project_id: '',
				// 活动项目详情
				voteDetail: '',
				//轮播图
				info: [{
					img: 'http://t9.9026.com/imgs/swiper01.png',
					name: '酒店预订'
				}, ],
				dotsStyles: {
					backgroundColor: 'rgba(255, 255, 255, .3)',
					border: '1px rgba(255, 255, 255, .3) solid',
					color: '#fff',
					selectedBackgroundColor: 'rgba(255, 255, 255, 1)',
					selectedBorder: '1px rgba(255, 255, 255, 1) solid'
				},
				//指示点显示位置
				current1: 0,
				//指示点模式
				mode: 'default',
			}
		},
		onLoad(op) {
			this.activity_project_id = op.id
			this.getDetail(op.id)
		},
		watch: {
			vipRewardTicketNum: {
				handler(newName, oldName) {
					console.log(newName, '--->监听')
				},
				immediate: true,
				deep: true
			}
		},
		methods: {
			//隐藏
			handleCancel(){
				console.log('取消审生成海报');
			},
			//生产海报
			getPoster(){
				this.$refs.hchPoster.posterShow()
			},
			// 切换轮播图指示点
			change1(e) {
				this.current1 = e.detail.current;
			},
			getDetail(id) {
				this.$api.active.getActiveProjectDetail({
					activity_project_id: id
				}).then(res => {
					console.log(res, "detail")
					if (res.code == 0) {
						this.voteDetail = res.data
						this.startTime = res.data.activity.start_time
						this.endTime = res.data.activity.end_time
						this.cancelTime(this.startTime, this.endTime)
						//活动可以使用的投票数
						this.userCanVoteNum = res.data.user_can_vote_num
						if (this.userCanVoteNum > 0) {
							this.isVoteNum = true
						} else {
							this.isVoteNum = false
						}
						this.sourceId = res.data.activity.id
						//跳转vip小程序获得投票数
						this.vipRewardTicketNum = res.data.activity.vip_reward_ticket_num
						console.log(this.vipRewardTicketNum);
					}
				})
			},
			cancelTime(i, j) {
				let nowtime = new Date() //获取当前时间
				let startime = new Date(i) //活动开始时间
				let endtime = new Date(j); //活动结束时间
				let lefttime = endtime.getTime() - nowtime.getTime() //距离结束时间的毫秒数
				let spendtime = nowtime.getTime() - startime.getTime() //距离结束时间的毫秒数
				if (lefttime > 0 && spendtime > 0) {
					//活动进行中
					this.spending = true
				} else if (lefttime < 0) {
					//活动结束
					this.isEnd = true
				} else if (spendtime < 0) {
					//活动未开始
					this.isStart = true
				}

			},
			// 打开弹出层
			openVote() {
				this.$refs.popup.open()
			},
			// 关闭弹出层
			closeVote() {
				this.$refs.popup.close()
			},
			// 跳转其他小程序
			goJoin() {
				let _this = this
				this.closeVote()
				wx.navigateToMiniProgram({
					appId: 'wx255b58f0992b3c53', //appid
					path: 'newUIMain/enrollment/enrollment', //path
					extraData: { //参数
						foo: 'bar'
					},
					// envVersion: 'develop', //开发版develop 开发版 trial   体验版 release 正式版 
					success(res) {
						_this.$api.my.userJoinVip({
							source_type: 1,
							source_id: _this.sourceId
						}).then(res => {
							console.log('-->res', res)
						})
						// 打开成功
					},
					fail(e) {
						console.log(e, '失败')
					}
				})
			},
			// 投票
			goVote(id) {
				this.$api.active.vote({
					activity_project_id: id
				}).then(res => {
					console.log(res, '投票')
					if (res.code == 0) {
						this.openVote()
						this.getDetail(this.activity_project_id)


					} else {
						if (res.msg.length > 7) {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						} else {
							uni.showToast({
								icon: 'error',
								title: res.msg
							})
						}

					}
				}).catch(err => {
					console.log(err)
					uni.showToast({
						icon: 'none',
						title: '投票失败'
					})
				})
			},
			// 去预约
			goBooking() {
				uni.switchTab({
					url: '/pages/map/map'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	$pageColor:#F9F9F9;
	$bgColor:#FFFFFF;

	@mixin flexlayout {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	page {
		height: 100% !important;
		background: #F9F9F9 !important;
	}

	.vote-detail {
		height: 100%;
		background: #F9F9F9;
	}

	.reserve {
		// position: fixed;
		// bottom: 0;
		width: 100%;
		height: 148rpx;
		padding: 0 30rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 30rpx;
		color: #fff;
		font-weight: bold;

		.reserve-left {
			width: 204rpx;
			height: 92rpx;
			background: linear-gradient(270deg, #FF6200 0%, #FF9342 100%);
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.reserve-right {
			width: 466rpx;
			height: 92rpx;
			background: linear-gradient(270deg, #FF6200 0%, #FF9342 100%);
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

	}



	.introduce {
		padding: 0 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-bottom: 102rpx;

		.introduce-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}

		.introduce-text {
			margin-top: 44rpx;
			line-height: 40rpx;
			color: #333333;
			font-size: 28rpx;
		}
	}


	.help {
		padding: 0 30rpx;
		margin-top: 30rpx;
		position: relative;

		.help-btn {
			height: 108rpx;
			background: #F9F9F9;
			border-radius: 14rpx;
			border: 2rpx solid #FF6200;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #FF6200;
			font-weight: bold;
			font-size: 30rpx;
		}

		.img-IHg {
			width: 84rpx;
			height: 84rpx;
			background-color: #fff;
			box-shadow: 0px 8rpx 24rpx 0px rgba(220, 222, 229, 0.4);
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			position: fixed;
			// top: -28rpx;
			// right: 14rpx;
			top: 1030rpx;
			right: 30rpx;
			z-index: 99;

		}
	}



	.btn {
		padding: 0 30rpx;
		margin-top: 48rpx;

		.btn-vote {
			height: 108rpx;
			background: linear-gradient(338deg, #FF6200 0%, #FF9D4F 100%);
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #FFFFFF;
			font-size: 30rpx;
			font-weight: bold;
		}

		.btn-vote-fasle {
			height: 108rpx;
			background: #CCCCCC;
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #FFFFFF;
			font-size: 30rpx;
			font-weight: bold;
		}
	}




	.swiper {
		padding: 0 30rpx;
		margin-top: 64rpx;
	}

	.home-swiper {
		height: 576rpx;

		.swiper-box {
			height: 576rpx;
		}
	}

	.title {
		padding-top: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.title-text {
			margin: 32rpx 0 16rpx;
			font-size: 40rpx;
			font-weight: bold;
			color: #333;
		}

		.title-btn {
			color: #FF6200;
			font-size: 28rpx;
		}
	}

	.rank {
		padding: 0 30rpx;
	}

	.vote-detail-btn {
		height: 190rpx;
		background-color: #fff;
		box-shadow: 0px 20rpx 40rpx 0px rgba(220, 222, 229, 0.4);
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;

		.vote-detail-btn-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			color: #333;

			.vote-detail-btn-item-num {
				font-size: 44rpx;
				font-weight: bold;
			}

			.vote-detail-btn-item-text {
				margin-top: 20rpx;
			}
		}
	}

	.pop {
		width: 640rpx;
		height: 740rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding-top: 32rpx;
		box-sizing: border-box;

		.title {
			@include flexlayout();
			margin-bottom: 32rpx;

			text {
				font-size: 34rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #FF7119;
			}
		}

		.img {
			margin-left: 48rpx;
			width: 544rpx;
			height: 306rpx;
			background: $bgColor;
			border-radius: 2rpx;
			@include flexlayout();
			margin-bottom: 32rpx;

			image {
				width: 544rpx;
				height: 306rpx;
			}
		}

		.textfont {
			margin-left: 30rpx;
			margin-bottom: 40rpx;
			@include flexlayout();
			width: 572rpx;
			height: 106rpx;

			text {
				text-align: center;
				font-size: 34rpx;
				font-weight: 400;
				color: #828282;
			}
		}

		.btn {
			width: 100%;
			height: 110rpx;
			display: flex;
			align-items: center;

			.cancel {
				@include flexlayout();
				width: 50%;
				height: 100%;
				border-top: #E5E5E5 solid 1rpx;
				border-right: #E5E5E5 solid 1rpx;

				text {
					font-size: 32rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #666666;
				}
			}

			.download {
				border-top: #E5E5E5 solid 1rpx;
				// border-left:#E5E5E5 solid 0.3rpx;
				@include flexlayout();
				height: 100%;
				width: 50%;

				text {
					font-size: 32rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #FF7119;
				}
			}
		}
	}

	.movableArea {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		pointer-events: none; //设置area元素不可点击，则事件便会下移至页面下层元素

		.movableView {
			pointer-events: auto; //可以点击
			width: 84rpx;
			height: 84rpx;
			padding: 0 30rpx;

			.img-IHg {
				width: 84rpx;
				height: 84rpx;
				background-color: #fff;
				box-shadow: 0px 8rpx 24rpx 0px rgba(220, 222, 229, 0.4);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
