<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-btn">
				<view class="pay" @click="pay" v-if="info.status == 1">支付{{ info.facePrice / 100 }}元 立即查看</view>
				<view class="pay" @click="screen" v-else>保存至手机相册</view>
			</view>
			<view class="index" @click="toIndex">去首页</view>
		</view>

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

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	import PayPopup from '../../components/pay-popup/pay-popup.vue'
	export default {
		components: {
			PayPopup
		},
		data() {
			return {
				serialNo: '',
				info: '',
				payInfo: {},
				payShow: false
			};
		},

		onLoad(options) {
			this.serialNo = options.id
			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{
							uni.redirectTo({
								url: `/pages/choice/index?id=${ res.data.ticketId }`
							})
						}
					}
				}).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
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			close() {
				this.payShow = false
			},

			pay() {
				let data = {
					ticketId: this.info.ticketId,
					autoCoupon: 1
				}
				$http.post('/api/v1/mp/user/ticket/order/settle', data).then(res => {
					console.log(res);
					if (res.code == 0) {
						let info = {
							...res.data,
							picUrl: env.filePublic + res.data.picUrl,
						}
						this.payInfo = info
						this.payShow = true
						console.log(info);
					}
				})
			},

			screen() {

			},

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

<style lang="scss" scoped>
	.lucky {
		width: 100%;
		height: calc(100vh - 50px);
		background: url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic3.zhimg.com%2F50%2Fv2-a6f5c8b5b66fe87e4e79c1fc82a61a36_hd.jpg&refer=http%3A%2F%2Fpic3.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649658373&t=f216ee4825d5b36e62aa3a490516bfb1) center center;

		&-number {
			padding-top: 400rpx;

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

				.title {
					margin-bottom: 20rpx;
				}

				.num {
					font-size: 100rpx;
				}

				.luckyNum {
					font-size: 36rpx;
				}
			}
		}

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

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

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

		&-btn {
			.pay {
				width: 400rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 8rpx;
				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;
		}
	}
</style>
