<template>
	<view>
		<u-navbar leftIconSize="0" title="幸运数字" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<view class="lucky">
			<view class="lucky-number">
				<view class="flex lucky-number-circle">
					<view class="title">幸运数字</view>
					<view class="num" v-if="info.status == 1">?</view>
					<view class="luckyNum" v-else>{{ info.plainLuckyNum }}</view>
				</view>
			</view>
			<view class="lucky-title">
				<view class="txt">{{ info.title }}</view>
				<view class="id">盲票序列号：{{ info.serialNo }}</view>
			</view>
			<!-- <view class="flex lucky-tip"><text>请刮开纸质票面的数字，与此幸运数字相同的就是所中奖项</text></view> -->
			<view class="flex lucky-btn">
				<view class="pay" @click="pay" v-if="info.status == 1">支付{{ info.salePrice / 100 }}元 立即查看</view>
				<view class="pay" @click="saveImg" v-else>保存至手机相册</view>
			</view>
		</view>

		<pay-popup :pay-show="payShow" :pay-info="payInfo" @close="close" @success="getDetailInfo" />

		<u-popup :show="showNull" :round="10" mode="center" :safeAreaInsetBottom="false">
			<view class="null-prize">
				<view class="title">该盲票已兑奖</view>
				<view class="btn" @click="toIndex">确认</view>
			</view>
		</u-popup>

		<hch-poster ref="hchPoster" @cancel="handleCancel" :posterData.sync="posterData" @previewImage='previewImage'
			@canvasConfirm="toIndex" />
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	import PayPopup from '../../components/pay-popup/pay-popup.vue'
	import HchPoster from "../../components/hch-poster/hch-poster.vue"

	export default {
		components: {
			PayPopup,
			HchPoster
		},
		data() {
			return {
				serialNo: '',
				info: '',
				payInfo: {},
				payShow: false,
				payLookFlag: true,
				showNull: false,
				posterData: {}
			};
		},

		onLoad(options) {
			if (options.id) {
				console.log(options);
				this.serialNo = options.id
			}

			if (options.q) {
				let url = JSON.stringify(options.q)
				this.serialNo = url.substring(url.length - 22, url.length - 1)
			}
		},
		onShow() {
			if (this.serialNo && this.payLookFlag) {
				this.getDetail()
			}
		},
		methods: {
			getDetail() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/ticket/queryLuckyNum', {
					serialNo: this.serialNo,
					noToken: true
				}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						if (res.data.status == 1) {
							this.info = res.data
						} else if (res.data.status == 2) {
							if (uni.getStorageSync('token')) {
								uni.redirectTo({
									url: `/pages/choice/index?id=${ res.data.ticketId }`
								})
							} else {
								uni.showModal({
									title: '提示',
									content: '您未登录或登录失效！',
									confirmText: '去登录',
									showCancel: false,
									success(res) {
										if (res.confirm) {
											uni.navigateTo({
												url: "/pages/login/index"
											})
										}
									}
								})
							}
						} else {
							this.showNull = true
						}
					} else {
						uni.$u.toast('该盲票不存在！');
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			getDetailInfo() {
				this.payShow = false
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/ticket/queryLuckyNum', {
					serialNo: this.serialNo,
					noToken: true
				}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						this.info = res.data
						let num = res.data.plainLuckyNum
						this.posterData.num.text =
							this.posterData = {
								poster: {
									//根据屏幕大小自动生成图片背景大小
									url: env.filePublic + this.info.picUrl, //图片地址
									r: 10, //圆角半径
									w: 300, //图片宽度
									h: 480, //图片高度
									p: 20 //图片内边距padding
								},
								square3: {
									x: 100,
									y: 100,
									r: 0, //圆角半径
									w: 240, //宽度
									h: 180, //高度
									mt: 65
								},
								title: {
									text: "幸运数字", //文本
									fontSize: 20, //字体大小
									color: "#000", //颜色
									lineHeight: 25, //行高
									mt: 20, //margin-top
									align: 'center'
								},
								num: {
									text: num + '', //文本
									fontSize: 40, //字体大小
									color: "#fff", //颜色
									lineHeight: 25, //行高
									mt: 20, //margin-top
									align: 'center'
								},
								txt: {
									text: res.data.title, //文本
									fontSize: 16, //字体大小
									color: "#000", //颜色
									lineHeight: 25, //行高
									mt: 20, //margin-top
									align: 'center'
								},
							}
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			close() {
				this.payShow = false
			},

			pay() {
				let data = {
					ticketId: this.info.ticketId,
					autoCoupon: 1
				}
				this.payLookFlag = false
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/ticket/order/settle', data).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						let info = {
							...res.data,
							...this.info,
							picUrl: env.filePublic + res.data.picUrl,
						}
						this.payInfo = info
						this.payShow = true
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			screen() {

			},

			toIndex() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},

			previewImage() {

			},

			saveImg() {
				this.$refs.hchPoster.posterShow()
			},

			handleCancel() {

			},

			toIndex() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.lucky {
		width: 100%;
		min-height: calc(100vh - 44px - var(--status-bar-height));
		background: url(https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/background.png) center center;

		&-number {
			padding-top: 300rpx;

			&-circle {
				box-sizing: border-box;
				flex-direction: column;
				width: 336rpx;
				height: 336rpx;
				background-color: #FFFFFF;
				border-radius: 50%;
				margin: auto;

				.title {
					margin-bottom: 20rpx;
				}

				.num {
					font-size: 120rpx;
					color: #E86737;
				}

				.luckyNum {
					font-size: 120rpx;
					color: #E86737;
				}
			}
		}

		&-title {
			margin-top: 40rpx;
			margin-bottom: 200rpx;

			.txt {
				text-align: center;
				font-size: 36rpx;
				font-weight: bold;
				line-height: 50rpx;
				margin-bottom: 8rpx;
			}

			.id {
				line-height: 40rpx;
				text-align: center;
				color: #888787;
			}
		}

		&-tip {
			padding: 0 40rpx;
			color: #333;
			width: 80vw;
			height: 200rpx;
			;
			border-radius: 10rpx;
			background-color: rgba(255, 255, 255, 20);
			// opacity: .4;
			text-align: center;
			margin: 0 auto 20rpx;
		}

		&-btn {
			.pay {
				width: 400rpx;
				height: 80rpx;
				font-size: 36rpx;
				line-height: 80rpx;
				border-radius: 20rpx;
				background-color: rgba(235, 112, 9, 100);
				color: #FFFFFF;
				text-align: center;
			}
		}

		.index {
			margin: 60rpx 0;
			text-align: center;
			font-weight: bold;
			color: $uni-color-primary;
		}
	}

	.null-prize {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 80vw;
		height: 320rpx;
		background-color: #FFFFFF;
		border: 1px solid rgba(187, 187, 187, 100);

		.btn {
			margin-top: 60rpx;
			width: 160rpx;
			height: 60rpx;
			line-height: 60rpx;
			border-radius: 8rpx;
			background-color: rgba(235, 112, 9, 100);
			color: rgba(255, 255, 255, 100);
			font-size: 28rpx;
			text-align: center;
		}
	}
</style>
