<template>
	<view class="body">
		<view class="list">
			<view class="listName">
				开户银行
			</view>
			<view class="listContent" @click="goBankList">
				{{bankName}}
				<image src="@/static/img/right.svg" mode=""></image>
			</view>
		</view>
		<view class="list">
			<view class="listName">
				银行卡号
			</view>
			<text v-if="edit">{{bankCardFilter(cardNumber)}}</text>
			<input v-else type="number" v-model="cardNumber" placeholder="请输入银行卡号" placeholder-style="color:#cccccc;" />
		</view>
		<view class="list">
			<view class="listName">
				默认银行卡
			</view>
			<switch :disabled="!canEditDef" :checked="def" @change="switchChange" />
		</view>
		<view v-if="loading" class="btn">
			<view class="bottombtn nocheck">{{btnText}}中</view>
		</view>
		<view v-else-if="validate" class="btn">
			<view class="bottombtn nocheck">{{btnText}}</view>
		</view>
		<view v-else class="btn" @click="goSet">
			<view class="bottombtn">{{btnText}}</view>
		</view>
	</view>
</template>

<script>
	import {
		bank
	} from '@/api/auth.js'
	import {
		validatenull
	} from '@/untils/validate.js'
	import {
		mapGetters
	} from 'vuex'
	import {
		bankCardFilter
	} from '@/untils/format.js'

	export default {
		data() {
			return {
				edit: false,
				canEditDef: false,
				loading: false,
				id: null,
				bankId: '',
				bankName: '选择银行',
				cardNumber: '',
				def: true,
				btnText: "认证"
			};
		},
		onLoad({
			id,
			name,
			card,
			def
		}) {
			if (id) {
				this.edit = true
				this.id = id
				this.bankId = this.bankName = name
				this.cardNumber = card
				this.def = def === '1'
				this.canEditDef = !this.def && this.auth.bankCardState
				this.btnText = '修改'
			} else {
				this.canEditDef = this.auth.bankCardState
				this.btnText = '认证'
			}
		},
		methods: {
			goBankList: function() {
				uni.$once('icCardSetBank', (data) => {
					this.bankId = data.id;
					this.bankName = data.name;
				})
				uni.navigateTo({
					url: '/pages/bankList/bankList'
				})
			},
			goSet() {
				this.loading = true
				bank(this.bankName, this.auth.authInfo.realName, this.cardNumber, this.def ? 1 : 0, this.id).then(resp => {
					this.loading = false
					this.$store.commit('SET_AUTH_BANKCARD', true)
					uni.navigateBack()
				}).catch(() => {
					this.loading = false
				})
			},
			switchChange(e) {
				if (this.auth.bankCardState && this.canEditDef) {
					this.def = !this.def
				}
			},
			bankCardFilter
		},
		computed: {
			...mapGetters(['auth']),
			validate() {
				return validatenull(this.bankId) || validatenull(this.cardNumber)
			}
		}
	};
</script>

<style>
	.list switch {
		margin-left: auto;
	}

	.nocheck {
		opacity: 0.3;
	}

	.listName {
		font-family: PingFangSC-Regular;
		font-size: 32rpx;
		color: #333333;
		width: 200rpx;
	}

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

	.listContent {
		display: flex;
		align-items: center;
		justify-content: center;
		font-family: PingFangSC-Regular;
		font-size: 28rpx;
		color: #999999;
		margin-left: auto;
	}

	.list input,
	.list text {
		margin-left: auto;
		text-align: right;
	}
.list input{
	flex:1;
}
	.list {
		display: flex;
		align-items: center;
		justify-content: flex-start;
		padding: 30rpx;
		padding-left: 0;
		border-bottom: 1rpx solid #f2f2f2;
	}

	.body {
		background-color: #fefefe;
		margin-top: 30rpx;
		padding-left: 30rpx;
	}

	page {
		background-color: #f6f6f6;
	}

	.bottombtn {
		background-color: #1b66ff;
		color: #fff;
		text-align: center;
		border-radius: 10rpx;
		font-family: PingFangSC-Medium;
		font-size: 32rpx;
		height: 90rpx;
		line-height: 90rpx;
	}

	.btn {
		background-color: #fefefe;
		width: 690rpx;
		padding: 30rpx;
		padding-bottom: 80rpx;
		position: fixed;
		bottom: 0;
		left: 0;
	}
</style>
