<template>
	<view v-if="result">
		<view class="tips">
			<image class="image" src="/static/success.png" mode=""></image>
			<text class="text">支付成功，祝您入住愉快！</text>
		</view>
		<view class="confirm-button">
			<button class="button" @click="gotoHome">{{buttonText}}</button>
		</view>
	</view>
</template>

<script setup>
	import moment from 'moment'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		computed,
		ref
	} from 'vue'
	import {
		useOrderStore
	} from '@/store/orderStore'
	import {
		useCheckinInfoStore
	} from '@/store/checkinInfoStore'
	let time = ref(3)
	let buttonText = computed(() => {
		return '确认(' + time.value + ')'
	})
	let orderStore = useOrderStore()
	let orderInfo = orderStore.orderInfo
	let checkinInfoStore = useCheckinInfoStore()
	let checkinInfo = checkinInfoStore.checkinInfo
	let result = ref(false)
	let countDownTimer
	async function checkin() {
		try {
			uni.showLoading({
				title: '正在办理入住...'
			})
			let info = {
				hotelId: orderInfo.hotelId,
				building: orderInfo.building,
				floorId: orderInfo.floor,
				roomId: orderInfo.room,
				startTime: moment(new Date()).format('YYMMDDHHmmss'),
				endTime: moment(orderInfo.endTime).format('YYMMDDHHmmss'),
				cardId: '0',
				userType: 1,
				orderId: orderInfo.orderId,
				isResv: orderInfo.orderSource !== 'MINI_APP'
			}
			let checkinResult = new Array(checkinInfo.length).fill(false)
			for (let i = 0; i < checkinInfo.length; i++) {
				if (i == 0) {
					info.isMainCustomer = true
					info.totalCustomerNum = checkinInfo.length
				} else {
					info.isMainCustomer = false
				}
				info.userName = checkinInfo[i].name
				info.userId = checkinInfo[i].idNumber
				info.faceData = checkinInfo[i].faceData
				info.userPhone = checkinInfo[i].phone
				info.address = checkinInfo[i].address
				info.race = checkinInfo[i].nation
				info.userGender = checkinInfo[i].sex === '男' ? 1 : 2
				info.birth = checkinInfo[i].birth
				info.issuingAuthority = checkinInfo[i].issuingAuthority
				info.issuingDate = checkinInfo[i].issuingDate
				info.expiryDate = checkinInfo[i].expiryDate
				let res = await uni.request({
					url: '/scm/checkin',
					method: 'POST',
					data: info
				})
				if (res.data.success) {
					checkinResult[i] = true
				}
			}
			let falsePos = checkinResult.indexOf(false)
			if (falsePos < 0) {
				result.value = true
				countDownTimer = setInterval(() => {
					time.value--
					if (time.value === 0) {
						clearInterval(countDownTimer)
						gotoHome()
					}
				}, 1000)
			} else {
				let failName = checkinInfo[falsePos].name
				uni.showModal({
					showCancel: false,
					content: '办理失败，请重新选择订单',
					success: (res) => {
						if (res.confirm) {
							uni.switchTab({
								url: '/pages/home/home'
							})
						}
					}
				})
			}
		} finally {
			uni.hideLoading()
		}
	}

	function gotoHome() {
		clearInterval(countDownTimer)
		uni.switchTab({
			url: '/pages/home/home'
		})
	}

	onLoad(async () => {
		await checkin()
	})
</script>

<style lang="scss">
	.tips {
		margin-top: 200rpx;
		display: flex;
		flex-direction: column;

		.image {
			width: 200rpx;
			height: 200rpx;
			margin: 0 auto;
		}

		.text {
			color: #666666;
			font-size: 40rpx;
			font-weight: bold;
			text-align: center;
		}
	}

	.confirm-button {
		margin-top: 200rpx;

		.button {
			width: 80%;
			margin: 0 auto;
			color: #ffffff;
			background-color: #9e97c3;
		}
	}
</style>