<template>
	<view>
		<u-navbar :custom-back="btnback" title="余额提现" :border-bottom="false" :background="bgc" title-color='#fff' back-icon-color="#fff"
			title-size='36' height='50'></u-navbar>
		<view class="cont">
			<view class="top_box">
				<view class="tip">账户余额(元)</view>
				<view class="txt">
					￥{{userinfo.balance == undefined ? '' : Number(userinfo.balance) + Number(userinfo.waitBonusAmount)}} <text style="margin-left: 20rpx;">{{'待结算(' + userinfo.waitBonusAmount + ')'}}</text>
				</view>
				<view class="tip" style="display: flex;justify-content: space-between;">
					<text>累计提现：￥{{userinfo.withDrawlAmount == undefined ? '' : userinfo.withDrawlAmount}}</text>
					<!-- <text v-if="userinfo.waitBonusAmount != 0 || userinfo.waitBonusAmount != null">待结算：￥{{userinfo.waitBonusAmount}}</text> -->
				</view>
			</view>
			<view class="tit">
				<view class="" style="display: flex;">
					<view class="tit_line" style="margin-top: 4rpx;"></view>
					余额提现
				</view>
				<view class="" style="color: #8883f0;" @click="btntxrecord">
					提现记录
				</view>
			</view>
			<view class="input_box">
				<view class="yuan"> 
					￥
				</view>
				<view class="ipt">
					<u-input v-model="money" type="number" @input="change" placeholder='输入自定义金额'
						placeholder-style='font-size: 24rpx;' />
				</view>
				<view class="altx" @click="all()">
					全部提现
				</view>
			</view>
			<view class="" style="color: red;font-size: 24rpx;padding-top: 30rpx;box-sizing: border-box;">
				提现服务费为 ￥{{roundToTwoDecimalsTraditional(this.serviceRates)}}   最低提现金额为￥20
			</view>
			<view class="tit">
				<view class="" style="display: flex;">
					<view class="tit_line" style="margin-top: 4rpx;">

					</view>
					提现方式
				</view>
			</view>
			<view class="pay_type" style="width: 100%;">
				<view class="type_box" v-if="listflag" v-for="(item,index) in txlist" :key="index"
					@click="pay(index,item.channelId)">
					<view class="box_left">
						<image :src="item.picture" mode=""></image>
						<view class="text">
							{{item.name}}
						</view>
					</view>
					<view class="box_right" :class="currentindex==index?'act1':''">
						<span v-if="currentindex==index" style="color: #fff;"> <u-icon style="margin-top: 22rpx;"
								name="checkbox-mark" color="#fff" size="28"></u-icon> </span>
					</view>
				</view>

				<view v-if="!listflag" class="wutx" style="width: 100%;margin-top: 230rpx;text-align: center;">
					<view class="" style="font-size: 32rpx;">
						当前暂无提现方式
					</view>
					<view class="addtj" @click="btnadd">
						去添加
					</view>
				</view>
			</view>
			<view class="btn" v-if="listflag" @click="sub()">
				确认提现
			</view>
			<view v-if="btnmsk"
				style="width: 100%;height: 100vh;position: fixed;top: 0;left: 0;background-color: #000;opacity: .1;z-index: 999;">
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				btnmsk: false,
				bgc: {
					backgroundColor: "#8883f0",
				},
				name: '',
				idnum: '',
				money: '',
				infoList: {},
				currentindex: -1,
				payType: 1,
				accountNo: '',
				userinfo: {},
				serviceType: '',
				serviceRate: '',
				serviceRates: '',
				sjmeoey: '',
				txlist: '',
				channelId: '',
				listflag: true
			}
		},
		onShow() {
			this.gettxlist()
			// this.getfuwu()
			this.getinfo()
			this.getuserinfo()
		},
		// 分享到好友（会话）
		onShareAppMessage: function() {
			return {
				title: '创想物联',
				path: '/pages/shouye/index'
			}
		},

		// 分享到朋友圈  
		onShareTimeline: function() {
			return {
				title: '创想物联',
				query: '',
				path: '/pages/shouye/index'
			}
		},
		methods: {
			// 添加提现方式
			btnadd() {
				uni.navigateTo({
					url: '/page_user/skzh'
				})
			},
			// 获取提现渠道列表
			gettxlist() {
				this.$u.get('/app/channel/withdraw/enabledList').then(res => {
					if (res.code == 200) {
						if (res.data.length > 0) {
							this.listflag = true
							this.txlist = res.data
						} else {
							this.listflag = false
						}
					}
				})
			},
            btnback(){
				uni.switchTab({
					url:'/pages/my'
				})
			},

			change() {
				if (this.serviceType == 2) {
					this.serviceRates = this.serviceRate
					this.sjmeoey = Number(this.money) - Number(this.serviceRates)
				} else {
					let qian = this.money * this.serviceRate / 100
					this.serviceRates = qian
					this.sjmeoey = Number(this.money) - Number(this.serviceRates)
				}
			},
			// 计算小数
			roundToTwoDecimalsTraditional(num) {
				return Math.round(num * 100) / 100;
			},
			// 跳转到提现记录
			btntxrecord() {
				uni.navigateTo({
					url: '/page_user/txrecord'
				})
			},
			getinfo() {
				this.$u.get('/app/account').then((res) => {
					if (res.code == 200) {
						this.infoList = res.data
					}
				})
			},
			pay(index, id) {
				this.channelId = id
				this.getfuwu()
				this.currentindex = index
			},
			all() {
				this.money = this.userinfo.balance
				if (this.serviceType == 2) {
					this.serviceRates = this.serviceRate
					this.sjmeoey = Number(this.money) - Number(this.serviceRates)
				} else {
					let qian = this.money * this.serviceRate / 100
					this.serviceRates = qian
					this.sjmeoey = Number(this.money) - Number(this.serviceRates)
				}
			},
			// 获取提现服务费
			getfuwu() {
				this.$u.get(`/app/bill/getWithdrawService?channelId=${this.channelId}`).then((res) => {
					if (res.code == 200) {
						this.serviceType = res.data.serviceType
						this.serviceRate = res.data.serviceRate
						if (this.serviceType == 2) {
							this.serviceRates = this.serviceRate
						} else {
							// this.serviceRates = 0
							this.serviceRates = this.serviceRate * this.money / 100
						}
					}
				})
			},
			sub() {
				if (this.userinfo.limitWithdraw == true) {
					uni.showToast({
						title: this.userinfo.limitWithdrawReason,
						icon: 'none',
						duration: 2000
					})
					setTimeout(()=>{
						uni.navigateTo({
							url: '/page_components/shiming'
						})
					},1000)
				}else{
					if (this.userinfo.isReal == true) {
						if (this.money > this.userinfo.balance) {
							uni.showToast({
								title: '提现金额不能大于余额',
								icon: 'none',
								duration: 2000
							});
						} else if (this.currentindex == -1) {
							uni.showToast({
								title: '请选择提现方式',
								icon: 'none',
								duration: 2000
							})
						} else {
							this.btnmsk = true
							let data = {
								money: this.money,
								arrivalAmount: this.roundToTwoDecimalsTraditional(Number(this.money) - Number(this
									.roundToTwoDecimalsTraditional(this.serviceRates))),
								serviceCharge: this.roundToTwoDecimalsTraditional(this.serviceRates),
								channelId: this.channelId,
								serviceRate: this.serviceRate, //服务费
								serviceType: this.serviceType //服务费收取方式
							}
							this.$u.post('/app/bill/withdraw', data).then((res) => {
								if (res.code == 200) {
									this.getuserinfo()
									uni.showToast({
										title: '申请提现成功',
										icon: 'success',
										duration: 2000
									})
									this.serviceRates = 0
									this.btnmsk = false
								}else if(res.code == 10001){
									uni.navigateTo({
										url: '/page_components/shiming?type=' + 1
									})
								}else if(res.code == 102001){
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 2000
									})
									setTimeout(()=>{
										uni.navigateTo({
											url: '/page_components/shiming?type=' + 2
										})
									},1000)
								}else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 2000
									})
									this.btnmsk = false
								}
							})
						}
					} else {
						uni.navigateTo({
							url: '/page_components/shiming'
						})
					}
				}
			},
			getuserinfo() {
				this.$u.get("/app/user/userInfo").then((res) => {
					if (res.code == 200) {
						this.userinfo = res.data
						this.money = ''
					}
				})
			},

		}
	}
