<template>
	<view class="page" :style="themeColor">
		<view class="flex align-stretch benben-position-layout flex Withdrawals_flex_0"
			:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class='flex align-center justify-between flex-sub Withdrawals_fd0_0'>
				<view class='flex align-center Withdrawals_fd0_0_c0' @tap.stop="handleJumpDiy" data-type="back"
					data-url="1">
					<text class='fu-iconfont2  Withdrawals_fd0_0_c0_c0'>&#xE794;</text>
				</view>
				<view class='flex align-center'>
					<text class='Withdrawals_fd0_0_c1_c0'>提现</text>
				</view>
				<view class='flex align-center justify-end Withdrawals_fd0_0_c2' @tap.stop="handleJumpDiy"
					data-type="navigateTo" :data-url="`/pages/md/withdrawalDetail/withdrawalDetail`">
					<text class='Withdrawals_fd0_0_c2_c0'>明细</text>
				</view>
			</view>

		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>

		<view class="flex flex-direction align-stretch benben-flex-layout Withdrawals_flex_1">
			<text class='Withdrawals_fd1_0'>输入提现金额</text>
			<view class='flex flex-wrap align-center justify-between Withdrawals_fd1_1'>
				<view class='flex flex-wrap align-end flex-sub Withdrawals_fd1_1_c0'>
					<text class='Withdrawals_fd1_1_c0_c0'>￥</text>
					<input class='flex-sub Withdrawals_fd1_1_c0_c1' type="text" :placeholder="'账户余额'+moeny"
						confirm-type="done" @input="moneychange" :maxlength="maxLength"  placeholder-style="color:#999;font-size:32rpx"
						v-model="fd1_1_c0_c1" />
				</view>
				<text class='Withdrawals_fd1_1_c1' @click="total_tx">全部</text>
			</view>
			<view class='flex flex-wrap align-center Withdrawals_fd1_2' @tap="closePop">
				<text class='Withdrawals_fd1_2_c0'>提现收取</text>
				<text class='Withdrawals_fd1_2_c1'>{{withdraw_handling_fee}}%</text>
				<text class='Withdrawals_fd1_2_c2'>的手续费</text>
				<text class='fu-iconfont2  Withdrawals_fd1_2_c3'>&#xE600;</text>
			</view>
		</view>


		<view class="flex align-start justify-center benben-position-layout flex Withdrawals_flex_2">
			<button class='Withdrawals_fd2_0' @tap.stop="handleSubmit()">提现</button>

		</view>
		<view :style="{height: '120rpx'}"></view>

		<fu-popup v-model="showPopup" mode="center" width="540rpx" height="600rpx">
		  <view class="pop-content padding">
		    <view class="text-333 text-bold text-lg text-center">提现规则</view>
				<scroll-view scroll-y="true" style="height: 350rpx;">
					<view class="margin-top-sm text-999">
						<view class="" v-html="fast_detaile"></view>
					</view>
				</scroll-view>
		    
		  </view>
		  <view class="padding-top flex align-center justify-center " @tap="closePop">
		    <view class="round close-btn flex align-center justify-center">
					<image style="width: 56rpx;height: 56rpx;" :src='STATIC_URL+"17.png"'></image>
				</view>
		  </view>
		</fu-popup>
		
	</view>
