<template>
	<view class="container">
		<!-- 二维码区域 -->
		<view class="code">
			<view class="code_top">
				<view class="top_title">◆ 请使用微信扫码 ◆</view>
				<view class="top_code">
					<uv-qrcode ref="qrcode" size="300rpx" :value="QRCodeUrl" @complete="over"></uv-qrcode>
					<view class="code_info">
						<img class="code_log" src="../../static/images/school-logo.jpg" />
						万载三中
					</view>
				</view>
			</view>
			<view class="code_bottom">填写访客信息</view>
		</view>

		<!-- 提示区域 -->
		<view class="tips">提示:微信扫码即可快速进入小程序进行预约</view>

		<!-- 按钮区域 -->
		<view class="btn" @click="handleSave">保存二维码到本地</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'

// 二维码元素标记
const qrcode = ref(null)

// 二维码是否加载完毕标识
const flag = ref(null)

// 二维码信息
const QRCodeUrl = ref('https://chtech.ncjti.edu.cn/testingServer/repairManage/test')

// 二维码加载完成时触发的回调
const over = (e) => {
	flag.value = e.success
}

// 点击保存按钮的回调
const handleSave = () => {
	if (flag.value) {
		qrcode.value.save({
			success: (e) => {
				// console.log(e)
				uni.showToast({
					title: '保存成功',
					icon: 'success'
				})
			},
			fail: (err) => {
				uni.showToast({
					title: '保存失败',
					icon: 'fail'
				})
			}
		})
	} else {
		uni.showToast({
			title: '二维码加载失败，无法保存',
			icon: 'none'
		})
	}
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	padding: 0 40rpx;
	min-height: 100vh;
	background-color: #f1f6fe;

	.code {
		margin-top: 42rpx;
		width: 670rpx;
		height: 795rpx;

		.code_top {
			display: flex;
			flex-direction: column;
			align-items: center;
			height: 679rpx;
			border-radius: 18rpx 18rpx 0 0;
			background-color: rgba(0, 97, 255, 0.8);

			.top_title {
				margin: 50rpx 0 40rpx 0;
				color: #fff;
				font-size: 44rpx;
			}

			.top_code {
				display: flex;
				flex-direction: column;
				justify-content: space-evenly;
				align-items: center;
				width: 406rpx;
				height: 456rpx;
				border-radius: 0 30rpx 0 30rpx;
				background-color: #fff;

				.code_info {
					display: flex;
					align-items: center;
					font-size: 28rpx;

					.code_log {
						margin-right: 18rpx;
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
		}

		.code_bottom {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 116rpx;
			font-size: 38rpx;
			border-radius: 0 0 18rpx 18rpx;
			background-color: #fff;
		}
	}

	.tips {
		margin-top: 70rpx;
		text-align: center;
		color: #a6a6a6;
	}

	.btn {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 80rpx;
		width: 670rpx;
		height: 100rpx;
		font-size: 32rpx;
		color: #fff;
		border-radius: 8rpx;
		background-color: #0061ff;
	}
}
</style>
