<template>
<view class="box">
	<view style="height: 20rpx;"></view>
 <view class="line" v-for="(item,index) in list">
	 <view class="linetop" v-if="type!=2">
		 
		 <view class="nickNameBox">
			 <view class="amount" >￥{{item.money}}</view>
		 </view>
	 </view>
	 <view class="lineCOnt"  v-if="type!=2">
	 		 <view class="lineline">
	 		 	<view class="lineContLeft">
	 				 <view class="lineContLeftMs">申请时间：{{item.applyTime}}</view>
	 		 	</view>
	 			<view class="lineTIme" >{{jkstateMap[~~item.applyState]}}</view>
	 			<view class="lineTIme">{{stateMap[~~item.auditState]}}</view>
	 		 </view>
	 </view>
	 <view class="linetop" v-if="type==2">
		 <view class="name">提现金额：{{item.applyMoney.toFixed(2)}}</view>
		 
		 <view class="resultTitle">
		 	<span v-if="item.cashType ==0 && item.cashState==3">线上打款/打款成功</span>
			<span v-if="item.cashType ==0 && item.cashState==2">线上打款/打款失败</span>
			<span v-if="item.cashType ==0 && item.cashState==1">线上打款/打款中</span>
			<span v-if="item.cashType ==0 && item.cashState==0">线上打款/未打款</span>
			<span v-if="item.cashType ==1 && item.cashState==3">线下打款/打款成功</span>
			<span v-if="item.cashType ==1 && item.cashState==2">线下打款/打款失败</span>
			<span v-if="item.cashType ==1 && item.cashState==1">线下打款/打款中</span>
			<span v-if="item.cashType ==1 && item.cashState==0">线下打款/未打款</span>
		 </view>
		 
		 <view class="lineTIme jkButton" @click="getMoney(item)" v-if="(item.applyState == 2 && item.cashState == 0 && item.cashType == 0) ||(item.applyState == 2 && item.cashState == 1 && item.cashType == 0)">确认收款</view>
		 <!-- <view class="lineTIme jkButton"  v-if="item.applyState == 2 && item.cashState == 1 && item.cashType == 0">提现中</view>
		 <view class="lineTIme jkButton"  v-if="item.applyState == 2 && item.cashState == 2 && item.cashType == 0">提现失败</view>
		 <view class="lineTIme jkButton"  v-if="item.applyState == 2 && item.cashState == 3 && item.cashType == 0">已提现</view>
		 <view class="lineTIme jkButton"  v-if="item.applyState == 2 && item.cashType == 1">线下打款</view> -->
	 </view>
	 
	 <view class="lineCOnt" v-if="type==2">
	 		 <view class="lineline">
	 		 	<view class="lineContLeft">
	 				 <view class="lineContLeftMs">申请时间：{{item.applyTime}}</view>
	 		 	</view>
	 			
				<view class="lineTIme" :style="[
					  item.applyState === 2 && { color: '#4FDF23' },
					  item.applyState === 0 && { color: 'red' },
					  item.applyState === 1 && { color: '#7F7E7E' }
					]"
				>{{jkstateMap[~~item.applyState]}}</view>
	 		 </view>
	 		
	 </view>
	 
	 
	 
 </view>
 <nodata v-if="list.length==0"></nodata>
</view>
</template>