</template>
<script>
	import {
		validate
	} from '@/common/utils/validate.js'
	export default {
		components: {},


		data() {
			return {
				"fd1_1_c0_c1": "",
				"type_id": "1",
				"type": [{
					"name": "支付宝",
					"value": "1",
					"image": "https://zhongben-crm.oss-cn-zhangjiakou.aliyuncs.com/uploads/images/20220110/348e8b49a890ed0b954d360573973023.png"
				}, {
					"name": "微信",
					"value": "2",
					"image": "https://zhongben-crm.oss-cn-zhangjiakou.aliyuncs.com/uploads/images/20211009/a665822a57096420848dc12457b53b39.png"
				}],
				"amount": "",
				withdraw_handling_fee:'', //提现手续费
				CustomBar: this.CustomBar,
				tabCurrentIndex: 2, // {Number} 支付宝还是微信
				moeny: '', // {String} 账户余额
				account_name: '', // {Number} 没啥用，不敢删
				account: '', // {String} 没啥用，不敢删
				change_money: '', // {String} 用户输入的提现金额
				is_click: false, // {Boolean} 按钮的点击状态
				show_key: false, // {Boolean} 支付密码组件是否显示
				min: 1, // {Number} 最小提现金额
				showPopup: false, // {Boolean} 弹窗显示
				fast_detaile: [] ,// {Object}
				lists:[],
				account_id:'',
				is_ye:false  ,//是否绑定银行卡
				is_wx:false  ,//是否绑定微信
				is_ali:false  ,//是否绑定支付宝
				maxLength:""
			};
		},
		computed: {
			themeColor() {
				return this.$store.getters.themeColor
			},

		},
		watch: {},
		onLoad(options) {
			this.getMoney();
			this.getLimitWithdraw();
			this.showRole();
		},
		onShow() {
			this.getWithdrawAccount();
		},
		methods: {
			getWithdrawAccount() {
			  this.$api.get(global.apiUrls.getWithdrawAccount,{
					account_type:3
				})
				.then(res => {
					res = res.data;
					if (res.code == 1) {
						this.is_ye=true
						this.tabCurrentIndex=3
					}else{
						this.is_ye=false
					}
				})
				.catch(err => {
					console.log(err, 'get_withdraw_account');
				});
			},
			
			/**
			 * @function
			 * @description 支付密码弹窗隐藏
			 */
			hideFun() {
			  this.show_key = false;
			},
			/**
			 * @function
			 * @description 判断用户是否设置支付密码
			 * @property {Number} pay_money - 订单金额
			 */
			isSetPayPassword() {
			  return new Promise(resolve => {
			    this.$api.post(global.apiUrls.isSetPay).then(res => {
			      res = res.data;
			      console.log('是否设置支付密码', res);
			      if (res.code == 1) {
			        if (res.data.falg == 0) {
			          setTimeout(() => {
			            this.$urouter.navigateTo('/pages/index/setting/change-pay-password/index');
			          }, 800);
			          resolve(false);
			        } else {
			          resolve(true);
			        }
			      } else {
			        this.$message.info(res.msg);
			        resolve(false);
			      }
			    });
			  });
			},
			filter(type) {
			  switch (Number(type)) {
			    case 1:
			      return this.i18n['请先绑定微信'];
			    case 2:
			      return this.i18n['请先绑定支付宝'];
			    case 3:
			      return this.i18n['请先绑定银行卡'];
			  }
			},
			// 判断用户是否绑定了提现账户
			isBindAccount() {
			  return new Promise(resolve => {
			    this.$api
			      .get(global.apiUrls.getWithdrawAccount, {
			        account_type: this.tabCurrentIndex
			      })
			      .then(res => {
			        res = res.data;
			        console.log('是否绑定了提现账号', res);
			        if (res.code == 0) {
			          let index = this.tabCurrentIndex
			          console.log('未绑定',this.filter(1));
			          // this.$message.info(this.filter(this.tabCurrentIndex));
			          let urls = '/pages/md/bindBankCard/bindBankCard';
			          // if (this.tabCurrentIndex == 1) {
			          //   urls = '/pages/user/wallet/wechat-bind/index';
			          // }
			          // if (this.tabCurrentIndex == 2) {
			          //   urls = '/pages/user/wallet/alipay-bind/index';
			          // }
			          // if (this.tabCurrentIndex == 3) {
			          //   urls = '/pages/user/wallet/card-bind/index';
			          // }
			          setTimeout(() => {
			            this.$urouter.navigateTo({
			              url: urls,
			              params: {
			                id: 1
			              }
			            });
			          }, 800);
			          resolve(false);
			        } else {
			          resolve(true);
			        }
			      });
			  });
			},
			/**
			 * @event
			 * @description 判断用户是否绑定体现账户,如果绑定就去体现
			 * @param {Object} n - 用户输入的支付密码
			 * @property {Number} account_type - 【账户类型 1 微信 2 支付宝 3 银行卡】
			 * @property {Number} type - 【提现类型 1：微信 2：支付宝】
			 * @property {Number} money - 【提现金额】
			 * @property {Number} pay_password - 支付密码
			 */
			getPassword(n) {
			  this.$api
			    .post(global.apiUrls.withdraw, {
			      type: this.tabCurrentIndex,
			      money: this.change_money,
			      pay_password: n.password
			    })
			    .then(res => {
			      res = res.data;
			      if (res.code == 1) {
			        uni.$emit('changeMoney', {
			          msg: this.i18n['页面更新']
			        });
			        this.$urouter.redirectTo({
			          url: '/pages/user/wallet/withdraw-success/index',
			          params: {
			            type: this.tabCurrentIndex,
			            money: this.change_money
			          }
			        });
			      } else {
			        this.$message.info(res.msg);
			      }
			    })
			    .catch(err => {
			      console.log('ERROR', err);
			    });
			},
			filterName(type) {
			  switch (type) {
			    case 1:
			      return this.i18n['微信'];
			    case 2:
			      return this.i18n['支付宝'];
			    case 3:
			      return this.i18n['银行卡'];
			  }
			},
			/**
			 * @function
			 * @description 开启支付密码组件
			 */
			showModal() {
			  this.show_key = true;
			},
			/**
			 * @event
			 * @description 全部提现
			 */
			total_tx() {
			  if (parseFloat(this.moeny) <= 0) {
			    this.$message.info('没有可提现的金额');
			  } else if (parseFloat(this.moeny) < parseFloat(this.min)) {
			    this.$message.info('当前余额低于最低提现金额限制');
			  } else {
			    this.fd1_1_c0_c1 = this.moeny;
			  }
			},
			moneychange(e){
				let that = this;
				let price = e.detail.value
				let maxLength = price.indexOf('.');
				if (price.indexOf(".") < 0 && price != "") {
					//'超过4位则大于1万元'
					if (price.length > 8) {
						price = price.substring(0, price.length - 1)
					} else {
						price = parseFloat(price);
					}
				} else if (price.indexOf(".") == 0) {
					//'首位小数点情况'
					price = price.replace(/[^$#$]/g, "0.");
					price = price.replace(/\.{2,}/g, ".");
				} else if (!(/^(\d?)+(\.\d{0,2})?$/.test(price))) {
					//去掉最后一位
					price = price.substring(0, price.length - 1)
				}
				that.$nextTick(function() {
					//'有小数点时，最大长度为7位，没有则是5位'
					that.maxLength = maxLength == -1 ? 7 : 5
					that.fd1_1_c0_c1 = price
				})
			},
			/**
			 * @event
			 * @description 用户选择提现到支付宝还是微信
			 */
			get_type(e) {
				// this.account_id = e
				console.log(e)
			  this.tabCurrentIndex = e
			},
			/**
			 * @function
			 * @description 获取用户余额
			 */
			getMoney() {
			  let _this = this;
			  _this.$api
			    .get(global.apiUrls.getMyCashMoney, {})
			    .then(res => {
			      console.log(res, 'get_my_money');
			      if (res.data.code == 1) {
			        _this.moeny = res.data.data.user_money;
			        // _this.withdraw_handling_fee = res.data.data.withdraw_handling_fee
			      }
			    })
			    .catch(err => {
			      console.log(err, 'get_my_money');
			    });
			},
			/**
			 * @function
			 * @description 获取最小提现金额限制
			 */
			getLimitWithdraw() {
			  this.$api
			    .post(global.apiUrls.postLimitWithdraw)
			    .then(res => {
			      if (res.data.code == 1) {
					  this.withdraw_handling_fee = res.data.data.withdraw_fee
			        this.min = res.data.data.min_money;
			      }
			    })
			    .catch(err => {});
			},
			/**
			 * @event
			 * @description 点击立即体现按钮
			 * 先判断是否绑定了对应账号
			 * 再判断是否设置了支付密码
			 * 再提现
			 */
			async handleSubmit() {
			  let isBindAccount = await this.isBindAccount();
			  if (!isBindAccount) return;
			  console.log('绑定了账号,下一步');
			  // let isSetPayPassword = await this.isSetPayPassword();
			  // if (!isSetPayPassword) return;
			  console.log('设置了支付密码,下一步');
			  if (!this.fd1_1_c0_c1) {
			    this.$message.info('请输入提现金额');
			    return false;
			  }
			  if (Number(this.fd1_1_c0_c1) < Number(this.min)) {
			    this.$message.info('最小提现金额为' + this.min + '元');
			    return false;
			  }
			  if (Number(this.fd1_1_c0_c1) > Number(this.moeny)) {
			    this.$message.info('可提现余额不足');
			    return false;
			  }
			  // uni.redirectTo({
			  // 	url:"/pages/md/application/application"
			  // })
			  this.$api.post(global.apiUrls.withdraw,{
				  cash_fee:this.fd1_1_c0_c1,
				  account_type:3
			  }).then(res=>{
				  if(res.data.code==1){
					  this.$urouter.redirectTo({
					    url: '/pages/md/application/application',
					    params: {
					      type: 3,
					      money: this.fd1_1_c0_c1
					    }
					  });
				  }else{
						this.$message.info(res.data.msg);
					}
			  })
			  
			  // this.showModal();
			},
			
			
			
			
			showRole() {
			  let _this = this;
			  _this.$api.post(global.apiUrls.Get636c78ae2216b,{aid:14})
			    .then(res => {
			      if (res.data.code == 1) {
			        _this.fast_detaile = res.data.data.body;
			        // this.showPopup = true;
			      }
			    })
			    .catch(err => {
			      console.log(err, 'get_my_money');
			    });
			},
			closePop() {
			  this.showPopup = !this.showPopup;
			},
		}
	};
</script>
<style lang="scss" scoped>
	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: #F8F8F8;
		background-size: 100% auto !important;
	}
	// 规则弹窗 start
	.pop-content {
	  width: 540rpx;
	  height: 472rpx;
	  border-radius: 24rpx;
	  background: #ffffff;
	}
	
	.close-btn {
	  width: 54rpx;
	  height: 54rpx;
	  background-color: rgba(255, 255, 255, 0.6);
	}
	
	// 规则弹窗 end

	.Withdrawals_flex_0 {
		border-bottom: 1px solid #eee;
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.Withdrawals_fd0_0_c2_c0 {
		font-size: 32rpx;
		font-weight: 400;
		color: #333;
	}

	.Withdrawals_fd0_0_c2 {
		width: 180rpx;
	}

	.Withdrawals_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 500;
		color: #333;
	}

	.Withdrawals_fd0_0_c0_c0 {
		font-size: 32rpx;
		font-weight: 500;
		color: #333;
	}

	.Withdrawals_fd0_0_c0 {
		width: 180rpx;
	}

	.Withdrawals_fd0_0 {
		padding: 0rpx 32rpx 0rpx 32rpx;
		line-height: 88rpx;
	}

	.Withdrawals_flex_1 {
		border-top: 10px solid #F8F8F8;
		border-bottom: 10px solid #F8F8F8;
		background: #fff;
		background-size: 100% auto !important;
		padding: 32rpx 32rpx 32rpx 32rpx;
	}

	.Withdrawals_fd1_2_c3 {
		margin: 0rpx 0rpx 0rpx 16rpx;
		color: rgba(191, 191, 191, 1);
		font-size: 26rpx;
	}

	.Withdrawals_fd1_2_c2 {
		line-height: 53rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
	}

	.Withdrawals_fd1_2_c1 {
		line-height: 53rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
	}

	.Withdrawals_fd1_2_c0 {
		line-height: 53rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
	}

	.Withdrawals_fd1_2 {
		margin: 24rpx 0rpx 24rpx 0rpx;
	}

	.Withdrawals_fd1_1_c1 {
		font-size: 32rpx;
		font-weight: 500;
		color: rgba(237, 64, 64, 1);
	}

	.Withdrawals_fd1_1_c0_c1 {
		padding: 0rpx 0rpx 0rpx 16rpx;
		font-size: 32rpx;
	}

	.Withdrawals_fd1_1_c0_c0 {
		font-size: 56rpx;
		font-weight: 400;
		color: #333333;
	}

	.Withdrawals_fd1_1_c0 {
		padding: 32rpx 0rpx 32rpx 0rpx;
	}

	.Withdrawals_fd1_1 {
		border-bottom: 1px solid #eee;
	}

	.Withdrawals_fd1_0 {
		font-size: 32rpx;
		font-weight: 700;
		color: #333333;
	}

	.Withdrawals_flex_2 {
		background: #fff;
		width: 750rpx;
		height: 120rpx;
		overflow: hidden;
		z-index: 10;
		bottom: calc(0rpx + var(--window-bottom));
		background-size: 100% auto !important;
	}

	.Withdrawals_fd2_0 {
		background: rgba(255, 147, 0, 1);
		border-radius: 40rpx 40rpx 40rpx 40rpx;
		font-size: 32rpx;
		color: #fff;
		width: 702rpx;
		height: 88rpx;
		line-height: 88rpx;
		font-weight: 700;
	}
</style>
