<template>
	<view class="WithdrawingCoin">
		<view class="withdrawing-content">
			<view class="item-name">{{bank_account_id===''?'添加':'编辑'}}提现银行</view>
			<view class="item">
				<view class="title">收款人姓名</view>
				<input class="input_num" v-model="name" maxlength="13" placeholder="请输入收款人姓名"
					placeholder-style="color:#999;font-weight:normal; font-size:30rpx;" />
			</view>
			<view class="item">
				<view class="title">收款人手机号</view>
				<input class="input_num" v-model="mobile" maxlength="11" placeholder="请输入手机号"
					placeholder-style="color:#999;font-weight:normal; font-size:30rpx;" />
			</view>
			<view class="item">
				<view class="title">开户行</view>
				<u-select v-model="bankShow" value-name='id' :default-value='[0]' label-name='name'
					@confirm='selectBank' :list="banks" />
				<view class="input_num" @click="bankShow = true">
					{{selectBankObj.label?selectBankObj.label:'请选择开户行'}}
				</view>
			</view>
			<view class="item">
				<view class="title">收款人账号</view>
				<input class="input_num" v-model="no" maxlength="20" type="number" placeholder="请输入收款人账号"
					placeholder-style="color:#999;font-weight:normal; font-size:30rpx;" />
			</view>
			<view class="item">
				<view class="title">支行名称</view>
				<input class="input_num" v-model="branch_name" maxlength="20" placeholder="省-市-区-支行名称"
					placeholder-style="color:#999;font-weight:normal; font-size:30rpx;" />
			</view>
		</view>
		<view class="btn" @click="sureAdd">确认{{bank_account_id===''?'添加':'编辑'}}银行</view>
		<view class="history" @click="$Router.push({name:'withdrawList'})">提现记录
			<u-icon name="question-circle-fill" />
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		accountsCreate,
		withdrawsAccounts,
		accountsCreateEdit,
		withdrawsAccountsEdits
	} from '@/apis/interfaces/withdraws'
	export default {
		data() {
			return {
				no: '', // 银行卡号
				name: '', // 用户姓名
				branch_name: '', // 支行名称
				mobile: '', // 手机号
				bank_id: '', // 银行卡id
				banks: [], // 开户行列表
				bankShow: false, // 显示开户行列表
				selectBankObj: {}, // 选择银行
				bank_account_id:''
			};
		},
		onLoad(e) {
			// 有id 是编辑
			if (e.id) {
				this.bank_account_id = e.id
				this.getAccountsCreateEdit()
			} else {
				this.getAccountsCreate()
			}
		},
		methods: {
			// 获取添加银行前置条件
			getAccountsCreate() {
				accountsCreate().then(res => {
					console.log(res)
					this.banks = res.banks
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.message,
						type: 'primary',
						duration: 3000
					})
				})
			},
			// 编辑前置
			getAccountsCreateEdit() {
				accountsCreateEdit(this.bank_account_id).then(res => {
					console.log(res)
					this.banks = res.banks
					this.no = res.info.no
					this.name = res.info.name
					this.mobile = res.info.mobile
					this.branch_name = res.info.branch_name
					this.selectBankObj.label = res.info.bank.name
					this.bank_id = res.info.bank.id
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.message,
						type: 'primary',
						duration: 3000
					})
				})
			},
			// 选择银行
			selectBank(e) {
				console.log(e[0])
				this.selectBankObj = e[0]
				this.bank_id = e[0].value
			},
			sureAdd() {
				let data = {
					no: this.no, // 银行卡号
					name: this.name, // 用户姓名
					branch_name: this.branch_name, // 支行名称
					mobile: this.mobile, // 手机号
					bank_id: this.bank_id, // 银行卡id
				}
				if (data.name === '') {
					this.$refs.uToast.show({
						title: '请核对收款人姓名',
						type: 'primary',
						duration: 3000
					})
					return;
				}
				if (data.mobile.length !== 11) {
					this.$refs.uToast.show({
						title: '请核对收款人手机号',
						type: 'primary',
						duration: 3000
					})
					return;
				}
				if (data.bank_id === '') {
					this.$refs.uToast.show({
						title: '请勾选开户行',
						type: 'primary',
						duration: 3000
					})
					return;
				}
				if (data.no === '') {
					this.$refs.uToast.show({
						title: '请核对收款账号',
						type: 'primary',
						duration: 3000
					})
					return;
				}
				if (data.branch_name === '') {
					this.$refs.uToast.show({
						title: '请核对省-市-区-支行名称',
						type: 'primary',
						duration: 3000
					})
					return;
				}
				if (this.bank_account_id) {
					data.bank_account_id = this.bank_account_id
					console.log(data)
					this.withdrawsAccountsEdits(data)
				} else {
					this.withdrawsAccounts(data)
				}
			},
			// 添加银行
			withdrawsAccounts(data) {
				withdrawsAccounts(data).then(res => {
					this.$refs.uToast.show({
						title: res,
						type: 'primary',
						duration: 3000
					})
					setTimeout(res => {
						uni.setStorageSync('refresh',true)
						uni.navigateBack({})
					}, 3000)
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.message,
						type: 'primary',
						duration: 3000
					})
				})
			},
			// 编辑银行卡银行
			withdrawsAccountsEdits(data) {
				withdrawsAccountsEdits(data).then(res => {
					this.$refs.uToast.show({
						title: res,
						type: 'primary',
						duration: 3000
					})
					uni.setStorageSync('refresh',true)
					setTimeout(res => {
						uni.navigateBack({})
					}, 3000)
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.message,
						type: 'primary',
						duration: 3000
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		min-height: 100vh;
		background-color: #fff;
	}

	.history {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
		color: #cacaca;

		.u-icon {
			margin-left: 10rpx;
		}
	}

	.withdrawing-content {
		min-height: 300rpx;
		background-color: #fff;
		box-shadow: 0 0 30rpx 1rpx rgba($color: #000000, $alpha: 0.2);
		margin: $margin;
		border-radius: 20rpx;
		padding: $padding;
		font-size: $title-size-m;

		.item-name {
			text-align: center;
			padding: $padding * .6;
			color: #303030;
			font-weight: bold;
			margin-bottom: $margin;
			font-size: $title-size *1.2;
			position: relative;

			&::after {
				content: '';
				position: absolute;
				background-color: rgba($color: $text-price, $alpha: .4);
				width: 200rpx;
				height: 10rpx;
				left: 50%;
				margin-left: -100rpx;
				bottom: 16rpx;
				z-index: 1;
				box-shadow: 0 10rpx 20rpx 0rpx rgba($color: #976dff, $alpha: 0.6);
			}
		}

		.item {
			border-bottom: solid 1rpx #f8f8f8;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: flex-start;
			box-sizing: border-box;
			padding: $padding 0;

			span:nth-child(1) {
				color: #666;
				margin-right: 20rpx;
			}

			.title {
				width: 200rpx;
				font-size: 28rpx;

				&::before {
					content: '*';
					color: red;
					padding-right: 4rpx;
				}
			}

			.input_num {
				font-size: 30rpx;
				color: #3a3a3a;
				// font-weight: bolder;
				flex: 1;
				margin-left: 20rpx;
			}
		}

		.item-total {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			padding: $padding*1 0 0 0;
			color: #3a3a3a;

			.total {
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				justify-content: center;
				box-sizing: border-box;

				.money {
					padding-top: $padding *0.5;
				}
			}

			.lists {
				color: $text-price;
			}
		}

	}

	.btn {
		background-image: linear-gradient(to right, #7c52fc, #976dff);
		color: #fff;
		border-radius: 10rpx;
		text-align: center;
		padding: $padding * .9;
		margin: $margin*3 $margin * 2 $margin $margin*2;
		font-size: $title-size;
		font-weight: bold;
		letter-spacing: 2rpx;
	}
</style>
