<template>
	<view style="height:100%">
<!-- 		<view v-if="!showResult">
			<NumBericKeyBoard @getAmount="getAmount"></NumBericKeyBoard>
			<payPopup ref="clearPwd" :show.sync="show" @getPassword="getPassword"></payPopup>
		</view>

		<payResult :show="showResult" :success="success" :number="param.amount"></payResult> -->

		<NumBericKeyBoard style="background-color: aliceblue;" @getAmount="getAmount"></NumBericKeyBoard>
		<payPopup ref="clearPwd" :show.sync="show" @getPassword="getPassword"></payPopup>

	</view>
</template>

<script>
	import {
		createPay,
		createReceive,
		createTransfer,
		genReceiveCode,
		genPayCode
	} from '@/api/home/qrcode.js'

	import {
		getUserAccInfo
	} from '@/api/login.js'

	import {
		checkPwd
	} from '@/api/mine/payPwd.js'

	import NumBericKeyBoard from '@/components/keyboard/numericKeyboard.vue'
	import payPopup from '@/components/keyboard/payPopup.vue'
	import payResult from '@/components/payResult/index.vue'
	export default {
		components: {
			NumBericKeyBoard,
			payPopup,
			payResult
		},
		data() {
			return {
				success:true,
				show: false,
				payee: {
					type: null,
					userId: null,
					phonenumber: null
				},
				isValidAmount: false,
				receiveCodeParam: {
					fromId: null,
					fromAccount: null
				},
				payCodeParam: {
					toId: null,
					toAccount: null
				},
				param: {
					fromId: null,
					fromAccount: null,
					toId: null, //目标id
					toAccount: null, //目标账户
					amount: null
				},
				paramPwd: {
					payPasswd: null
				},
				showResult:false
			}
		},
		onLoad(e) {
			this.payee.type = e.type;
			if (e.type == "TRANSFER_MONEY") {
				//收款人信息   只有在转账时使用
				this.payee.userId = e.userId;
				this.payee.phonenumber = e.phonenumber;
				//付款人信息
				this.param.fromId = e.merchantId;
				this.param.fromAccount = e.fromAccount;
			} else if (e.type == "P") {
				//付款人信息
				this.param.fromId = e.merchantId;
				this.param.fromAccount = e.fromAccount;
			} else if (e.type == "R") {
				//收款人信息
				this.param.toId = e.toId;
				this.param.toAccount = e.toAccount;
			}
		},
		created() {
			uni.setNavigationBarTitle({
				title: this.$languageData.navBar.setAmount
			});
		},
		methods: {
			getAmount(data) {
				this.param.amount = data;
				this.getUserInfo()
			},
			validateAmount() {
				const num = parseFloat(this.amount);
				if (isNaN(num) || num <= 0) {
					this.errorMessage = '请输入有效的收款金额';
					this.isValidAmount = false;
				} else {
					this.errorMessage = '';
					this.isValidAmount = true;
				}
			},
			closeBack(flag) {
				// this.showResult = true
				let obj = {
					number:this.param.amount,
					type: flag,
					success:true
				}
				this.param.amount = null
				uni.navigateTo({
					url: `/components/payResult/indexNew?data=${encodeURIComponent(JSON.stringify(obj))}`
				})
				// setTimeout(() => {
				// 	uni.switchTab({
				// 		url: '/pages/home/index'
				// 	})
				// }, 1000)
			},
			getUserInfo() {
				if (this.payee.type == 'S') {
					//设置金额
					this.$store.commit('SET_AMOUNT', this.param.amount)
					uni.navigateTo({
						url: "/pages/account/personalPaymentCode"
					})
				} else if (this.payee.type == 'TRANSFER_MONEY') {
					//转账
					//目标id
					this.receiveCodeParam.fromId = this.payee.userId;
					//目标账号
					this.receiveCodeParam.fromAccount = this.payee.accountId;
					this.genReceiveCode();
				} else {
					getUserAccInfo().then(res => {
						if (res.code == 200) {
							if (this.payee.type == "P") {
								//来源id
								this.receiveCodeParam.fromId = res.data.userId;
								//来源账号
								this.receiveCodeParam.fromAccount = res.data.accountId;
								this.param.toId = res.data.userId;
								this.genReceiveCode();
							} else {
								//来源
								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",
							});
						}
					})
				}
			},
			genReceiveCode() {
				//生成目标账户收款码
				console.log("111111111")
				genReceiveCode(JSON.stringify(this.receiveCodeParam)).then(res => {
					if (res.code == 200) {
						this.param.toAccount = res.data;
						//调用输入支付密码弹窗
						//生成订单
						this.submit();
					}
				})
			},
			genPayCode() {
				//生成来源账户付款码
				genPayCode(JSON.stringify(this.payCodeParam)).then(res => {
					if (res.code == 200) {
						this.param.fromAccount = res.data;
						//调用输入支付密码弹窗
						this.show = true;
					}
				})
			},
			submit() {
				if (this.payee.type == 'P') {
					//创建扫码付款订单
					createPay(JSON.stringify(this.param)).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: "收款成功",
								icon: "none",
							});
							this.submitOk();
							this.closeBack('P');
						}else{
							this.success = false
							this.closeBack('P');
						}
					})
				} else if (this.payee.type == 'TRANSFER_MONEY') {
					//创建转账订单
					this.param.toId = this.payee.userId;
					createTransfer(JSON.stringify(this.param)).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: "转账成功",
								icon: "none",
							});
							this.submitOk();
							this.closeBack('T');
						}else{
							this.success = false
							this.closeBack('T');
						}
					})
				} else {
					//创建扫码收款订单
					createReceive(JSON.stringify(this.param)).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: "支付成功",
								icon: "none",
							});
							this.submitOk();
							this.closeBack('R');
						}else{
							this.success = false
							this.closeBack('R');
						}
					})
				}

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

			},
			getChangePwd(pwd) {
				this.paramPwd.payPasswd = pwd;
				//TODO
				this.submit()
				return;
				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();
				})
			},
			submitOk(){
				//清除密码
				this.claearPwd();
				//关闭支付密码弹窗
				this.show = false;
			}
		},

	}
</script>
<style lang="scss" scoped>
	::v-deep .u--input {
		font-size: 30px !important;
		/* 使用 !important 确保覆盖 */
	}

	::v-deep .u-form-item__body__left__content__label {
		font-size: 30px;
	}
</style>