<template>
	<view class="box">
		<view class="tixiancs">
			<view class="tixiancsLeft"></view>
			<view class="tixiancsRight" @click="goRouter('../referral/extractList?type=2')">提现明细</view>
		</view>
		<view class="header">
			<view class="title">提现金额</view>
			<view class="moneyView">
				<text class="icon">￥</text>
				<input type="digit" class="moneyInput" placeholder-style="color:#999999; font-size:30rpx"
					 v-model="money"  @input="inputValue" />
				<view class="allBtn" @click="allBtn">全部提现</view>
			</view>
			<view class="zongMoney">可提现金额: <span style="padding:  0 10rpx;">{{canMoney.toFixed(2)}} 元 </span> </view>

			
		</view>

		<view class="sureBtn" @click="sure">申请提现</view>

		<view class="tishi" style="padding:20rpx 20rpx 100rpx 20rpx ; ">
			<view class="tishi-title" style="color: #999999;padding-left: 18rpx;">
				提现说明:
			</view>
			<view class="tishiText">
				 <rich-text :nodes="description"></rich-text>
			</view>
			
			<!-- <view class="tishiLine">
				<view class="tishiTxt">1.最小提现金额：<span class="tishiTxtColor">{{minCash}}</span>元</view>
			</view>
			<view class="tishiLine">
				<view class="tishiTxt">2.只允许一笔提现在途</view>
			</view> -->
			<!-- <view class="tishiLine">
				<view class="tishiTxt">1.提现冻结天数：<span class="tishiTxtColor">{{detail.explain&&detail.explain.freezeDay}}</span>天</view>
			</view>
			<view class="tishiLine">
				<view class="tishiTxt">2.最小提现金额：<span class="tishiTxtColor">{{detail.explain&&detail.explain.singleLow}}</span>元</view>
			</view>
			<view class="tishiLine">
				<view class="tishiTxt">3.每月可提现次数：<span class="tishiTxtColor">{{detail.explain&&detail.explain.monthlyMost}}</span>次</view>
			</view> -->
		</view>

		

		

	</view>
</template>

