<template>
	<view class="pay">
		<view class="pays">
			<view class="mm">
				<!-- <input type="text" v-model="form.openingBank" placeholder="请输入开户行" @focus="getLast"/>
				<input type="text" v-model="form.bankAccount" placeholder="请输入您要提现的银行卡号" /> -->
				<view class="title">
					到账银行卡
				</view>
				<view class="select_bank" @click="isSelect = true">
					<view class="blank_logo">
						<image src="../../static/identify/rest.png" alt="" />
					</view>
					<view class="blank_num">
						{{blank}}<br />
						<text>注：仅支持整数，72小时内到账</text>
					</view>
					<view class="toggle">
						<image src="../../static/identify/more.png" alt="" />
					</view>
				</view>
			</view>
			<view class="money">
				<view class="withdraw">
					提现金额
				</view>
				<view class="inp">
					<text>￥</text>
					<input ref="goldInput" type="number" v-model="form.dPrice" :maxlength="String(getAmount).length" @input="inputGold"/>
				</view>
				<view class="balance" v-if="getAmount>0">可提现金额为 {{getAmount}} 元 ,<text @click="allBalance">全部提现</text></view>
				<view class="balance" v-else>暂无可提现余额</view>
				<br />
				<!-- <view class="balance"> 订单收益七天后可提现</view> -->
				<!-- <view class="balance" >不可提现：{{dBalance - getAmount}} 元</text></view> -->
			</view>
			<view class="btn">
				<view class="paybtn" @click="submit">申请提现</view>
			</view>
			<view class="log" @click="history">
				提现记录
			</view>
		</view>
		<view class="popup" @click="isSelect = false" v-if="isSelect">
			<view class="select_blank" @click.stop>
				<view class="popup_title">
					选择提现银行卡<br />
					<text>请留意各银行到账时间</text>
				</view>
				<view class="popup_content">
					<view class="blannk_card">
						<view class="card_list">
							<view class="card" v-for="(item,index) in blankList" :key="index" @click="selectBlank(item)">
								<view class="card_left">
									<image :src="item.img" mode=""></image>
								</view>
								<view class="card_name">
									{{item.bankName}}({{item.bankCardNum.slice(-4)}})
									<text>72小时到账</text>
								</view>
								<view class="card_select">
									<image :src="item.bankLogo" mode="" v-if="item.check"></image>
								</view>
							</view>
						</view>
						<view class="add_btn" @click="addBlank">
							<view class="add_left">
								<image src="../../static/other/add_blank.png" mode=""></image>
								添加银行卡提现
							</view>
							<view class="add_right">
								<image src="../../static/identify/more.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
			</view> 
		</view>
		<!-- 		<view class="popup" v-if="isLast">
			<view class="histroy">
				<view class="title">
					已有提交信息，是否继续使用
				</view>
				<view class="content">
					开户行:{{openingBank}}
				</view>
				<view class="content">
					银行卡号：{{bankAccount}}
				</view>
				<view class="btn_box">
					<view class="btn" @click="cancel">
						取消
					</view>
					<view class="btn success" @click="use">
						使用
					</view>
				</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	import {
		pay,
		getInfo,
		withdraw,
		last,
		myBank
	} from '../../api/index.js'
	export default {
		data() {
			return {
				isSelect: false,
				placeholder: '转出金额',
				money: '',
				getAmount: '',
				dBalance: '',
				form: {
					cOpenId: uni.getStorageSync('wx_copenid'),
					dPrice: null,
				},
				isLast: false,
				bankAccount: '',
				openingBank: '',
				blank:'选择银行卡',
				blankList: []
			}
		},
		methods: {
			addBlank() {
				uni.navigateTo({
					url: './add_blank',
				})
				this.isSelect = false
			},
			history() {
				uni.navigateTo({
					url: '/pages/my/history'
				})
			},
			getLast() {
				let data = {}
				data.cOpenId = uni.getStorageSync('wx_copenid')
				last(data).then(res => {
					if (res.data.code == 200) {
						this.isLast = true
						this.openingBank = res.data.data.openingBank
						this.bankAccount = res.data.data.bankAccount
					} else {
						return
					}
				})
			},
			selectBlank(item) {
				this.blankList.forEach(i => {
					i.check = false;
				});
				item.check = true
				this.isSelect = false
				console.log(item)
				this.blank = item.bankName +'('+  '****' + '   '+item.bankCardNum.slice(-4)+')'
				this.form.bankId = item.bankId
				this.form.bankAccount = item.bankCardNum
				this.form.openingBank = item.openingBank
			},
			//实时更新剩余金豆
			inputGold(event) {
				if (this.form.dPrice > this.getAmount) {
					this.form.dPrice = this.getAmount
					this.$nextTick(() => {
						this.$refs.goldInput.value = this.getAmount;
					});
				}
			},
			cancel() {
				this.isLast = false
			},
			use() {
				this.isLast = false
				this.form.openingBank = this.openingBank
				this.form.bankAccount = this.bankAccount
			},
			allBalance() {
				this.form.dPrice = this.getAmount
			},
			getbank(){
				myBank().then(res => {
					console.log(res)
					this.blankList = res.data.data
				})
			},
			info() {
				getInfo().then(res => {
					this.getAmount = res.data.data.getAmount.toFixed(2)
					this.dBalance = res.data.data.dBalance.toFixed(2)
				})
			},
			submit() {
				this.form.cOpenId = uni.getStorageSync('wx_copenid')

				// if (this.form.openingBank == '') {
				// 	uni.showToast({
				// 		title: '请输入开户行',
				// 		icon: 'none'
				// 	})
				// 	return; // 格式不符合要求
				// }
				// var regex = /^[0-9]{13,19}$/;
				// if (!regex.test(this.form.bankCardNum)) {
				// 	uni.showToast({
				// 		title: '请输入正确的银行卡号',
				// 		icon: 'none'
				// 	})
				// 	return; // 格式不符合要求
				// }
				// if (this.form.dMoney <= 0) {
				// 	uni.showToast({
				// 		title: '请输入金额',
				// 		icon: 'none'
				// 	})
				// 	return
				// } else if (this.form.dMoney > this.dBalance) {
				// 	uni.showToast({
				// 		title: '输入金额超出账户余额',
				// 		icon: 'none'
				// 	})
				// 	return
				// } else {
					withdraw(this.form).then(res => {
						// console.log(res,"提现")
						if (res.data.code == 200) {
							uni.showToast({
								title: '已提交审核！',
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					})
				// }
			},
		},
		onLoad() {
			// this.getLast()
		},
		onShow() {
			this.info()
			this.getbank()
		}
	}
</script>

<style lang="scss" scoped>
	.pay {
		width: 100vw;
		height: 100vh;

		.pays {

			// padding: 80rpx 30rpx;
			.mm {
				width: 100%;
				height: 242rpx;
				background-color: #F8FAFE;
				padding: 40rpx;
				box-sizing: border-box;

				.title {
					font-weight: 700;
					font-size: 32rpx;
					color: #000000;
					line-height: 36rpx;
					margin-bottom: 34rpx;
				}

				.select_bank {
					width: 100%;
					display: flex;
					justify-content: space-between;

					.blank_logo {
						margin-right: 16rpx;

						image {
							width: 40rpx;
							height: 40rpx;
						}
					}

					.blank_num {
						flex: 1;
						font-size: 32rpx;
						color: #000000;
						line-height: 36rpx;

						text {
							font-size: 28rpx;
							color: #666666;
							line-height: 36rpx;
							display: inline-block;
							margin-top: 22rpx;
						}
					}

					.toggle {
						image {
							width: 32rpx;
							height: 32rpx;
						}
					}
				}
			}

			.log {
				width: 100%;
				text-align: center;
				margin-top: 30rpx;
				font-size: 24rpx;
				color: #21899e;
			}

			// input {
			// 	background-color: #f8f8f8;
			// 	padding: 20rpx;
			// 	text-align: center;
			// 	margin-bottom: 30rpx;
			// }

			.money {
				margin-top: 32rpx;
				padding: 0px 40rpx;

				.withdraw {
					font-size: 30rpx;
					color: #000000;
					line-height: 36rpx;
				}

				.inp {
					height: 90rpx;
					font-weight: 600;
					font-size: 52rpx;
					color: #333333;
					line-height: 61rpx;
					display: flex;
					align-items: center;
					border-bottom: 1px solid #E1E1E1;

					input {
						flex: 1;
						height: 100%;
						background-color: #fff;
						text-align: left;
						margin-left: 12rpx;
						font-size: 52rpx;
					}
				}



				.my {
					font-size: 24rpx;
					margin-bottom: 10rpx;
				}

				.balance {
					margin-top: 16rpx;
					font-size: 28rpx;
					color: #999999;
					line-height: 36rpx;

					text {
						color: #00B8B0;
						margin-left: 10rpx;
					}
				}
			}

			.btn {
				margin: 0 auto;
				width: 100%;
				margin-top: 632rpx;
				padding: 0px 40rpx;
				box-sizing: border-box;

				.paybtn {
					height: 76rpx;
					background: linear-gradient(90deg, #1AD7CE 0%, #21C9C1 100%);
					border-radius: 106rpx;
					text-align: center;
					color: #fff;
					line-height: 76rpx;
				}

				.log {
					font-size: 28rpx;
					color: #00B8B0;

				}
			}
		}

		.popup {
			width: 100%;
			height: 100%;
			position: fixed;
			top: 0;
			left: 0;
			background-color: rgba(0, 0, 0, 0.5);
			display: flex;
			align-items: end;

			.select_blank {
				width: 100%;
				height: 774rpx;
				background: #F3FFFE;
				border-radius: 32px 32px 0px 0px;
				display: flex;
				flex-direction: column;

				.popup_title {
					width: 100%;
					height: 148rpx;
					font-size: 34rpx;
					color: #000000;
					line-height: 36rpx;
					font-weight: 700;
					background-color: #fff;
					border-radius: 32px 32px 0px 0px;
					text-align: center;
					padding: 32rpx;
					box-sizing: border-box;

					text {
						display: inline-block;
						font-size: 28rpx;
						color: #666666;
						line-height: 36rpx;
						font-weight: 400;
						margin-top: 12rpx;
					}
				}

				.popup_content {
					flex: 1;

					.blannk_card {
						width: 670rpx;
						height: 450rpx;
						background: linear-gradient(35deg, #FFFFFF 0%, #E4FFFD 100%);
						box-shadow: 0px 4rpx 12rpx 0px rgba(88, 209, 187, 0.1);
						border-radius: 16rpx;
						border: 1px solid #D5F5F4;
						margin: 40rpx auto 0px;
						padding: 24rpx;
						display: flex;
						flex-direction: column;

						.card_list {
							flex: 1;
							overflow-y: auto;
							margin-bottom: 20rpx;

							.card {
								width: 100%;
								height: 100rpx;
								display: flex;
								align-items: center;
								justify-content: space-between;
								border-bottom: 1px solid #EEEEEE;
								margin-bottom: 20rpx;
									padding-bottom: 20rpx;
									box-sizing: border-box;
								.card_left {
									width: 40rpx;
									height: 100%;
									margin-right: -150rpx;

									image {
										width: 40rpx;
										height: 40rpx;
									}
								}

								.card_name {
									width: 402rpx;
									height: 100%;
									font-size: 30rpx;
									color: #333333;
									line-height: 35rpx;
									display: flex;
									flex-direction: column;
									justify-content: space-between;
								

									text {
										display: inline-block;
										margin-top: 4rpx;
										font-size: 24rpx;
										color: rgba(51, 51, 51, 0.6);
										line-height: 28rpx;
									}
								}

								.card_select {
									width: 48rpx;
									height: 48rpx;

									image {
										width: 48rpx;
										height: 48rpx;
									}
								}
							}
						}

						.add_btn {
							width: 100%;
							height: 42rpx;
							display: flex;
							align-items: center;
							justify-content: space-between;

							.add_left {
								display: flex;
								align-items: center;
								font-size: 30rpx;
								color: #333333;
								line-height: 35rpx;

								image {
									width: 40rpx;
									height: 40rpx;
									margin-right: 20rpx;
								}
							}

							.add_right {
								image {
									width: 32rpx;
									height: 32rpx;
								}
							}
						}
					}
				}
			}
		}

	}
</style>