<template>
	<view>
		<u-navbar title="付款码">
		</u-navbar>
		<view class="content">
			<!-- 饭卡信息 -->
			<view class="head">
				<view class="person">
					<view class="photo">
						<img v-if="userInfo.headImg" :src="userInfo.headImg" alt="">
						<u-avatar v-else size="48"></u-avatar>
					</view>
					<view class="name">
						{{userInfo.name}}
					</view>
				</view>
				<view class="balance">
					<view class="title">
						饭卡余额：
					</view>
					<view class="value">
						{{balance}} 元
					</view>
				</view>
			</view>
			<!-- 二维码 -->
			<view class="qr-code ">
				<view class="img">
					<div id="qrCodeDiv" ref="qrCodeDiv"></div>
				</view>
				<!-- <view class="img">
					<img src="../../assets/img/qrCode.png" alt="">
					<view class="qr-logo">
						<img src="../../assets/img/qrLogo.png" alt="">
					</view>
				</view> -->
				<view class="hint">
					将二维码对准摄像头，即可付款就餐
				</view>
			</view>
			<!-- 付款中 -->
			<view class="paying" v-if="isPaying">
				<view class="loading-img">
					<img src="../../assets/img/if-spinner@1x.png" alt="">
				</view>
				<view class="">
					付款中...
				</view>
			</view>
			<!-- 我的消费记录 -->
			<view class="consumption-record" @click="gotoUrl('pages/mine/myMealCard')">
				<view class="icon">
					<img src="../../assets/img/riLine-file-list-3-line@1x.png" alt="">
				</view>
				<view class="text">
					我的消费记录
				</view>
				<view class="right">
					<u-icon name="arrow-right" color="#999999"></u-icon>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import * as mineAPI from '@/apis/pagejs/mine.js'
	import QRCode from 'qrcodejs2'

	export default {
		components: {
			QRCode
		},
		data() {
			return {
				userInfo: {},
				balance: '',
				isPaying: false,
				card: '',
				time: ''
			}
		},
		onReady() {
			this.userInfo=this.carhelp.getPersonInfo();
			
			this.getLoadCardData();
			this.getCardAndTime();
		},
		methods: {
			getCardAndTime() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				mineAPI.getCardInfo().then((response) => {
					uni.hideLoading();
					this.card = response.data.cardId;
					
					var date = new Date();
					var year = date.getFullYear();
					var month = date.getMonth() + 1;
					var day = date.getDate();
					var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
					var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
					var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
					month >= 1 && month <= 9 ? (month = "0" + month) : "";
					day >= 0 && day <= 9 ? (day = "0" + day) : "";
					this.time = year + month + day + hour + minute + second;
					
					this.getQrcode();
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getPayAmount() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				mineAPI.payAmount().then((response) => {
					uni.hideLoading();
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getLoadCardData() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				mineAPI.loadCardData().then((response) => {
					uni.hideLoading();
					this.balance = response.data.balance;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getQrcode() {
				this.$nextTick(() => {
					let div = document.createElement('div');// 创建一个div,用来生成二维码
					// 生成二维码
					let qrcode = new QRCode(div, {
						text: 'card='+this.card+'&time='+this.time, // 你的扫码内容,填网址
						width: 200, // 二维码宽度 
						height: 200, // 二维码高度
						colorDark: "#333333", //二维码颜色
						colorLight: "#ffffff", //二维码背景色
						correctLevel: QRCode.CorrectLevel.H, //从上至下生成码密度越来越高 L - M - Q - H
						// 容错率越高,越复杂
					})
					let logo = new Image();
					logo.crossOrigin = 'Anonymous';
					logo.src = require("@/assets/img/qrLogo.png") // 填入你本地log图片
					// 生成log图
					logo.onload = () => {
						let container = this.$refs['qrCodeDiv']; 
						// 获取页面上的div,可以使用document.querySelector()等等方法,不类推了
						if (container.innerHTML != "") {
							// 获取页面div , 有则清空已存在的
							container.innerHTML = ""
						}
						let qrImg = qrcode._el.getElementsByTagName('img')[0]; // 获取二维码
						let canvas = qrcode._el.getElementsByTagName('canvas')[0]; // 获取canvas
				
						let ctx = canvas.getContext("2d");
						ctx.drawImage(logo, 200 * 0.5 - 22, 200 * 0.5 - 22, 56, 56); // 写入log
						qrImg.src = canvas.toDataURL();
						container.appendChild(qrcode._el);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	// #qrCodeDiv {
	// 	width: 400rpx;
	// 	height: 400rpx;
	// 	position: relative;
	// 	// margin: 0 144rpx;
	// 	padding: 0 144rpx 0 144rpx;
	// }
	
	.loading-img{
		animation: rotation 2s linear infinite;
	}
	@keyframes rotation{
		from {transform: rotate(0deg)}
		to {transform: rotate(360deg)}
	}
	
	page {
		background-color: #2A8EFB;
		font-family: 'PingFang Regular';
	}

	/deep/.u-navbar {
		background-color: #2A8EFB !important;

	}

	/deep/.u-title {
		color: #fff !important;
		font-weight: bold !important;
	}

	/deep/.uicon-nav-back {
		color: #fff !important
	}

	/deep/.u-border-bottom:after {
		border-bottom-width: 0px;
	}

	.content {
		border-radius: 24rpx;
		background-color: rgba(255, 255, 255, 1);
		margin: 48rpx 32rpx;

		// 饭卡信息
		.head {
			padding: 32rpx 0;
			margin: 0 32rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid rgba(232, 232, 232, 1);

			.person {
				display: flex;
				align-items: center;

				.photo {
					width: 48rpx;
					height: 48rpx;
					border-radius: 100rpx;
					overflow: hidden;

					img {
						width: 100%;
						height: 100%;
					}
				}

				.name {
					color: rgba(51, 51, 51, 1);
					font-size: 36rpx;
					margin-left: 16rpx;
				}
			}

			.balance {
				display: flex;
				align-items: center;
				color: rgba(16, 16, 16, 1);
				font-size: 32rpx;
				font-weight: bold;
			}
		}

		// 二维码
		.qr-code {
			padding: 80rpx 0;

			.img {
				// width: 400rpx;
				// height: 400rpx;
				// position: relative;
				// margin: 0 144rpx;
				padding: 0 144rpx 0 144rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				img {
					width: 100%;
					height: 100%;
				}
			}

			.qr-logo {
				width: 112rpx;
				height: 112rpx;
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				margin: auto;

				img {
					width: 100%;
					height: 100%;
				}
			}

			// 付款中

		}

		.hint {
			margin-top: 16rpx;
			color: rgba(51, 51, 51, 1);
			text-align: center;
		}

		//我的消费记录
		.consumption-record {
			display: flex;
			align-items: center;
			padding: 32rpx 0;
			margin: 0 32rpx;
			border-top: 1px solid rgba(232, 232, 232, 1);

			.icon {
				width: 40rpx;
				height: 40rpx;

				img {
					width: 100%;
					height: 100%;
				}
			}

			.text {
				color: rgba(51, 51, 51, 1);
				font-size: 32rpx;
				margin-left: 8rpx;
			}

			.right {
				margin-left: auto;
			}
		}
	}

	// 付款中
	.paying {
		width: 160rpx;
		height: 160rpx;
		border-radius: 8px;
		background-color: rgba(0, 0, 0, 1);
		color: #fff;
		padding: 22rpx 0;
		text-align: center;
		position: absolute;
		top: 440rpx;
		left: 0;
		right: 0;

		margin: auto;

		img {
			width: 72rpx;
			height: 72rpx;
		}
	}

	// 透明
	.opacity {
		opacity: 0.3;
	}
</style>