<template>
	<view>
		<loadSke :loading='loading'>
			<view class="remind">请到影院现场柜台或取票机取票</view>
			<view class="des-box">
				<loadSke :loading='qrcodeLoading'>
					<view v-if="ticketType==2">
						<view v-if="orderDes.orderDataJson.filmMcpData" class="code-box">
							<view class="code">
								<text>取票码</text>
								<text>{{orderDes.orderDataJson.filmMcpData.ticket.slice(4)}}</text>
							</view>
							<image class="qrcode" :src="filmDes.qrcode" mode="widthFix"></image>
						</view>
						<view v-else class="code-box">
							<image src="https://t1-1305573081.cos.ap-shanghai.myqcloud.com/wxapp/static/imgs/%E8%AE%A2%E5%8D%95%E5%85%B3%E9%97%AD.png"
							 class="qrcode-loading" mode="widthFix"></image>
						</view>
					</view>
					<view v-else-if="ticketType!=2">
						<view v-if="filmDes.orderStatus < 4" class="code-box">
							<image src="https://t1-1305573081.cos.ap-shanghai.myqcloud.com/wxapp/static/imgs/%E7%94%B5%E5%BD%B1%E7%A5%A8%E7%AD%89%E5%BE%85.png"
							 class="qrcode-loading" mode="widthFix"></image>
						</view>
						<view v-else-if="filmDes.orderStatus != 10" class="code-box"  v-for="(item,index) in filmDes.qrcode" :key='index'>
							<view class="code">
								<text>取票码</text>
								<text>{{item.code}}</text>
							</view>
							<image class="qrcode" :src="item.qrcode" mode="widthFix"></image>
						</view>
						<view v-else class="code-box">
							<image src="https://t1-1305573081.cos.ap-shanghai.myqcloud.com/wxapp/static/imgs/%E8%AE%A2%E5%8D%95%E5%85%B3%E9%97%AD.png"
							 class="qrcode-loading" mode="widthFix"></image>
						</view>
					</view>
				</loadSke>
				<view class="order-num">
					<text>订单号：{{orderDes.outTradeNo}}</text>
					<text>{{filmDes.orderStatusStr}}</text>
				</view>
				<view class="des">
					<text class="film-name">{{orderDes.orderDataJson.cinemaData.filmName}}</text>
					<view class="film-time">
						<text>时间</text>
						<text>{{orderDes.orderDataJson.cinemaData.showTime}}</text>
					</view>
					<view class="film-address">
						<text>影院</text>
						<text>{{orderDes.orderDataJson.cinemaData.cinemaName}}</text>
					</view>
					<view class="film-video">
						<text>影厅</text>
						<text>{{orderDes.orderDataJson.cinemaData.hallName}}</text>
					</view>
					<view class="film-seat">
						<text>座位</text>
						<view class="seat-box">
							<text v-for="(item,index) in seatList" :key='index'>{{item}}</text>
						</view>
					</view>
				</view>
				<view class="price">
					<text>总价</text>
					<text>¥{{orderDes.total/100}}</text>
				</view>
			</view>
		</loadSke>
	</view>
</template>

<script>
	import {
		getWxOrder
	} from '@/api/order.js'
	import {
		orderQuery
	} from '@/api/cinema.js'
	import QRCode from 'qrcode'
	export default {
		data: () => ({
			orderDes: {},
			seatList: [],
			outTradeNo: null,
			loading: true,
			pollingTiemId: null,
			filmDes: {},
			qrcode: null,
			qrcodeLoading: true,
			ticketType: null
		}),
		onLoad: function(option) {
			this.outTradeNo = option.outTradeNo
		},
		mounted() {
			this.init()
		},
		computed: {},
		methods: {
			async init() {
				let orderRes = await getWxOrder(this.outTradeNo)
				orderRes.data.orderDataJson = JSON.parse(orderRes.data.orderDataJson)
				this.orderDes = orderRes.data
				this.seatList = this.orderDes.orderDataJson.wxOrderCreateDTO.seat.split(',')
				this.ticketType = this.orderDes.orderDataJson.wxOrderCreateDTO.ticketType
				if (this.ticketType == 2) {
					if (this.orderDes.orderDataJson.filmMcpData) {
						this.qrcode = await this.qrcodeGenerate(this.orderDes.orderDataJson.filmMcpData.ticket.slice(4))
					}
				}
				if (this.ticketType != 2) {
					let filmRes = await orderQuery({
						thirdOrderId: this.outTradeNo
					})
					this.filmDes = filmRes.data.data
					if (this.filmDes.orderStatus < 4) {
						this.polling()
					} else if (this.filmDes.orderStatus < 10) {
						this.qrcodeShow()
					}
				}
				this.loading = false
				this.qrcodeLoading = false
			},
			polling() {
				this.pollingTiemId = setInterval(async () => {
					let filmRes = await orderQuery({
						thirdOrderId: this.outTradeNo
					})
					this.filmDes = filmRes.data.data
					if (this.filmDes.orderStatus == 4 || this.filmDes.orderStatus == 5) {
						clearInterval(this.pollingTiemId)
						this.qrcodeShow()
					}
				}, 3000)
			},
			qrcodeShow() {
				let qrcode = []
				this.filmDes.ticketCode.forEach(async val => {
					if (val.type == 1) {
						let qr = await this.qrcodeGenerate(val.code)
						qrcode.push({
							qrcode: qr,
							code: val.code
						})
					}
				})
				this.$set(this.filmDes, 'qrcode', qrcode)
			},
			async qrcodeGenerate(data) {
				let qrcodeRes = await QRCode.toString(data, {
					margin: 0,
					errorCorrectionLevel: 'H'
				});
				return 'data:image/svg+xml;base64,' + Buffer(qrcodeRes).toString('base64');
			}
		},
		beforeDestroy() {
			clearInterval(this.pollingTiemId)
		}
	}
