<template>
	<view class="container">
		<view class="ReflectBox">
			<view class="left_">提现金额 </view>
			<view class="right_">每日可提现一次</view>
		</view>
		<view class="OtherMoney" v-if="true">
			<span>￥</span>
			<input class="TxClsTx" v-model="inputOtherM" @input="inputControl" @blur="ReturnToTop"
				:placeholder="placeholder" placeholder-class="placeholderinputOtherM" type="text" maxlength="6" />
			<span class="ALL" @click="TxAllFun">全部</span>
		</view>
		<!-- 提现账户 -->
		<view class="payType" @click="queryBindCardList">
			<view>提现账户</view>
			<view>{{bankInfo_.accountName}}({{bankInfo_.accountNo.slice(bankInfo_.accountNo.length-4)}}) <span
					class="arrow"></span></view>
		</view>
		<view class="OtherMoney" v-if="false" style="color: #BFBFBF;font-weight: 500;">
			每日仅可提现一次，请明日再来。
		</view>
		<view class="bottomdiv">
			<view class="DepositID" @click="toDeposit(0)" v-bind:style="inputOtherM>0?'':'opacity: 0.5 !important;'">
				<span>提现</span>
			</view>
			<view class="switchbankCard" @click="BackToMini('TxHistory')">提现记录</view>
		</view>
		<!-- 弹出层 -->
		<uni-popup ref="popup" :mask-click="false">
			<!-- 充值确认 -->
			<view class="RechargeConfirmationBox">
				<view class="RechargeInfo">
					<view class="closePhoneBox" @click="close()">
						<image src="../../../static/img/close.png" mode="aspectFit"></image>
					</view>
					<view class="RechargeInfo1">
						验证校园卡开卡手机号
					</view>
					<van-field label-class="openCardPhonTxt" label="手机号:" maxlength="11" v-model="openCardPhon"
						placeholder="校园卡开卡手机号" center :border="false" />
					<view style="width: 520rpx;height: 1rpx;background: #EEEEEE;"></view>
					<van-field v-model="openCardPhonSms" label-class="openCardPhonSmsTxt" maxlength="6" label="验证码:"
						placeholder="输入短信验证码" center clearable :border="false">
						<van-button slot="button" size="small" type="primary" @click="SendSms">
							<div style="font-size:28rpx;">{{CodeMsg}}</div>
						</van-button>
					</van-field>
					<view style="width: 520rpx;height: 1rpx;background: #EEEEEE;"></view>
					<!-- 取消 确认充值按钮 -->
					<view class="BtngGoup">
						<view class="" @click="ExchangeWithdraw">
							提现
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		Dialog,
		Toast
	} from 'vant';
	import keyboardPackage from "@/components/keyboard-package/keyboard-package.vue"
	import cityData from "@/common/js/city.js"
	var gHost = "https://" + window.location.host;
	if (window.location.protocol != "https:") {
		gHost = "https://fpdev.xiaopay.net";
	}
	let times = 0;
	var limitClick = false; //禁止重复点击
	var sessionID = '',
		userID = '';
	var payCardNumber = ''; //一类卡号
	var orderSerialNo = ''; //提现结果查询 指令号
	var businessNo = '',
		otpOrderNo = '',
		mobileNo = '';
	var msCount = 60; //定时器
	var MyTimer = null;
	var fromType = 0; //0转账  1提现
	var opeType = 0; //0充值  1提现   手机号验证
	var bankInfo = {
		acctNoArray: '',
		idNo: '',
		schoolId: '',
		trueName: '',
		userId: '',
		sessionid: '',
		wechattype: 1
	}
	export default {
		components: {

		},
		name: "Reflect",
		data() {
			return {
				Money: '',
				inputOtherM: '',
				ImgUrl: this.$ImgUrl,
				ReflectMoney: 3,
				cardBalanceTx: '',
				placeholder: "可提现金额0.00元",
				bankInfo_: {
					accountNo: '**** **** **** 1234'
				},
				/**
				 * 20211129新增
				 */
				// 充值手机号码
				openCardPhon: "",
				openCardPhonSms: "",
				msCount: 60, //定时器
				CodeMsg: "获取验证码",
				RechargeSmsorderNoInfo: {} //第三方合作交易标识
			}
		},
		onLoad: function(options) {
			console.log(options);
			// 初始化
			userID = options['userID'] || '';
			sessionID = options['sessionID'] || '';
			fromType = options['fromType'] || '';
			bankInfo.sessionid = sessionID || '';
			this.QueryUserInfo()
		},
		created: function() {

		},
		mounted: function() {

		},
		methods: {
			// 充值验证获取验证码
			async SendSms() {
				if (MyTimer) {
					return
				}
				if (!this.openCardPhon || this.openCardPhon.length !== 11) {
					Dialog({
						title: '系统提示',
						message: '请收入正确的手机号码！'
					});
					return
				}

				let this_ = this;
				let loading = Toast.loading({
					message: '加载中...',
					forbidClick: true,
					loadingType: 'spinner',
					duration: 0
				});
				$.ajax({
					url: gHost + '/bank-wechat/bocom/exchange/withdrawSms',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						amount: (this.inputOtherM * 1).toFixed(2), //充值金额，单位元
						schoolId: bankInfo.schoolId, //学校id
						userId: userID, //用户id
						sessionid: sessionID,
						wechattype: 1,
					},
					headers: {
						'sessionid': sessionID,
					},
					success: function(res5) {
						loading.clear();
						if (res5.code == "10000") {
							this_.IntervalFun();
							MyTimer = window.setInterval(this_.IntervalFun, 1000);
							this_.RechargeSmsorderNoInfo = res5.results ? res5.results : {}
						} else {
							weui.topTips(res5.message)
							uni.showModal({
								title: "系统提示",
								content: res5.message
							})
						}
					},
					error: function(err) {
						loading.clear();
					},
				});
			},
			//定时器
			IntervalFun() {
				if (this.msCount == 1) {
					window.clearInterval(MyTimer); //停止计时器
					MyTimer = null
					this.CodeMsg = "获取验证码"
					this.msCount = 60;
				} else {
					this.msCount--;
					this.CodeMsg = this.msCount + "s后获取"
				}
			},
			// 验证码弹出框关闭
			close() {
				this.$refs.popup.close()
			},
			//全部提现
			TxAllFun() {
				let this_ = this
				this.inputOtherM = this.cardBalanceTx * 1
				this.$nextTick(function() {
					// if (this_.inputOtherM == '' || this_.inputOtherM * 1 < 3) {
					// 	$('.DepositID').attr('style', 'background: rgb(230, 230, 230) !important;');
					// } else {
					// 	$('.DepositID').removeAttr('style');
					// }
				})
			},
			//查询一类卡绑定列表
			queryBindCardList() {
				return
				let this_ = this;
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/bank-wechat/query/account',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						schoolId: bankInfo.schoolId,
						userId: userID,
						sessionid: sessionID,
						wechattype: 1
					},
					success: function(res4) {
						loading.hide();
						if (res4.code == "10000") {
							this_.bindcardList = res4.results.accountList[0].bindcardList
							this_.popshow = true
						} else {
							weui.topTips(res4.message)
						}
					},
					error: function(err) {
						loading.hide();
					},
				});
			},
			// 提现
			async toDeposit() {
				if (!this.inputOtherM) {
					uni.showModal({
						title: "系统提示",
						content: "请输入要提现的金额!"
					})
					return
				}
				if (this.inputOtherM * 1 < 0.01) {
					weui.topTips('提现金额须大于等于0.01元');
					return
				}
				if (this.inputOtherM * 1 > this.ReflectMoney) {
					weui.topTips(`提现金额不可大于账户余额`);
					return
				}
				this.$refs.popup.open('top')
			},
			// 提现
			async ExchangeWithdraw() {
				let this_ = this;
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/bank-wechat/bocom/exchange/withdraw',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						amount: (this.inputOtherM * 1).toFixed(2), //充值金额，单位元
						orderNo: this_.RechargeSmsorderNoInfo.orderNo,
						msgCodeSeq: this_.RechargeSmsorderNoInfo.msgCodeSeq,
						msgVerCode: this_.openCardPhonSms,
						schoolId: bankInfo.schoolId,
						userId: userID,
						sessionid: sessionID,
						wechattype: 1,
					},
					headers: {
						'sessionid': sessionID,
					},
					success: function(res5) {
						loading.hide();
						if (res5.code == "10000") {
							wx.showModal({
								title: '提现申请已提交',
								content: '银行处理需要1-5分钟时间，充值结果以余额查询为准。',
								showCancel: false,
								success: function() {
									this_.ExchangeWithdrawQuery();
								}
							})
						} else {
							weui.topTips(res5.message)
						}
					},
					error: function(err) {
						loading.hide();
					},
				});
			},
			// 充值结果查询
			async ExchangeWithdrawQuery() {
				let this_ = this;
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/bank-wechat/bocom/exchange/withdrawQuery',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						orderNo: this_.RechargeSmsorderNoInfo.orderNo,
						schoolId: bankInfo.schoolId,
						userId: userID,
						sessionid: sessionID,
						wechattype: 1
					},
					headers: {
						'sessionid': sessionID,
					},
					success: function(res5) {
						loading.hide();
						if (res5.code == "10000") {
							let appStatus = res5.results.status;
							if (appStatus == 'S') {
								uni.showModal({
									title: "系统提示",
									content: "提现成功!"
								})
								uni.navigateBack({})
							} else {
								uni.showModal({
									title: "系统提示",
									content: appStatus == 'P' ? '提现处理中' : "提现失败"
								})
							}

						} else {
							weui.topTips(res5.message)
							uni.showModal({
								title: "系统提示",
								content: res5.message
							})
						}
					},
					error: function(err) {
						loading.hide();
					},
				});
			},
			// 输入限制
			inputControl(e) {
				let this_ = this
				let obj = e.detail
				this.$nextTick(function() {
					obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
					obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
					obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
					obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); //只允许输入一个小数点
					obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
					obj.value = obj.value * 1 > 2000 ? 2000 : obj.value;
					// if (obj.value == '' || obj.value * 1 < 3) {
					// 	$('.DepositID').attr('style', 'background: rgb(230, 230, 230) !important;');
					// } else {
					// 	$('.DepositID').removeAttr('style');
					// }
					this_.inputOtherM = obj.value
				})
			},
			//input键盘弹起不会弹
			ReturnToTop() {
				this.$nextTick(function() {
					document.scrollingElement.scrollTo(0, this.scrollTop);
				})
			},
			// 复制我卡号
			async CopyCardNumber() {
				let value = "sdfsdf"
				Dialog({
					title: '系统提示',
					message: '复制成功！'
				});
				this.$nextTick(function() {
					let clipboard = new ClipboardJS('#cardNumright', {
						text: function() {
							return value
						}
					})
				})
			},
			//选择输入其他金额
			OtherMoney() {
				this.depotMoneyother = true
			},
			// 快捷选择金额
			async depotMoney(value, MoneyIndex) {
				console.log(value)
				if (!value) {
					this.isOtherMoney = true
					this.Money = ''
					this.MoneyIndex = MoneyIndex
					return
				}
				this.isOtherMoney = false
				this.Money = parseInt(value)
				this.MoneyIndex = MoneyIndex
			},
			//按钮操作
			HideHTML(dom, type) {
				if (type == 0) {
					$('.' + dom).css('display', 'none');
				} else {
					$('.' + dom).css('display', 'block');
				}
			},
			//返回小程序 跳转指定页面
			BackToMini(page) {
				if (page == 'UserMain' || page == 'personalCenter') {
					jWeixin.miniProgram.switchTab({
						url: `/pages/${page}/main`,
					});
				} else {
					jWeixin.miniProgram.redirectTo({
						url: `/pages/${page}/main`,
					});
				}
			},
			//换绑卡
			ChangeCardPay(bankName, bindCard) {
				let this_ = this;
				if (bindCard == payCardNumber) {
					return
				}
				var loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/bank-wechat/user/bank/change/bind',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						schoolId: bankInfo.schoolId,
						userId: userID,
						sessionid: sessionID,
						wechattype: 1,
						accountName: bankName,
						accountNo: bindCard,
					},
					success: function(res5) {
						loading.hide();
						if (res5.code == "10000") {
							this_.HideHTML('showBindCardList', 0);
							this_.queryUserBankInfo();
						} else {
							weui.topTips(res5.message)
						}
					},
					error: function(err) {
						loading.hide();
					},
				});
			},
			//获取银行余额
			queryBankInfo() {
				let this_ = this;
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/bank-wechat/bocom/balance/getBankBlanceAndSyncLocal',
					type: "POST",
					timeout: 8000,
					contentType: "application/x-www-form-urlencoded",
					data: {
						mediumId: bankInfo.accountNo || '',
						schoolId: bankInfo.schoolId || '',
						userId: bankInfo.userId || '',
						sessionid: sessionID,
						wechattype: 1,
					},
					success: function(res3) {
						loading.hide();
						if (res3.code == "10000") {
							if (res3.results.accountBalance == null) {
								weui.topTips("账户信息为空，余额查询失败");
							} else {
								this_.ReflectMoney = res3.results.accountBalance * 1
								this_.cardBalanceTx = (res3.results.accountBalance * 1).toFixed(2);
								$('.TxClsTx').attr('placeholder',
									`可提现金额${(res3.results.accountBalance * 1).toFixed(2)}元`
								)
								this_.RechargeMax = ((2000 - ((res3.results.accountBalance * 1).toFixed(2) *
									1)).toFixed(2));
								this_.placeholder = `可提现金额${(res3.results.accountBalance * 1).toFixed(2)}元`
							}
						} else {
							weui.topTips(res3.message)
						}
					},
					error: function(err) {
						loading.hide();
					},
					complete: function(XMLHttpRequest, status) {
						loading.hide();
						if (status == 'timeout') {
							weui.topTips("请求超时，余额查询失败");
						}
					}
				})
			},
			//获取用户已填写银行信息
			queryUserBankInfo() {
				let this_ = this;
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/bank-wechat/user/bank/get',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						schoolId: bankInfo.schoolId,
						userId: userID,
						sessionid: sessionID,
						wechattype: 1
					},
					success: function(res2) {
						if (res2.code == "10000") {
							bankInfo = res2.results;
							bankInfo.sessionid = sessionID;
							this_.bankInfo_ = bankInfo
							// 20211129
							this_.openCardPhon = bankInfo.mobileNo
							this_.queryBankInfo();
						} else {
							weui.topTips(res2.message)
						}
					},
					error: function(err) {
						loading.hide();
					},
				});
			},
			//查询用户信息
			QueryUserInfo() {
				let this_ = this;
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/wechat/svweapp/QueryPersonalInfo',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						userid: userID,
						sessionid: sessionID,
						wechattype: 1
					},
					success: function(res1) {
						loading.hide();
						if (res1.code == "10000") {
							bankInfo.schoolId = res1.results.schoolid;
							bankInfo.userId = res1.results.id;
							this_.PersonalInfo = res1.results
							if (res1.results.usertype == 1) {
								$('.StudentCls').css('display', 'block');
								$('.TeacherCls').css('display', 'none');
							} else {
								$('.StudentCls').css('display', 'none');
								$('.TeacherCls').css('display', 'block');
							}
							this_.queryUserBankInfo();
						} else {
							weui.topTips(res1.message);
							setTimeout(() => {
								this_.BackToMini('UserMain');
							}, 1500)
						}
					},
					error: function(err) {
						loading.hide();
					},
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	html,
	body,
	page {
		width: 750rpx;
		background: linear-gradient(180deg, #FFFFFF 0%, #FAFAFA 100%);
	}

	.OtherMoney {
		display: flex;
		justify-content: flex-start;
		// color: #999999;
		// font-size: 28rpx;
		// font-family: PingFangSC-Regular, PingFang SC;
		// font-weight: 400;
		// color: #999999;
		// line-height: 40rpx;
		width: 686rpx;
		height: 100rpx;
		background: #F7F6FB;
		border-radius: 10rpx;
		margin: 0 auto;
		line-height: 100rpx;
		margin-top: 32rpx;
		padding-left: 26rpx;

		span {
			display: inline-block;
			font-size: 40rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #000000;
		}

		input {
			height: 100%;
		}


		.ALL {
			position: absolute !important;
			font-size: 26rpx !important;
			font-family: PingFang SC;
			font-weight: bold !important;
			color: #129A5E !important;
			right: 31rpx !important;
		}
	}

	.ReflectBox {
		width: 680rpx;
		margin: 0 auto;
		display: flex;
		justify-content: space-between;
		height: 100rpx;
		line-height: 100rpx;

		.left_ {
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #000000;
		}

		.right_ {
			font-size: 26rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #888888;
		}

	}

	.bottomdiv {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 80rpx;
	}

	.DepositID {
		width: 654rpx;
		height: 92rpx;
		background: #04BA73;
		border-radius: 8rpx;
		margin: 0 auto;
		text-align: center;
		bottom: 30rpx;

	}

	.DepositID span {
		height: 50rpx;
		font-size: 36rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 92rpx;
	}

	.switchbankCard {
		text-align: center;
		/* width: 205px; */
		// height: 28rpx;
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		text-decoration: underline;
		color: #268EF8;
		margin-top: 23rpx;
	}

	.payType {
		width: 654rpx;
		margin: 0 auto;
		height: 98rpx;
		line-height: 98rpx;
		display: flex;
		justify-content: space-between;
	}

	.payType view:nth-child(1) {
		// width: 120rpx;
		height: 42rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #888888;
		line-height: 98rpx;
	}

	.payType view::nth-child(2) {
		width: 225rpx;
		height: 45rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		line-height: 45rpx;
	}

	.arrow {
		transform: rotate(-45deg);
		top: -3rpx;
		border: solid #999999;
		border-width: 0 1rpx 1rpx 0;
		display: inline-block;
		padding: 8rpx;
		position: relative;
	}

	.RechargeConfirmationBox {
		position: fixed;
		left: 0;
		right: 0;
		top: 400rpx;
		width: 618rpx;
		height: 520rpx;
		background: #FFFFFF;
		border-radius: 8rpx;
		margin: 0 auto;



		.RechargeInfo {
			width: 560rpx;
			height: 100%;
			margin: 0 auto;


			/deep/ .openCardPhonTxt {
				font-weight: bold;
				color: #000000;
				width: auto;
			}

			/deep/ .openCardPhonSmsTxt {
				font-weight: bold;
				color: #000000;
				width: auto;
			}

			/deep/ .van-cell {
				padding-left: 0;
				padding-right: 0;
				height: 100rpx;
			}

			/deep/ .van-button {
				background: #1CC48B;
				border-radius: 8rpx;
				font-size: 10rpx;
			}

			.RechargeInfo1 {
				height: 120rpx;
				line-height: 88rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #000000;
				text-align: center;
			}

			.ContenInfo {
				text-align: center;
				// width: 383rpx;
				height: 159rpx;
				margin: 0 auto;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #010101;

				view {
					line-height: 75rpx;
				}
			}

			.BtngGoup {
				justify-content: space-between;
				display: flex;
				margin-top: 40rpx;

				view {
					width: 100%;
					height: 68rpx;
					border-radius: 8rpx;
					text-align: center;
					line-height: 68rpx;
				}

				view:nth-child(1) {
					background: #1CC48B;
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #EBEBEB;
				}

				view:nth-child(2) {
					background: #1CC48B;
					font-size: 36rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}

			}

		}
	}

	.closePhoneBox {
		// width: 35rpx;
		height: 35rpx;
		position: relative;
		right: -20rpx;
		top: 20rpx;
		text-align: right;

		image {
			width: 35rpx;
			height: 35rpx;
			position: relative;
			right: 10rpx;
		}

	}
</style>