<script>
	export default {
		data() {
			return {
				okMoney: '',
				money: 0,
				name: '',
				account: '',
				detail: {},
				alreadyMoney: 0,
				canMoney: 0,
				inMoney: 0,
				sumMoney: 0,
				minCash: 0.1,
				couContent: '', //提现规则
				cashState:null,
				timer :null,
				appId:'',
				mchId:'',
				outBatchNo:'',
				packageInfo:'',
				state:'',
				jkcashid:'',
				maxCash:0,
				description:'',
			}
		},
		onLoad() {
			this.getDetailData()
		},
		methods: {
			goRouter(url){
				uni.navigateTo({
					url:url
				})
			},
			getDetailData() {
				uni.showLoading({
					title: '加载中'
				})
				
				this.$http('jkCashout/getCashoutData', {}, 'GET').then(res => {
					uni.hideLoading();
					this.alreadyMoney = res.data.AlreadyMoney
					this.canMoney = res.data.CanMoney
					this.inMoney = res.data.InMoney
					this.sumMoney = res.data.SumMoney
					this.minCash = res.data.MinCash
					this.cashState = res.data.CashState
					this.maxCash = res.data.MaxCash
					this.description = res.data.description
				})
			},
			inputValue(event) {
				if (event.detail.value < 0) {
					setTimeout(() => {
						this.money = 0; // 解决
					}, 0)
				} else {
					this.money = event.detail.value;
				}
			},

			allBtn() {

				setTimeout(() => {
					this.money = this.canMoney.toFixed(2); // 解决
					
				}, 0)

			},
			sure() {
				 if (this.money > this.canMoney) {
				 	uni.showToast({
				 		title: '提现金额不能大于可提现金额',
				 		icon: 'none',
						duration: 3000,
				 	});
				 	return;
				 }
				 if (this.money <= 0) {
				 	uni.showToast({
				 		title: '提现金额不能为0',
				 		icon: 'none',
						duration: 3000,
				 	});
				 	return;
				 }
				 if (this.money < this.minCash) {
				 	uni.showToast({
				 		title: '提现金额最低为'+this.minCash,
				 		icon: 'none',
				 		duration: 3000,
				 	});
				 	return;
				 }
				 if (this.money > this.maxCash) {
				 	uni.showToast({
				 		title: '提现金额最高为'+this.maxCash,
				 		icon: 'none',
				 		duration: 3000,
				 	});
				 	return;
				 }
				this.postData();
			},
			postData() {
				uni.showLoading({
					title: '提现申请中..'
				})
				let url = 'jkCashout/applyJkCashout',
					params = {
						money: this.money,
						
					}
				this.$http(url, params, 'POST').then(res => {
					//uni.hideLoading();
					console.log(res)
					if(res.code ==0){
						this.canMoney = this.canMoney - this.money
					}
					//人工审核
					if(res.code==0 && res.data.cashType ==1){
						uni.showModal({
							title: '提交成功',
							content: '请耐心等待平台审核',
							showCancel:false,
							confirmText:'知道了',
							success: function(res) {
								uni.hideLoading();
							}
						});
					}else if(res.code==0 && res.data.cashType ==0){
						//自动审核
						//轮询查询状态 
						uni.showToast({
							title: '审核成功请等待..',
							icon: 'success',
							duration: 3000,
						});
						this.appId = res.data.appId
						this.mchId = res.data.mchId
						this.outBatchNo = res.data.outBatchNo
						this.packageInfo = res.data.packageInfo
						this.state = res.data.state
						this.jkcashid = res.data.jkcashid
						if(res.data.state =='WAIT_USER_CONFIRM'){
							this.getWechatMoney()
						}else{
							this.checkOrderStatus()
						}
					}else if(res.code==0 && res.data.cashType ==2){
						//线下
						uni.showToast({
							title: '申请成功请等待平台审核',
							icon: 'success',
							duration: 3000,
						});
						
					}
				})
			},
			async checkOrderStatus() {
			      try {
			       this.$http('jkCashout/checkJkCashoutState', {
			           outBatchNo:this.outBatchNo,
			       	 jkcashid:this.jkcashid
			       }, 'POST').then(res => {
			       	console.log(res);
			       	if(res.code == 0 && res.data.state =='WAIT_USER_CONFIRM'){
			       		uni.hideLoading();
						clearTimeout(this.timer);
			       		this.getWechatMoney();
			       	}
			       	 else if(res.code == 0 && res.data.state =='SUCCESS'){
			       		//成功页面
			       		console.log("领取成功-跳转成功页面");
						clearTimeout(this.timer);
			       		this.goRouter('../subPack/extractSuccess?money='+this.money);
			       	}
			       	else if(res.code == 0 && res.data.state =='FAIL'){
			       		//失败页面
			       		console.log("领取失败-跳转失败页面");
						clearTimeout(this.timer);
			       		this.goRouter('../subPack/extractFail?money='+this.money)
			       	}else if(res.code == 1){
						
						uni.showToast({
							title: res.msg,
							icon: 'error',
							duration: 3000,
						});
						clearTimeout(this.timer);
					}
					else{
						console.log("else")
						this.timer = setTimeout(this.checkOrderStatus, 2000);
					}
			       })
			      } catch (error) {
			        console.error('查询订单状态失败:', error);
			        // 出错时也继续轮询
			        clearTimeout(this.timer);
			      }
			},
			  checkOrderStatusMounted() {
			    this.checkOrderStatus(); // 立即执行第一次查询
			  },
			  beforeDestroy() {
			    clearTimeout(this.timer);
			  },
			  getWechatMoney(){
			  	if (wx.canIUse('requestMerchantTransfer')) {
			  	  wx.requestMerchantTransfer({
			  	      mchId: this.mchId,
			  	      appId: this.appId,
			  	      package: this.packageInfo,
			  	      success: (res) => {
			  	        // res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
			  			console.log(res);
			  			 if (res.errMsg === 'requestMerchantTransfer:ok') {
							 //展示页面成功
			  				this.checkOrderStatusMounted();
			  			}
						if (res.errMsg === 'requestMerchantTransfer:fail') {
							//展示页面失败
							//this.checkOrderStatusMounted();
						}
						if (res.errMsg === 'requestMerchantTransfer:cancel') {
							//用户点击取消
							this.beforeDestroy();
							//this.userCancelMoney();
							this.goRouter('../referral/extractList?type=2');
						}
			  	      },
			  	      fail: (res) => {
						  console.log(res);
			  			 if (res.errMsg === 'requestMerchantTransfer:fail:internal error') {
			  			 	//用户点击取消
			  			 	this.beforeDestroy();
			  			 	//this.userCancelMoney();
							this.goRouter('../referral/extractList?type=2');
			  			 }
						 
						 
			  	      }
			  	    });
			  	}
			  },
			  userCancelMoney(){
				  //用户取消提现 outBatchNo:this.outBatchNo,
				  
				  this.$http('jkCashout/failJkCashout', {
					 jkcashid:this.jkcashid
				  }, 'POST').then(res => {
					
				 })
			  }
		}
	}
