<template>
	<view class="addWithdrawalAccount">
		<u--form :model="form" :rules="rules" ref="uForm" labelWidth="90" errorType="toast">
			<u-form-item label="开户名称" prop="bank_user">
				<u-input v-model="form.bank_user" placeholder="请输入" type="text" border="none"
					customStyle="text-align: right;" />
			</u-form-item>

			<u-form-item label="开户银行" prop="bank_wseq" @click="show = true">
				<view class="bank-text-wrap">
					<text :class="['bank-text', { isPlaceholder: !form.bank_wseq }]">{{
            form.bank_name || "下拉选择银行/支付宝"
          }}</text>
					<u-icon name="arrow-down"></u-icon>
				</view>
			</u-form-item>

			<u-form-item label="账号" prop="bank_code">
				<u-input type="number" placeholder="请输入" v-model="form.bank_code" border="none"
					customStyle="text-align: right;" />
			</u-form-item>
		</u--form>

		<view class="submit-btn" @click="submit" type="warning">提交</view>
		<view class="agree-box">
			<view class="unchecked-tips" v-show="showUnchecked">请先阅读并同意协议</view>
			<u-checkbox-group @change="termsChange" v-model="termsChecked">
				<u-checkbox name="term" shape="circle" size="36rpx"></u-checkbox>
			</u-checkbox-group>
			<text class="item-text">
				已阅读并同意
				<text class="text-primary fs14 fw600" @click="checkPrimary('2')">《隐私保护协议》</text>
				与
				<text class="text-primary fs14 fw600" @click="checkPrimary('1')">《用户协议》</text>
			</text>
		</view>

		<u-popup class="select-banks-popup" :show="show" mode="bottom" @close="close" :customStyle="{
        maxHeight: '68vh',
        padding: '0 20rpx',
        'overflow-y': 'auto',
      }">
			<view class="select-banks-title">
				<text>选择银行</text>
				<u-icon name="close" @click="close"></u-icon>
			</view>
			<view v-for="b in banksList" :key="b.id" class="select-banks-item" :style="{ backgroundColor: b.bank_bg }"
				@click="handleBank(b)">
				<view class="left">
					<u--image v-if="b.bank_logo" width="60rpx" height="60rpx" radius="100%" shape="circle"
						customStyle="border-radius:100%;" :src="b.bank_logo" :fade="true" duration="450"></u--image>
					<view class="name">{{ b.bank_name }}</view>
				</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { getBanksList, addBanks } from "@/common/api/withdrawal";
	export default {
		data() {
			return {
				banksList: [],
				show: false,
				showUnchecked: false,
				termsChecked: false,
				form: {
					bank_user: "",
					bank_code: "",
					bank_wseq: "",
					bank_name: "",
				},
				rules: {
					bank_user: [{
						required: true,
						message: "请输入开户名称",
					}, ],
					bank_wseq: [{
						required: true,
						message: "请选择开户银行",
					}, ],
					bank_code: [{
						required: true,
						message: "请输入账号",
					}, ],
				},
			};
		},
		onShow() {
			this.getBanksList();
		},
		methods: {
			termsChange(value) {
				if (value.length) {
					this.showUnchecked = false;
				} else {
					this.showUnchecked = true;
				}
			},
			checkPrimary(name) {
				uni.navigateTo({
					url: '/pages/singlePage/index?name=' + name
				});
			},
			getBanksList() {
				getBanksList().then(({ data = [] }) => {
					console.log("data=>", data);
					this.banksList = data;
				});
			},
			close() {
				this.show = false;
			},
			handleBank(b) {
				this.form.bank_name = b.bank_name;
				this.form.bank_wseq = b.bank_wseq;
				this.close();
			},
			submit() {
				if (!this.termsChecked.length) {
					this.showUnchecked = true;
					return;
				}
				this.$refs.uForm
					.validate()
					.then(() => {
						addBanks({
							bank_wseq: this.form.bank_wseq,
							bank_user: this.form.bank_user,
							bank_code: this.form.bank_code,
							status: 1,
						}).then(({ data }) => {
							console.log("data=>", data);
							uni.$u.toast("添加成功");
							uni.navigateBack();
						});
					})
					.catch((errors) => {
						const firstError = errors[0];
						console.log("errors=>", errors);
						uni.$u.toast(firstError.message);
					});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.addWithdrawalAccount {
		padding: 20rpx 20rpx 40rpx;

		::v-deep {
			.u-form-item {
				justify-content: center;
				margin-bottom: 20rpx;
				padding: 0 30rpx 0 25rpx;
				background-color: #fff;
				border-radius: 10rpx;
				min-height: 112rpx;

				.u-form-item__body__left__content__label {
					font-size: 32rpx;
					font-weight: 500;
					color: #232323;
					line-height: 1;
				}

				.u-border {
					border: none;
				}

				.uni-input-wrapper {
					text-align: right;
				}
			}
		}

		.agree-box {
			display: flex;
			color: $gray-1;
			position: relative;
			align-items: center;
			margin: 20rpx 30rpx;

			.unchecked-tips {
				position: absolute;
				height: 50rpx;
				color: #fff;
				background-color: rgba($color: #27282a, $alpha: 1);
				top: -60rpx;
				left: 0;
				border-radius: 8rpx;
				line-height: 50rpx;
				font-size: 24rpx;
				padding: 0 16rpx;

				&::after {
					display: "block";
					content: "";
					position: absolute;
					width: 20rpx;
					height: 20rpx;
					// background: rgba($color: #27282a, $alpha: 1);
					bottom: -6rpx;
					left: 10rpx;
					transform: rotate(45deg);
					border-radius: 4rpx;
				}
			}
		}

		.bank-text-wrap {
			display: flex;
			width: 100%;
			align-items: center;
			justify-content: flex-end;

			.bank-text {
				margin-right: 10rpx;

				&.isPlaceholder {
					color: #c0c4cc;
				}
			}
		}

		.submit-btn {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100%;
			height: 84rpx;
			margin-top: 40rpx;
			background: #f39800;
			border-radius: 39rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #ffffff;
			line-height: 1;
		}
	}

	.select-banks-popup {
		padding: 0 30rpx;
	}

	.select-banks-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #1d161f;
		height: 100rpx;
		flex-shrink: 0;
	}

	.select-banks-item {
		display: flex;
		justify-content: space-between;
		height: 80rpx;
		padding: 25rpx 20rpx;
		margin-bottom: 15rpx;
		background-color: #f5f6f8;
		border-radius: 10rpx;

		.left {
			display: flex;
			align-items: center;
			align-items: center;
		}

		.name {
			margin-left: 23rpx;
			font-size: 32rpx;
			font-weight: bold;
			color: #1d161f;
			line-height: 1;
		}
	}
</style>