</script>

<style lang="scss">
	/deep/ .u-flex {
		padding-top: 20rpx !important;
	}

	/deep/ .u-title {
		padding-bottom: 22rpx;
	}

	/deep/ .u-icon__icon {
		padding-bottom: 22rpx;
	}

	page {
		background: #F7FAFE;
	}

	.addtj {
		margin-top: 44rpx;
		width: 646rpx;
		height: 88rpx;
		background: #E4E3FB;
		box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		text-align: center;
		line-height: 88rpx;
		font-size: 36rpx;
		color: #8883F0;
	}

	.cont {
		display: flex;
		flex-wrap: wrap;
		margin: 0 auto;
		width: 634rpx;

		.top_box {
			padding: 28rpx 32rpx;
			margin-top: 38rpx;
			width: 634rpx;
			height: 246rpx;
			background: #8883F0;
			border-radius: 28rpx 28rpx 28rpx 28rpx;
			border-radius: 20rpx;

			.tip {
				font-weight: 400;
				font-size: 28rpx;
				color: #FFFFFF;
				line-height: 38rpx;
			}

			.txt {
				margin-top: 28rpx;
				margin-bottom: 28rpx;
				font-weight: 500;
				font-size: 40rpx;
				color: #FFFFFF;
				line-height: 54rpx;
			}
		}

		.tit {
			margin-top: 38rpx;
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			font-weight: 500;
			font-size: 28rpx;
			color: #3D3D3D;
			line-height: 38rpx;
			display: flex;
			justify-content: space-between;
			width: 100%;

			.tit_line {
				margin-right: 12rpx;
				width: 4rpx;
				height: 32rpx;
				background: #8883F0;
				border-radius: 0rpx 0rpx 0rpx 0rpx;
			}

		}

		.input_box {
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			margin-top: 36rpx;
			width: 634rpx;
			height: 86rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
			border-radius: 20rpx;

			.yuan {
				margin-left: 36rpx;
				font-weight: 500;
				font-size: 52rpx;
				color: #8883F0;
				line-height: 72rpx;
			}

			.ipt {
				width: 60%;
			}

			.altx {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 158rpx;
				height: 56rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.2);
				border-radius: 12rpx 12rpx 12rpx 12rpx;

				font-weight: 400;
				font-size: 28rpx;
				color: #8883F0;
				line-height: 38rpx;
			}
		}

		.pay_type {
			.type_box {
				width: 100%;
				margin-top: 26rpx;
				display: flex;
				align-items: center;
				flex-wrap: nowrap;
				width: 634rpx;
				height: 86rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
				border-radius: 20rpx;

				.box_left {
					display: flex;
					align-items: center;
					flex-wrap: nowrap;
					margin-left: 22rpx;

					image {
						width: 50rpx;
						height: 50rpx;
					}

					.text {
						margin-left: 30rpx;
						font-weight: 400;
						font-size: 32rpx;
						color: #3D3D3D;
						line-height: 44rpx;
					}
				}

				.box_right {
					display: flex;
					align-items: center;
					justify-content: center;
					margin-left: auto;
					margin-right: 34rpx;
					width: 44rpx;
					height: 44rpx;
					border: 2rpx solid #444444;
					border-radius: 50%;
				}

				.act1 {
					color: #FFFFFF;
					background-color: #8883F0;
				}
			}
		}

		.btn {
			margin-left: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 218rpx;
			width: 590rpx;
			height: 84rpx;
			background: #8883f0;
			border-radius: 40rpx;
			font-family: AlibabaPuHuiTi, AlibabaPuHuiTi;
			font-weight: 400;
			font-size: 36rpx;
			color: #FFFFFF;

		}
	}
</style>