</script>

<style>
	.box {
		min-height: 100vh;
		background: #F4F5F7;
		padding-top: 20rpx;
	}
	.tishiLine{
		display: flex;padding-top: 20rpx;
		color: #999999;
	}
	.tishiY{
		width: 10rpx;
		height: 10rpx;
		background: #DDDDDD;
		margin-top: 16rpx;
		border-radius: 50%;
	}
	.tishiTxt{
		padding-left: 18rpx;
		color: #999999;
	}
.tixiancs{
	display: flex;
	justify-content: space-between;
	font-size: 24rpx;
	padding-left: 24rpx;
	padding-right: 24rpx;
	padding-bottom: 10rpx;
}
.tixiancsLeft{
	color: #999999;
}
.tixiancsRight{
	 color:#596999;
}
.tishiTxtColor{
	color: #999999;
}
	.header {
		background-color: #FFFFFF;
		margin: 0 24rpx;
		border-radius: 10rpx;
		padding: 30rpx 20rpx;
		
	}

	.title {
		font-size: 28rpx;
		color: #3C3C3C;
		
	}

	.moneyView {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 15rpx 0;
	}

	.icon {
		font-size: 50rpx;
		color: #222222;
		
	}

	.moneyInput {
		font-size: 70rpx;
		color: #222222;
		/* flex-grow: 1; */
		height: 98rpx;
		width: 100%;
	}

	.allBtn {
		font-size: 30rpx;
		color: #596999;
		width: 200rpx;
		height: 50rpx;
		line-height: 50rpx;
		margin-left: 10rpx;
		text-align: right;
		position: absolute;
		right: 45rpx;
		top: 40px;
	}

	.zongMoney {
		font-size: 25rpx;
		color: #999999;
		border-top: solid 3rpx #F4F4F4;
		padding-top: 25rpx;
	}

	.line {

		background-color: #EEEEEE;
		height: 1rpx;
	}

	.name {
		display: flex;
		justify-content: flex-start;
		padding: 28rpx 0;
	}

	.leftTitle {
		font-size: 30rpx;
		color: #3C3C3C;
		margin-right: 90rpx;
	}

	.Input {
		font-size: 30rpx;
		color: #333333;

	}

	.tishi {
		padding: 0 44rpx;
		font-size: 26rpx;
		color: #999999;
		line-height: 1.5;
		
	}
	.tishi-title{
		padding-left: 18rpx;
	}
	.tishiText{
		padding-left: 18rpx;
	}
	
	.sureBtn {
		width: 690rpx;
		margin: 40rpx 30rpx;
		border-radius: 15rpx;
		height: 74rpx;
        background: #EB0F0A;
		color: #FFFFFF;
		font-size: 30rpx;
		text-align: center;
		line-height: 74rpx;
	}
</style>