<script>
	import { requestTransfer } from '@/utils/transfer';
	import nodata from '../../components/nodata/nodata.vue'
	export default {
		components: {
			nodata
		},
		
		data() {
			return {
				list:'',
				themeColor:'',
				userInfo:'',
				type:0,
				stateMap: ['待审核', '审核通过', '审核拒绝'],
				jkstateMap: ['未审核', '审核拒绝', '审核通过'],
				outBatchNo:'',
				packageInfo:'',
				state:'',
				mchId:'',
				appId:'',
				timer:'',
				jkcashid:'',
				cashState:2,
				showGet:1,
				money:0,
				 intervalId: null
			}
		},
		onLoad(opt) {
			this.themeColor = uni.getStorageSync("themeColor");
			this.userInfo = uni.getStorageSync("userInfo");
			  this.type = opt.type;
			 if(this.type==2){
				 this.getListByJkCashout()
			 }else{
				 this.getList()
			 }
			 
			
		},
		onShow() {
			
		},
		methods: {
			goRouter(url){
				uni.navigateTo({
					url:url
				})
			},
			getList(){
				uni.showLoading({
					title: '加载中'
				})
				this.$http('openMCustomer/getWithdrawalDetail', {
				    
				}, 'GET').then(res => {
					uni.hideLoading();
					this.list = res.data;
				})
			},
			//推广
			getListByJkCashout(){
				uni.showLoading({
					title: '加载中'
				})
				this.$http('jkCashout/getJkCashoutDetail', {
				    
				}, 'GET').then(res => {
					uni.hideLoading();
					this.list = res.data;
					
				})
			},
			//提现
			getMoney(jk){
				uni.showLoading({
					title: '加载中'
				})
				if(jk.cashState == 0 || jk.cashState == 1){
					this.money = jk.applyMoney
					this.$http('jkCashout/jkCashout', {
					    jkcashid:jk.id
					}, 'POST').then(res => {
						uni.hideLoading();
						if(res.code == 0){
							this.outBatchNo = res.data.outBatchNo
							this.packageInfo = res.data.packageInfo
							this.state = res.data.state
							this.mchId = res.data.mchId
							this.appId = res.data.appId
							this.jkcashid = jk.id
							uni.showLoading({
								title: '请稍候...'
							})
							this.stopInterval()
							 this.intervalId = setInterval(() => {
							      this.checkJkCashoutState()
							    }, 2000)
						}else{
							uni.showLoading({
								title: res.msg
							})
							setTimeout(() => {
							  uni.hideLoading()
							}, 2000)
						}
					})
				}else{
					uni.hideLoading();
				}
				
			},
			stopInterval() {
			    if (this.intervalId) {
			      clearInterval(this.intervalId);
			      this.intervalId = null;
			    }
			},
			//查询状态
			checkJkCashoutState(){
				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();
						this.stopInterval();
						this.getWechatMoney();
					}
					else if(res.code == 0 && res.data.state =='SUCCESS'){
						//成功页面
						console.log("领取成功-跳转成功页面");
						this.goRouter('../subPack/extractSuccess?money='+this.money);
					}
					else if(res.code == 0 && res.data.state =='FAIL'){
						//失败页面
						console.log("领取失败-跳转失败页面");
						this.goRouter('../subPack/extractFail?money='+this.money)
					}else if(res.code == 1){
						uni.showToast({
							title: res.msg,
							icon: 'error',
							duration: 3000,
						});
						this.stopInterval();
					}else{
						this.stopInterval();
						uni.hideLoading();
					}
				})
			},
			getWechatMoney(){
				if (wx.canIUse('requestMerchantTransfer')) {
				  wx.requestMerchantTransfer({
				      mchId: this.mchId,
				      appId: this.appId,
				      package: this.packageInfo,
				      success: (res) => {
				        // res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
						if (res.errMsg === 'requestMerchantTransfer:ok') {
							 //展示页面成功
							this.checkJkCashoutState();
						}
						if (res.errMsg === 'requestMerchantTransfer:fail') {
							//展示页面失败
							this.stopInterval()
							this.getWechatMoney();
						}
						if (res.errMsg === 'requestMerchantTransfer:cancel') {
							//用户点击取消
							console.log("用户取消");
							this.stopInterval()
							//this.userCancelMoney();
						}
						
				      },
				      fail: (res) => {
						  console.log(res);
						 if (res.errMsg === 'requestMerchantTransfer:fail:internal error') {
						 	//用户点击取消
						 	this.stopInterval()
						 	//this.userCancelMoney();
						 }
				      }
				    });
				}
			},
			  userCancelMoney(){
				  //用户取消提现 outBatchNo:this.outBatchNo,
				  this.$http('jkCashout/failJkCashout', {
					 jkcashid:this.jkcashid
				  }, 'POST').then(res => {
					
				 })
			  }
		},
		onReachBottom(){
			//this.page++;
			if(this.type==2){
				this.getListByJkCashout()
			}else{
				this.getList()
			}
			//console.log("shanglas")
		},
		onPullDownRefresh(){
		    //this.page=1;
			//this.list=[];
			if(this.type==2){
				this.getListByJkCashout()
			}else{
				this.getList()
			}
			//this.getList();
			setTimeout(() => {
					uni.stopPullDownRefresh(); // 关闭下拉刷新 
			}, 2000);
		}
		
	}
</script>

<style scoped lang="less">
.box {
	width: 100vw;
	min-height: 100vh;
	background: #F4F5F7;
}
.nickNameBox{
	display: flex;
	
}
.nickName{
	font-weight: 400;
	color: #999999;
	font-size: 24rpx;
	line-height: 30rpx;
	padding-left: 10rpx;
}
.lineContLeftImg{
	width: 30rpx;
	height: 30rpx;
	margin-right: 10rpx;
}
.line{
	width: 702rpx;
	background: #FFFFFF;
	border-radius: 10rpx;
	margin-bottom: 20rpx;
	margin-left: 24rpx;
}
.linetop{
	display: flex;
	justify-content: space-between;
	font-size: 30rpx;
	padding: 20rpx;
	padding-bottom: 0;
}
.lineline{
	display: flex;
	justify-content: space-between;
	padding: 20rpx;
	font-weight: 400;
	color: #999999;
	font-size: 24rpx;
	line-height: 30rpx;
}
.lineContLeft{
	display: flex;
	
}
.lineTIme{
	font-size: 28rpx;
	padding-right: 10rpx;
}
.name{
	font-weight: 500;
	color: #999999;
	width: 300rpx;
}
.resultTitle{
	position: absolute;
	color: darkorange;
	left: 320rpx;
	font-size: 28rpx;
}
.amount{
	color: #EC0F0A;
}
.nickNameImg{
	width: 30rpx;
	height: 30rpx;
}
.jkButton{
	color: black;
	font-size: 28rpx;
	border: 1rpx solid #999999;
	background-color: #ffffff;
	width: 120rpx;
	height: 40rpx;
	text-align: center;
	line-height: 40rpx;
	border-radius: 10rpx;
}
</style>
