<template>
	<payPopup ref="clearPwd" :show.sync="show" @getPassword="getPassword"></payPopup>
</template>

<script>
	import {
		verifyCode,
		parseReceiveCode,
		parsePayCode,
		createReceive,
		genPayCode
	} from '@/api/home/qrcode.js'
	import {
		getUserInfo,getUserAccInfo
	} from '@/api/login.js'
	import {
		checkPwd
	} from '@/api/mine/payPwd.js'

	import payPopup from '@/components/keyboard/payPopup.vue'
	export default {
		components: {
			payPopup
		},
		data() {
			return {
				payAmt: 0,
				show: false,
				amount: null,
				payCodeParam: {
					toId: null,
					toAccount: null
				},
				param: {
					fromId: null,
					fromAccount: null,
					toId: null, //目标id
					toAccount: null, //目标账户
					amount: null
				},
				paramPwd: {
					payPasswd: null
				}
			}
		},
		methods: {
			scanQRCode() {
				let _this = this;
				uni.scanCode({
					success: (res) => {
						if (res.result) {
							// 处理扫码结果
							_this.checkQrCode(res.result)
						} else {
							uni.showToast({
								title: "扫描失败",
								icon: "none",
							});
						}
					},
					fail: (err) => {
						if (err.errMsg.includes("cancel")) {
							uni.showToast({
								title: "扫描已取消",
								icon: "none",
							});
						} else {
							uni.showToast({
								title: "调用相机失败",
								icon: "none",
							});
						}
					},
				});
			},

			// 校验二维码合法性结果
			checkQrCode(code) {
				
				verifyCode(code).then(res => {
					if (res.code == 200) {
						let codeArray = code.split("@");
						//提取场景
						if (codeArray[0] == 'R') {
							//R 收款
							//调用付款码解析
							this.getReceiveCode(code);
						} else if (codeArray[0] == 'P') {
							//P 支付 
							//调用收款码解析
							this.getParsePayCode(code);
						}
					}
				})
			},
			//收款码解析
			getReceiveCode(code) {
				parseReceiveCode(code).then(res => {
					if (res.code == 200) {
						//解析成功 跳转金额输入页面
						if (res.data.hasAmount == "true") {
							this.receiveToolBtnAmount(res.data.merchantId, code, res.data.amount);
						} else {
							this.receiveToolBtn(res.data.merchantId, code);
						}

					}
				})
			},
			//付款码解析
			getParsePayCode(code) {
				
				parsePayCode(code).then(res => {
					if (res.code == 200) {
						//解析成功 跳转金额输入页面
						this.payToolBtn(res.data.merchantId, code);
					} else {
						uni.showToast({
							title: "收款码已过期",
							icon: "none",
						});
					}
				})
			},
			//付款跳转
			payToolBtn(merchantId, fromAccount) {
				//merchantId：付款人id，fromAccount：付款人的付款码
				uni.navigateTo({
					url: '/pages/account/setAmount?type=P' + '&merchantId=' + merchantId + '&' +
						'fromAccount=' + fromAccount
				})
			},
			//收款跳转
			receiveToolBtn(toId, toAccount) {
				//toId：目标账户id，toAccount：目标账户收款码
				uni.navigateTo({
					url: '/pages/account/setAmount?type=R' + '&toId=' + toId +
						'&' + 'toAccount=' + toAccount
				})
			},
			receiveToolBtnAmount(toId, toAccount, amount) {
				//toId：目标账户id，toAccount：目标账户收款码
				this.param.toId = toId;
				this.param.toAccount = toAccount;
				this.param.amount = amount;
				//直接调用支付方法
				this.getUserInfo();
			},
			getUserInfo() {
				getUserAccInfo().then(res => {
					if (res.code == 200) {
						//来源
						this.payCodeParam.toId = res.data.userId;
						//来源账号
						this.payCodeParam.toAccount = res.data.accountId;
						this.param.fromId = res.data.userId;
						//生成付款码
						this.genPayCode();
					} else {
						uni.showToast({
							title: "获取用户信息异常",
							icon: "none",
						});
					}
				})
			},
			genPayCode() {
				//生成来源账户付款码
				genPayCode(JSON.stringify(this.payCodeParam)).then(res => {
					if (res.code == 200) {
						this.param.fromAccount = res.data;
						//调用输入支付密码弹窗
						this.show = true;
					}
				})
			},
			submit() {
				//创建扫码收款订单
				createReceive(JSON.stringify(this.param)).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: "支付成功",
							icon: "none",
						});
						
						//清除密码
						this.claearPwd();
						//关闭支付密码弹窗
						this.show = false;
						this.closeBack(this.param.amount);
					}
				})

			},
			//获取用户输入的支付密码
			getPassword(data) {
				//验证支付密码
				this.getChangePwd(data.password);

			},
			getChangePwd(pwd) {
				//TODO 
				this.submit();
				return
				this.paramPwd.payPasswd = pwd;
				checkPwd(this.paramPwd).then(res => {
					if (res.data != null) {
						//支付
						this.submit();
					} else {
						//密码错误
						this.$nextTick(() => {
							this.$refs.clearPwd.errorPwd();
						})
					}
				})
			},
			claearPwd() {
				//密码重置
				this.$nextTick(() => {
					this.$refs.clearPwd.clearPwd();
				})
			},
			closeBack(amount) {
				// this.showResult = true
				let obj = {
					number:this.param.amount,
					type: 'P',
					success:true
				}
				this.param.amount = null
				uni.navigateTo({
					url: `/components/payResult/indexNew?data=${encodeURIComponent(JSON.stringify(obj))}`
				})
				
				// setTimeout(() => {
				// 	uni.switchTab({
				// 		url: '/pages/home/index'
				// 	})
				// }, 2000)
			},
		}
	}
</script>

<style>
</style>