<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>
	</view>
</template>

<script>
	import {
		Dialog
	} 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'
				},
			}
		},
		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: {
			//全部提现
			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
				}
				await this.ExchangeWithdraw()
			},
			// 提现
			async ExchangeWithdraw() {
				let this_ = this;
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/bank-wechat/icbc/account/exchange/withdraw',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						schoolId: bankInfo.schoolId,
						userId: userID,
						sessionid: sessionID,
						wechattype: 1,
						amount: this_.inputOtherM * 1,
						bindMedium: this_.bankInfo_.bindCardNo,
						summary: "提现",
					},
					headers: {
						'sessionid': sessionID,
					},
					success: function(res5) {
						loading.hide();
						if (res5.code == "10000") {
							// this_.ExchangeWithdrawQuery(res5.results.flowId, res5.results.corpSerno);
							wx.showModal({
								title:'提现申请已提交',
								content:'银行处理需要1-5分钟时间，充值结果以余额查询为准。',
								showCancel:false
							})
						} else {
							weui.topTips(res5.message)
						}
					},
					error: function(err) {
						loading.hide();
					},
				});
			},
			// 充值结果查询
			async ExchangeWithdrawQuery(flowId, oriCorpSerno) {
				let this_ = this;
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/bank-wechat/icbc/account/exchange/withdrawQuery',
					type: "POST",
					contentType: "application/x-www-form-urlencoded",
					data: {
						schoolId: bankInfo.schoolId,
						userId: userID,
						sessionid: sessionID,
						wechattype: 1,
						flowId: flowId,
						oriCorpSerno: oriCorpSerno,
					},
					headers: {
						'sessionid': sessionID,
					},
					success: function(res5) {
						loading.hide();
						if (res5.code == "10000") {
							let appStatus = res5.results.appStatus;
							if (appStatus == '4') {
								uni.showModal({
									title: "系统提示",
									content: "提现成功!"
								})
								uni.navigateBack({})
							} else {
								uni.showModal({
									title: "系统提示",
									content: appStatus == '1' ? '申请已受理' : res5.message
								})
							}

						} 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/icbc/account/balance/balanceQuery',
					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
							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;
	}
</style>