</script>

<style lang="scss">
	.remind {
		margin-top: 36rpx;
		font-size: 26rpx;
		font-weight: 400;
		color: #0F0404;
		text-align: center;
	}

	.des-box {
		width: 564rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: auto;
		margin-top: 30rpx;

		.code-box {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-bottom: 38rpx;

			.code {
				margin-top: 43rpx;

				text {
					font-size: 26rpx;
					font-weight: 400;
					color: #666666;
					margin-right: 20rpx;

					&:nth-child(2) {
						color: #0F0404;
					}
				}
			}

			.qrcode {
				margin-top: 43rpx;
				width: 260rpx;
				height: 260rpx;
			}

			.qrcode-loading {
				margin-top: 43rpx;
				width: 100%;
			}
		}

		.order-num {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 34rpx 31rpx;
			border-top: 1rpx dashed #D9D9D9;
			border-bottom: 1rpx dashed #D9D9D9;
			position: relative;

			&::after {
				content: '';
				width: 24rpx;
				height: 24rpx;
				border-radius: 50%;
				background-color: #F1F1F1;
				position: absolute;
				top: 0;
				left: 0;
				transform: translate(-50%, -50%);
			}

			&::before {
				content: '';
				width: 24rpx;
				height: 24rpx;
				border-radius: 50%;
				background-color: #F1F1F1;
				position: absolute;
				top: 0;
				right: 0;
				transform: translate(50%, -50%);
			}

			text {
				&:nth-child(1) {
					width: 60%;
					word-break: break-all;
					font-size: 26rpx;
					font-weight: 400;
					color: #666666;
				}

				&:nth-child(2) {
					padding: 10rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					background: #FFE5E5;
					border: 1rpx solid #E31818;
					border-radius: 20rpx;
					font-size: 20rpx;
					font-weight: 400;
					color: #E31818;
				}
			}
		}

		.des {
			padding: 45rpx 30rpx;
			border-bottom: 1rpx dashed #D9D9D9;
			display: flex;
			flex-direction: column;

			.film-name {
				font-size: 30rpx;
				font-weight: 400;
				color: #0F0404;
			}

			text {
				font-size: 26rpx;
				font-weight: 400;
				color: #666666;
				margin-right: 20rpx;
				white-space: nowrap;
			}

			.film-time {
				text:nth-child(2) {
					color: #999999;
				}
			}

			.film-address {
				text:nth-child(2) {
					color: #999999;
				}
			}

			.film-video {
				text:nth-child(2) {
					color: #999999;
				}
			}

			.film-seat {
				display: flex;
				justify-content: flex-start;
				align-items: stretch;
				margin-top: 10rpx;

				.seat-box {
					display: flex;
					flex-wrap: wrap;

					text {
						height: 40rpx;
						padding: 4rpx 15rpx;
						margin-bottom: 10rpx;
						background: #EBEBEB;
						border-radius: 4rpx;
						color: #999999;
					}
				}
			}
		}

		.price {
			padding: 40rpx 30rpx;

			text {
				font-size: 26rpx;
				font-weight: 400;
				color: #666666;
				margin-right: 20rpx;

				&:nth-child(2) {
					color: #E31818;
				}
			}
		}
	}
</style>
