<template>
	<view class="container">
		<!-- #ifdef APP -->
		<text class="iconfont icon-youjiantou zuojiantou" @click="goBack()" style="top: 20rpx;"></text>
		<!-- #endif -->
		
		<view class="top">
			<view>
				可兑换
			</view>
			<view>
				{{exchangeAmount}}积分
			</view>
		</view>
		<view class="content_wrapper">
			<view class="item">
				<text>提现金额(税前)</text>
				<text>{{exchangeAmount}}</text>
			</view>
			<view class="item">
				<text>代缴税</text>
				<text>{{taxAmount}}</text>
			</view>
			<view class="item">
				<text>实际到账</text>
				<text>{{noTaxAmount}}</text>
			</view>
		</view>
		<view class="content_wrapper tixian">
			<h4>提现到</h4>
			<view class="paymentItem" @click="selectPaymentMethod(2)">
				<view>
					<i class="iconfont icon-zhifubao"></i>
					<text style="font-size: 28rpx;">支付宝钱包</text>
				</view>
				<view class="selectRadio" :class="{'actived':paymentMethod===2}">
					<i class="iconfont icon-duihao" style="font-size: 28rpx;"></i>
				</view>
				
			</view>
			<!-- <view class="paymentItem" @click="selectPaymentMethod(1)">
				<view>
					<i class="iconfont icon-py_weixinzhifu"></i>
					<text>微信钱包</text>
				</view>
				<view class="selectRadio" :class="{'actived':paymentMethod===1}">
					<i class="iconfont icon-duihao"></i>
				</view>
			</view> -->
			<view class="tips_wrapper">
				<view class="">
					提示:
				</view>
				<view class="">
					1、金额低于1元时无法提现;
				</view>
				<view class="">
					2、当前可提金额是您的税前收入，具体计税规则可参考 
				</view>
				<view class="">
					<navigator class="rule" url="/pages/taxRules/taxRules">《劳务报酬税缴纳规则》;</navigator>
				</view>
				<view class="">
					3、如需帮助，请联系银盾家办客服电话400-921-9290;
				</view>
			</view>
			<view class="btn" @click="confirmWithdrawal()" :class="{'gray':readonlyFlag}" style="bottom: 30rpx;font-size: 28rpx;">
				确认提现
			</view>
		</view>
	</view>
</template>

