<template>
	<view>
		<public-module></public-module>
		<view class="binding dis f-c a-c ">
			<view class="title dis f-c a-c ">
				<text>添加银行卡</text>
				<text>请填写本人的银行卡信息</text>
			</view>
			<view class="formInfo">
				<u-field v-model="accountno" @blur="validateBankCard" :field-style="{textAlign:'right'}" label="银行卡"
					placeholder="请输入银行卡卡号">
				</u-field>
				<u-field v-model="bankname" :field-style="{textAlign:'right'}" label="选择银行" placeholder="系统自动识别">
				</u-field>
			</view>
			<u-button class="mt-5" type="primary" style="background-color:#0052FF ;font-weight: bold;width: 100%;"
				@tap="submit">
				绑定银行卡</u-button>
		</view>
		<u-popup mode="bottom" v-model="promptShow" width="100%" height="100%" border-radius="14">
			<view style="padding: 16px;">
				<view class="prompt dis a-c j-c f-c">
					<image src="/static/image/bindBank/success.png" mode="" style="width: 50px;height:50px;"></image>
					<text>绑定成功</text>
				</view>
				<view class="dis j-end a-c" style="width: 100%;">
					<text class="back" @click="GoBack">完成</text>
				</view>
			</view>

		</u-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex"
	import bankBin from "@/common/bankcardinfo.js"
	export default {
		data() {
			return {
				promptShow: false,
				bankname: "", //银行名称
				accountno: "", //银行卡账号
				disabled: true,
				validateBankCardStatus: false, //银行卡验证状态
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		watch: {
			bankname(val) {
				this.OnBtnChange();
			},
			accountno(val) {
				this.OnBtnChange();
			},
		},
		methods: {
			...mapMutations(['setUserModules']),
			// 改变按钮状态
			OnBtnChange() {
				if (this.accountno && this.bankname) {
					this.disabled = false;
					return;
				}
				this.disabled = true;
			},
			// 验证银行卡
			async validateBankCard() {
				var that = this;
				that.bankname = "";
				await bankBin.getBankBin(this.accountno)
					.then((data) => {
						console.log(data);
						that.bankname = data.bankName;
						that.validateBankCardStatus = true;
						return true;
					})
					.catch((err) => {
						that.validateBankCardStatus = false;
						return uni.showToast({
							title: err.split(":")[1],
							icon: "none"
						});
					})
			},
			async submit() {
				if (!this.bankname || !this.accountno) {
					return uni.showToast({
						title: '信息不完整',
						duration: 2000,
						icon: "none"
					});
				}
				var params = {
					"bankNumber": this.accountno,
					"bankAccount": this.bankname,
					"isDefault": 0,
				}
				let res = await this.$http.post('/userBank/insertByBankNumber', params);
				if (res.code == '200') {
					this.promptShow = true;
				} else {
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: "none"
					});
				}

			},
			GoBack() {
				return uni.navigateBack();
			}


		}
	}
</script>


<style lang="scss" scoped>
	@import '@/style/mixin.scss';

	page {
		background-color: #F8FAFE;
	}

	.prompt {
		padding: 16px;
		position: absolute;
		top: 30%;
		left: 50%;
		transform: translate(-50%, -50%);

		text {
			font-size: 22px;
			color: #333333;
			font-weight: bold;
			margin-top: 15px;
		}

		.back {
			font-size: 14px;
			color: #333333;
		}
	}

	.binding {
		padding: 16px;

		.title {
			font-size: 14px;
			color: #333333;

			text:first-child {
				font-size: 20px;
				font-weight: bold;
			}
		}

		.formInfo {
			margin-top: 18px;
			width: 100%;
			height: 88px;
			background: #FFFFFF;
			box-shadow: 0px 4px 10px 0px #DAE3F4;
			border-radius: 6px;


		}


	}
</style>