<template>
	<view class="content">
		<view class="top-title flex flex-space-between">
			<view class="left flex">
				<view class="text1">我的卡</view>
				<view class="text2">（共{{bankList?bankList.length:0}}张）</view>
			</view>

			<view @click="addBankCard" class="add-bankcard flex">
				<view class="car-text">添加银行卡</view>
				<u-icon class="icon" name="plus-circle-fill" color="#2979ff" size="24"></u-icon>
			</view>

		</view>
		<view class="card-list" v-for="(item,index) in bankList" :key='index'>
			<view class="card-list-item">
				<view class="flex flex-end number">{{item.bankCard}}</view>
				<view class="flex align-center">
					<image class="img left" src="@/static/images/mine/bank/gflogo@2x.png" mode='widthFix'>
					</image>
					<view class="right">
						<view class="text1">{{item.bankDeposit}}</view>
						<view class="text2">收款人：{{item.payeeName}}</view>
					</view>
				</view>
			</view>

			<view class="flex flex-space-between row2 align-center">
				<view class="flex align-center">
					<radio :checked="item.defaultFlag==1" @click='radioChange(item)' />
					<view class="default-card">设为默认收款账户</view>
				</view>
				<view>
					<u-icon name="trash" size="24" @click="del(item,index)"></u-icon>
				</view>
			</view>
		</view>
		<!-- <view @click="addBankCard">添加银行卡</view> -->
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				bankList: []
			};
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
		},
		onLoad() {
			if (!this.hasLogin) {
				uni.$u.route('/pages/public/login');
			}
			this.init();
		},
		methods: {
			radioChange(val) {debugger
				for (let i = 0; i < this.bankList.length; i++) {
					if (val.id == this.bankList[i].id) {
						this.bankList[i].defaultFlag = 1
						this.$request.baseRequest('post', '/driverPayeeInfo/api/setDefault', val).then(res => {
								if (res.code == '200') {
									// this.bankList = res.data
									this.$refs.uToast.show({
										type: 'success',
										message: "设置成功！",
									})
								} else {
									uni.$u.toast(res.message);
								}

							})
							.catch(res => {
								uni.$u.toast(res.message);
							});

					} else {
						this.bankList[i].defaultFlag = 0
					}
				}
			},
			init() {
				this.$request.baseRequest('post', '/driverPayeeInfo/selectDriverPayee', {
						driverId: this.userInfo.driverId,
					}).then(res => {
						if (res.code == '200') {
							this.bankList = res.data
						} else {
							uni.$u.toast(res.message);
						}

					})
					.catch(res => {
						uni.$u.toast(res.message);
					});
			},
			addBankCard() {
				uni.$u.route('/pages/mine/manageBankCards/addBankCard');
			},
			edit() {
				uni.$u.route('/pages/mine/manageBankCards/editBankCard');
				let params = {
					type: 'success',
					message: "修改成功",
					iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
				}
				this.$refs.uToast.show({
					...params
				})
			},
			del(val, index) {
				this.$request.baseRequest('post', '/driverPayeeInfo/api/deleteDriverPayee', {
						id: val.id
					}).then(res => {
						if (res.code == '200') {
							this.$refs.uToast.show({
								type: 'success',
								message: "删除成功！",
							})
							this.bankList.splice(index, 1)
						}

					})
					.catch(res => {
						uni.$u.toast(res.message);
					});
			}
		},

	}
</script>

<style lang="scss">
	.content {
		background: white;
		padding: 20rpx;
	}

	.top-title {
		margin: 0 0 20rpx 0;

		.left {
			.text1 {
				font-size: 29rpx;
				color: #1F1F1F;
				font-weight: 700;
			}

			.text2 {
				font-size: 29rpx;
				color: #999999;
			}
		}
	}

	.card-list {
		padding: 0 20rpx;

		.number {
			color: white;
		}
	}

	.add-bankcard {
		background: #EEF4FF;
		color: #2772FB;
		font-weight: 700;
		font-size: 26rpx;
		align-items: center;
		padding: 4rpx 10rpx 4rpx 20rpx;
		border-radius: 30rpx;

	}

	.card-list-item {
		background: url(../../../static/images/mine/bank/zsbg.png) center no-repeat;
		background-size: 100% 100%;
		padding: 60rpx 43rpx 100rpx 43rpx;

		.img {
			width: 80rpx;
			height: 80rpx;
			margin-right: 20rpx;
		}

		.right {
			.text1 {
				font-size: 32rpx;
				font-weight: 500;
				color: #FFFFFF;
			}

			.text2 {
				font-size: 24rpx;
				color: #FFFFFF;
				margin-top: 10rpx;
			}
		}
	}

	.row2 {
		margin: 20rpx 0;
	}

	.default-card {
		font-size: 28rpx;
		color: #333333;
	}
</style>