<script>
	import api from '../../api/api';
	export default{
		data(){
			return {
				exchangeAmount:null,
				partnerTradeNo:null,
				taxAmount:null,
				noTaxAmount:null,
				paymentMethod:2,//初始化支付宝提现
				aliWithdrawalResVO:null,
				wxWithdrawalResVO:null,
				readonlyFlag:false,
			}
		},
		onLoad(options){
			this.exchangeAmount = options.exchangeAmount;
			this.partnerTradeNo = options.partnerTradeNo;
			this.goFortuneWithdrawal()
		},
		methods:{
			goBack() {
				uni.navigateBack({
					delta: 1  
				});
			},
			goFortuneWithdrawal(){
				const param = {
					userId:uni.getStorageSync('cffp_userId'),
					// userId:1,
					exchangeAmount:this.exchangeAmount,
					partnerTradeNo:this.partnerTradeNo?this.partnerTradeNo:null
				}
				api.goFortuneWithdrawal(param).then((res)=>{
					if(res['success']){
						const data = res['data']
						this.exchangeAmount = data.exchangeAmount;
						this.partnerTradeNo = data.partnerTradeNo;
						this.taxAmount = data.taxAmount;
						this.noTaxAmount = data.noTaxAmount;
						this.aliWithdrawalResVO = data['aliWithdrawalResVO'];
						this.wxWithdrawalResVO = data['wxWithdrawalResVO'];
					}else{
						this.exchangeAmount = 0;
						this.partnerTradeNo = null;
						this.taxAmount = 0;
						this.noTaxAmount = 0;
						uni.showModal({
						    content: res['message'],
							success: function (res) {
						   		if (res.confirm) {
						   			console.log('用户点击确定');
						   		} else if (res.cancel) {
									console.log('用户点击取消');
						   		}
							}
						});
					}
				})
			},
			selectPaymentMethod(value){
				this.paymentMethod = value;
			},
			//支付宝去提现
			aliWithdrawal(){
				this.readonlyFlag = true;
				//#ifdef H5
				let aliWithdrawalResVOParam = {
					...this.aliWithdrawalResVO,
					dataSource:2,
					orderNo:this.partnerTradeNo,
					amount:this.exchangeAmount,
					userId:uni.getStorageSync('cffp_userId'),
				}
				//#endif
				// #ifdef APP-PLUS
				let aliWithdrawalResVOParam = {
					...this.aliWithdrawalResVO,
					dataSource:2,
					orderNo:this.partnerTradeNo,
					amount:this.exchangeAmount,
					userId:uni.getStorageSync('cffp_userId'),
				}
				//#endif
				api.aliWithdrawal(aliWithdrawalResVOParam).then((res)=>{
					this.readonlyFlag = false;
					if(res['success']){
						uni.showModal({
						    content: '提现成功！',
							showCancel: false, // 不显示取消按钮
							success: function (res) {
						   		if (res.confirm) {
						   			console.log('用户点击确定');
						   		} else if (res.cancel) {
									console.log('用户点击取消');
						   		}
							}
						});
					}else{
						uni.showModal({
						    content: res['message'],
							showCancel: false, // 不显示取消按钮
							success: function (res) {
						   		if (res.confirm) {
						   			console.log('用户点击确定');
						   		} else if (res.cancel) {
									console.log('用户点击取消');
						   		}
							}
						});
					}
					
				})
			},
			//微信去提现
			wxWithdrawal(){
				// APP：
				// #ifdef APP-PLUS
				uni.login({
				    provider: 'weixin',
				    success: function (loginRes) {
				        // 登录成功
						// alert(JSON.stringify(loginRes))
				        uni.getUserInfo({
				            provider: 'weixin',
				            success: function(info) {
				                // 获取用户信息成功, info.authResult保存用户信息
								console.log(info)
								this.wxWithdrawalResVO.openId=info.openId;
								const wxWithdrawalResVO = {
									...this.wxWithdrawalResVO,
									userId:uni.getStorageSync('cffp_userId'),
								}
								api.wxWithdrawal(wxWithdrawalResVO).then((res)=>{
									console.log(res)
								})
				            }
				        })
				    },
				    fail: function (err) {
				        // 登录授权失败  
				        // err.code是错误码
				    }
				})
				// #endif
			},
			confirmWithdrawal(){
				if(!this.exchangeAmount || this.exchangeAmount==0){
					uni.showModal({
					    content: '金额低于1元时无法提现',
						success: function (res) {
					   		if (res.confirm) {
					   			console.log('用户点击确定');
					   		} else if (res.cancel) {
								console.log('用户点击取消');
					   		}
						}
					});
					return false;
				}
				//#ifdef H5
				this.aliWithdrawal();
				//#endif
				// APP：
				// #ifdef APP-PLUS
				if(!this.readonlyFlag){
					this.readonlyFlag = true;
					this.aliWithdrawalResVO = {
						...this.aliWithdrawalResVO,
						orderNo:this.partnerTradeNo,
						amount:this.exchangeAmount,
						userId:uni.getStorageSync('cffp_userId'),
					}
					api.isAuth(this.aliWithdrawalResVO).then((res)=>{
								this.readonlyFlag = false;
								if(res['success']){
									this.aliWithdrawalResVO.aliUserId = res['data']['aliUserId'];
									if(res['data']['isAuth'] == 1){
										if(this.paymentMethod == 1){
											this.wxWithdrawal();
										}
										if(this.paymentMethod == 2){
											this.aliWithdrawalResVO.aliAuthUrl=res['data']['aliAuthUrl'];
											// APP：
											// #ifdef APP-PLUS
											let urls= this.aliWithdrawalResVO.aliAuthUrl;
											urls=encodeURIComponent(urls);
											// 判断平台
											if (plus.os.name == 'Android') {
											  plus.runtime.openURL(
											    'alipays://platformapi/startapp?appId=20000067&url=' + urls,
											    res => {
												//这里写打开URL地址失败后的处理
											      console.log(res);
											      uni.showModal({
											        content: '本机未检测到对应客户端，是否打开浏览器访问页面？',
											        success: function (res) {
											          if (res.confirm) {
											            //plus.runtime.openURL();
											          }
											        }
											      });
											    },
											    'com.eg.android.AlipayGphone'
											  );
											} else if (plus.os.name == 'iOS') {
											  plus.runtime.openURL(
											    'alipay://platformapi/startapp?appId=20000067&url=' + urls,
											    res => {
											      console.log(res);
											      uni.showModal({
											        content: '本机未检测到对应客户端，是否打开浏览器访问页面？',
											        success: function (res) {
											          if (res.confirm) {
											            //plus.runtime.openURL(url);
											          }
											        }
											      });
											    },
											    'com.eg.android.AlipayGphone'
											  );
											}
											// #endif
										}
									}else{
										if(this.paymentMethod == 1){
											this.wxWithdrawal();
										}
										if(this.paymentMethod == 2){
											this.aliWithdrawal();
										}
										
									}
								}else{
									uni.showModal({
									    content: res['message'],
										success: function (res) {
									   			if (res.confirm) {
									   				console.log('用户点击确定');
									   			} else if (res.cancel) {
													console.log('用户点击取消');
									   			}
										}
									});
								}
							});
					}
				// #endif
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		box-sizing: border-box;
		padding: 20rpx;
	}
	.top{
		width: 100%;
		text-align: center;
		padding: 20rpx 0;
		background-color: #fff;
		border-radius: 10rpx;
		view:nth-child(1){
			font-size: 36rpx;
			color: #666;
			margin-bottom: 10rpx;
		}
		view:nth-child(2){
			font-size: 40rpx;
			color: #2A36AD;
		}
	}
	.content_wrapper{
		border-radius: 10rpx;
		background: #fff;
		margin: 20rpx auto;
		padding:0 30rpx;
		.btn{
			background-color: #2A36AD;
		}
		.btn.gray{
			background: #666;
		}
		.item{
			display: flex;
			padding: 20rpx 0;
			border-bottom: 2rpx solid #F2F2F2;
			justify-content: space-between;
			font-size: 28rpx;
		}
		h4{
			font-size: 32rpx;
			margin-bottom: 20rpx;
		}
		.paymentItem{
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 10rpx;
			view{
				display: flex;
				align-items: center;
			}
			.icon-zhifubao{
				color: #5A9EF7;
				font-size: 60rpx;
				margin-right: 20rpx;
			}
			.icon-py_weixinzhifu{
				color: #09BB07;
				font-size: 60rpx;
				margin-right: 20rpx;
			}
			.selectRadio{
				width: 40rpx;
				height: 40rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 50%;
				border: 1px solid #D8D8D8;
				color: transparent;
				&.actived{
					background-color: #0A2F99;
					color: #fff;
				}
			}
		}
		.withdrawal_content{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 20rpx;
			margin-bottom: 20rpx;
			.icon-py_weixinzhifu{
				color:#09BB07;
				font-size: 80rpx;
				margin-right: 30rpx;
			}
			.icon-gou{
				font-size: 48rpx;
				color: #2A36AD;
			}
			
		}
		.tips_wrapper{
			font-size: 26rpx;
			view{
				color: #666666;
				margin-bottom: 10rpx;
			}
			.rule{
				color: #2A36AD
			}
		}
	}
	.tixian{
		padding: 20rpx;
	}
</style>