<template>
	<view class="content">
			<!-- <view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}</view>
		</view> -->
		<view class="row b-b">
			<text class="tit">姓名</text>
			<input class="input" v-model="name" type="text" placeholder="请输入姓名"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b" >
			<text class="tit">{{type==3?'银行卡号':'账号'}}</text>
			<input class="input" v-model="payment" type="text" :placeholder="'请输入'+ (type==3?'银行卡号':'账号')"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b" v-if="type==3">
			<text class="tit">手机号码</text>
			<input class="input" v-model="phone" type="text" placeholder="请输入手机号码"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b" v-if="type==3">
			<text class="tit">所属银行</text>
			<input class="input" v-model="bank" type="text" placeholder="请输入所属银行"
				placeholder-class="placeholder" />
		</view>
		<!-- <view class="row b-b" v-if="type==3">
			<text class="tit">所属支行</text>
			<input class="input" v-model="bank_name" type="text" placeholder="请输入所属支行"
				placeholder-class="placeholder" />
		</view> -->

		<button class="add-btn up" @click="confirm">提 交 保 存</button>
	</view>
</template>

<script>
	import {
		aliInfo,
		bankInfo,
		setAliInfo,
		setBankInfo,
		setBank,
		getBank
	} from '@/api/wallet.js';
	export default {
		data() {
			return {
				type: 0,
				payment: '',
				name:'',
				bank: '',
				phone:'',
				bank_name: '',
				loading: false
			};
		},

		onLoad(options) {
			// this.tabCurrentIndex = +options.state || 0;
			// this.loadAli();
			// this.loadBank();
			this.type = options.type
			this.getBank()
		},

		methods: {
			//加载数据
			loadAli(source) {
				aliInfo({}).then(({
					data
				}) => {
					let order = this.navList[0].orderList;
					this.$set(order, 'name', data.fullname)
					this.$set(order, 'code', data.alino)
					this.$set(order, 'id', data.id)
				});
			},
			loadBank() {
				bankInfo({}).then(({
					data
				}) => {
					let order = this.navList[1].orderList;
					this.$set(order, 'name', data.fullname);
					this.$set(order, 'code', data.bankno);
					this.$set(order, 'bankName', data.bank);
					this.$set(order, 'id', data.id)
				});
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			// 提交保存
			confirm() {
				let obj = this;
				if(obj.loading) {
					return
				}
				if(obj.name == '') {
					return obj.$msg('请输入姓名')
				}
				if(obj.payment == '') {
					if(obj.type == 3) {
						return obj.$msg('请输入银行卡号')
					}
					if(obj.type == 2) {
						return obj.$msg('请输入账号')
					}
				}
				if(obj.type == 3) {
					if(obj.phone == '') {
						return obj.$msg('请输入手机号')
					}
					if(obj.bank == '') {
						return obj.$msg('请输入所属银行')
					}
					// if(obj.bank_name == '') {
					// 	return obj.$msg('请输入所属支行')
					// }
				}
				
				obj.loading = true
				setBank({
					type: obj.type,
					name: obj.name,
					phone: obj.phone,
					payment: obj.payment,
					bank: obj.bank,
					// bank_name: obj.bank_name
				}).then(res => {
					obj.loading = false
					uni.showToast({
						title: '修改成功',
						duration: 2000,
						position: 'top'
					});
					console.log('修改成功')
					obj.$prePage().dataUp()
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				}).catch(err => {
					obj.loading = false
				})
			},
			getBank() {
				let obj = this
				getBank().then(res => {
					let bank;
					console.log(obj.type)
					if(obj.type == 2) {
						
						bank =  res.data.zfb
					}
					if(obj.type == 3) {
						console.log( res.data.bank)
						bank =  res.data.bank
						console.log(bank)
					}
					console.log(bank)
					if(bank.name) {
						obj.name = bank.name
						obj.phone= bank.phone
						obj.payment= bank.payment
						obj.bank= bank.bank
						obj.bank_name= bank.bank_name
					}
					
					
				})
			},
			/* 保存银行卡详细 */
			setBankInfo(obj) {
				setBankInfo(obj)
					.then(e => {
						uni.showToast({
							title: '修改成功',
							duration: 2000,
							position: 'top'
						});
						this.$prePage().dataUp()
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					})
					.catch(e => {
						console.log(e);
					});
			},
			// 修改支付宝信息
			setAliInfo(obj) {
				setAliInfo(obj)
					.then(e => {
						uni.showToast({
							title: '修改成功',
							duration: 2000,
							position: 'top'
						});
						this.$prePage().dataUp()
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					})
					.catch(e => {
						console.log(e);
					});
			},
		}
	};
</script>

<style lang="scss">
	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30rpx;
		height: 110rpx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 200rpx;
			font-size: 30rpx;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 30rpx;
			color: $font-color-dark;
		}

		.iconlocation {
			font-size: 36rpx;
			color: $font-color-light;
		}
	}

	page,
	.content {
		background: $page-color-base;
		height: 100%;
	}

	.swiper-box {
		height: 750rpx;
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;

			&.current {
				color: $base-color;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}

	.add-btn {
		&.up {
			background-color:#ff4072;
			color: #fff;
		}

		display: flex;
		align-items: center;
		justify-content: center;
		width: 690rpx;
		height: 80rpx;
		margin: 0 auto;
		margin-top: 30rpx;
		font-size: $font-lg;
		border-radius: 10rpx;
		// box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
