<template>
	<view class="container">
		<view class="top">
			<view class="topTitle">可提现金额</view>
			<view class="inputs one">
				¥{{mymoney}}
			</view>
		</view>
		<view class="top">
			<view class="topTitle">提现金额<span class="bangding" @click="bangding" />
				<view class="zuidimoney">{{min}}{{value}}元
				</view>
				<!-- <image class="icon"
						src="https://h5.canmoujiang.com/img/20210714/bddef556a80b46c187b498e1419e267e.png"
						mode="widthFix"></image>绑定支付宝 -->
			</view>
			<view class="inputs">
				<span style="margin-right: 10rpx;">¥</span><input class="chongzhi" type="number" v-model="money"
					maxlength="10" placeholder="请输入提现金额" />
			</view>
			<view class="ktxje">可提现金额{{mymoney}}元。<span @click="tixian" v-if="mymoney != 0"> 全部提现</span></view>
			<view style="color: red;font-size: 24rpx;" v-if="type == 2">*注微信单笔限制最大提现额度为{{WxMaxMoney}}元</view>
		</view>
		<view class="duihuan_view" v-if="wxCash != '否'">
			<view style="padding: 5rpx 10rpx;">兑换方式 <text style="font-size: 24rpx;color: red;margin-left: 10rpx;"
					v-if="shouxufei != 0 && shouxufei">（提现手续费为{{shouxufei * 100}}%）</text></view>
			<view class="duihuan_item">
				<view class="item_view" @tap="typeSelect(2)" :class="type == 2 ? 'item_view_sel' : 'item_view'">
					<image src="../../static/img/share/icon_weixin.png"></image>
					<view class="name">微信</view>
				</view>
				<view class="item_view" @tap="typeSelect(1)" :class="type == 1 ? 'item_view_sel' : 'item_view'">
					<image src="https://download.cyjyyjy.com/zhifubao.png"></image>
					<view class="name">支付宝</view>
				</view>
			</view>
		</view>
		<button class="btnCz" @click="getOut">立即提现</button>
		<view style="display: flex;width: 100%;justify-content: center;text-align: center;margin-top: 40rpx;">
			<view style="color: grey;flex:1" @click="bangding">提现账号</view>
			<view style="color: grey;flex:1" @click="goDetail">提现记录</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wxCash: '否',
				type: 2,
				WxMaxMoney: 0,
				money: '',
				mymoney: 0,
				zhifubao: '',
				zhifubaoName: '',
				shouxufei: 0,
				value: 0,
				min: '',
				arr: [],
				showModal: true
			};
		},
		onLoad() {
			let that = this;
			that.$Request.getT('/common/type/210').then(res => { //订单审核提醒
				if (res.code == 0) {
					if (res.data && res.data.value) {
						// that.orderStart = res.data;
						that.arr.push(res.data.value)
					}
				}
			})

			// 提现费率
			that.$Request.getT('/common/type/114').then(res => {
				if (res.code == 0) {
					if (res.data && res.data.value) {
						that.shouxufei = res.data.value;
					}
				}
			});
		},
		onShow() {
			this.WxMaxMoney = this.$queue.getData('WxMaxMoney');
			this.wxCash = this.$queue.getData('wxCashSelect');
			let userId = this.$queue.getData('userId');
			if (userId) {
				this.getUserInfointegral(userId);
				this.getUserInfo(userId);
				this.zhifubao = this.$queue.getData("zhifubao");
				this.zhifubaoName = this.$queue.getData("zhifubaoName");
				//订阅
				if (this.showModal) {
					this.openMsg()
				}
			}
			this.getasdas();
		},
		methods: {
			// 开启订阅消息
			openMsg() {
				var that = this
				wx.getSetting({
					withSubscriptions: true, //是否获取用户订阅消息的订阅状态，默认false不返回
					success(ret) {
						// console.log(ret,'------------------')
						// console.log(Object.keys(ret.subscriptionsSetting.itemSettings).length)
						// if (JSON.stringify(ret.subscriptionsSetting.itemSettings).indexOf('accept')!=-1) {
						if (ret.subscriptionsSetting.itemSettings) {
							uni.setStorageSync('sendjifenMsg', true)
							uni.openSetting({ // 打开设置页 
								success(rea) {
									console.log(rea.authSetting)
								}
							});
						} else { // 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
							uni.setStorageSync('sendjifenMsg', false)
							uni.showModal({
								title: '提示',
								content: '为了更好的体验,请绑定消息推送',
								confirmText: '确定',
								cancelText: '取消',
								success: function(res) {
									if (res.confirm) {
										uni.requestSubscribeMessage({
											tmplIds: that.arr,
											success(re) {
												// console.log(re,'**********')
												var datas = JSON.stringify(re);
												if (datas.indexOf("accept") != -1) {
													console.log(re)
												}
											},
											fail: (res) => {
												console.log(res)
											}
										})
										that.showModal = false
									} else if (res.cancel) {
										that.showModal = true
									}
								}
							})
						}
					}
				})
			},
			typeSelect(type) {
				if (uni.getStorageSync('sendjifenMsg')) {
					uni.requestSubscribeMessage({
						tmplIds: this.arr,
						success(re) {
							// console.log(re,'**********')
							var datas = JSON.stringify(re);
							if (datas.indexOf("accept") != -1) {
								console.log(re)
							}
						},
						fail: (res) => {
							console.log(res)
						}
					})
				}
				this.type = type;
			},
			goDetail() {
				uni.navigateTo({
					url: '/pages/my/cashList'
				});
			},
			getUserInfo(userId) {
				this.$Request.postT("/app/selectUserById?userId=" + userId).then(res => {
					if (res.code === 0) {
						this.$queue.setData("avatar", res.data.imageUrl ? res.data.imageUrl :
							'/static/img/logo.jpg');
						this.$queue.setData("nickName", res.data.nickName ? res.data.nickName : res.data.phone);
						this.$queue.setData("mobile", res.data.phone);
						this.$queue.setData("invitationCode", res.data.invitationCode);
						this.$queue.setData("relation_id", res.data.relationId);
						this.$queue.setData("relation", res.data.invitationCode);
						this.$queue.setData("grade", res.data.grade);
						this.$queue.setData("wxId", res.data.wxId);
						this.$queue.setData("zhifubao", res.data.zhifubao);
						this.$queue.setData("zhifubaoName", res.data.zhifubaoName);
						this.$queue.setData("isInvitation", res.data.isInvitation);
						this.$queue.setData("gender", parseInt(res.data.gender));
						this.$queue.setData("sex", res.data.sex);
						this.zhifubao = this.$queue.getData("zhifubao");
						this.zhifubaoName = this.$queue.getData("zhifubaoName");
					} else {
						this.$queue.logout();
						uni.navigateTo({
							url: '/pages/public/login'
						});

					}
					uni.hideLoading();
				});
			},
			getasdas() {
				this.$Request.getT('/common/type/145').then(res => {
					if (res.code == 0) {
						this.min = res.data.min;
						this.value = res.data.value;
					}
				});
			},
			//获取用户积分信息
			getUserInfointegral(userId) {
				this.$Request.getT('/goodsMerchant/selectMerchantMoney?userId=' + userId).then(res => {
					if (res.code === 0) {
						this.mymoney = res.data.mayMoney ? res.data.mayMoney : 0;
					}
				});
			},
			// 前往绑定
			bangding() {
				uni.navigateTo({
					url: '/pages/member/zhifubao'
				});
			},
			// 全部提现
			tixian() {
				this.money = this.mymoney
			},
			//校验用户输入金额
			checkMobile(money) {
				return RegExp(/^1[34578]\d{9}$/).test(money);
			},
			numFilter(value) {
				// 截取当前数据到小数点后三位
				let tempVal = parseFloat(value).toFixed(3)
				let realVal = tempVal.substring(0, tempVal.length - 1)
				return realVal
			},
			getOut() {
				let that = this;
				let token = this.$queue.getData("token");
				let userId = this.$queue.getData("userId");
				let cashMoney = this.$queue.getData("cashMoney");
				if (token) {
					if (uni.getStorageSync('sendjifenMsg')) {
						uni.requestSubscribeMessage({
							tmplIds: this.arr,
							success(re) {
								// console.log(re,'**********')
								var datas = JSON.stringify(re);
								if (datas.indexOf("accept") != -1) {
									console.log(re)
								}
							},
							fail: (res) => {
								console.log(res)
							}
						})
					}
					if (that.zhifubao && that.zhifubaoName) {
						if (!/^\d+$/.test(this.money)) {
							uni.showToast({
								icon: 'none',
								title: '请输入正确金额，纯数字不能包含特殊字符和小数'
							});
							return;
						}
						if (parseFloat(this.mymoney).toFixed(1) >= parseFloat(this.money)) {
							if (parseInt(this.value) <= parseInt(this.money)) {
								if (this.type == 1) {
									let contentName = '请仔细确认收款人信息\n\n姓名:' + that.zhifubaoName + '\t\t金额:' + this.money +
										'元\n\n收款账号：' + that.zhifubao + '';
									if (this.shouxufei != 0 && this.shouxufei) {
										contentName = '请仔细确认收款人信息\n\n姓名:' + that.zhifubaoName + '\t\t金额:' + this.money +
											'元\n\n收款账号：' + that.zhifubao + '\n\n手续费:' + this.numFilter(parseFloat(this.money) *
												parseFloat(this.shouxufei))+ '元';
									}
									uni.showModal({
										title: '提现申请提示',
										content: contentName,
										success: e => {
											if (e.confirm) {
												this.$queue.showLoading('提现中...');
												this.$Request.postT('/userMoney/cashMoney?userId=' +
														userId +
														'&money=' + this.money + '&type=' + this.type)
													.then(
														res => {
															if (res.code === 0) {
																that.$queue.showToast('提现申请成功，预计三个工作日到账');
																that.getUserInfointegral(userId);
															} else {
																uni.showModal({
																	title: '温馨提示',
																	content: res.msg,
																	showCancel: false,
																	cancelText: '取消',
																	confirmText: '确认'
																});
															}
															uni.hideLoading();
														});
											}
										}
									});
								} else {
									if (parseInt(this.money) <= parseInt(this.WxMaxMoney)) {

										let contentName = '请仔细确认收款信息\n\n金额:' + this.money + '元';
										if (this.shouxufei != 0 && this.shouxufei) {
											contentName = '请仔细确认收款信息\n\n金额:' + this.money + '元\n\n手续费:' + this.numFilter(
												parseFloat(this.money) * parseFloat(this.shouxufei)) + '元';
										}
										uni.showModal({
											title: '提现申请提示',
											content: contentName,
											success: e => {
												if (e.confirm) {
													this.$queue.showLoading('提现中...');
													this.$Request.postT('/userMoney/cashMoney?userId=' +
															userId +
															'&money=' + this.money + '&type=' + this.type)
														.then(
															res => {
																if (res.code === 0) {
																	that.$queue.showToast('提现申请成功，预计三个工作日到账');
																	that.getUserInfointegral(userId);
																} else {
																	uni.showModal({
																		title: '温馨提示',
																		content: res.msg,
																		showCancel: false,
																		cancelText: '取消',
																		confirmText: '确认'
																	});
																}
																uni.hideLoading();
															});
												}
											}
										});
									} else {
										this.$queue.showToast('微信单次最大提现兑换额度为' + this.WxMaxMoney);
									}
								}
							} else {
								this.$queue.showToast('最低可提现金额为' + this.value + '元');
							}
						} else {
							this.$queue.showToast("您的余额不足");
						}
					} else {
						uni.navigateTo({
							url: "/pages/member/zhifubao"
						})
					}
				} else {
					uni.navigateTo({
						url: '/pages/public/login'
					});
				}
			}
		}
	}
</script>

<style lang='scss'>
	page {
		/* background: #fff; */
	}

	.duihuan_view {
		width: 100%;
		height: max-content;
		background: #FFFFFF;
		margin-top: 20rpx;

		.duihuan_item {
			display: flex;
			width: 100%;
			margin-top: 20rpx;
			justify-content: space-between;

			.item_view {
				background: #FFFFFF;
				display: flex;
				justify-content: center;
				width: 49%;
				align-items: center;
				border: #999 1upx solid;
				padding: 15rpx 0;
				border-radius: 15rpx;

				.name {
					font-size: 28rpx;
					color: #000000;
					margin-left: 15rpx;
				}

				image {
					width: 50rpx;
					height: 50rpx;
					border-radius: 50rpx;
				}
			}

			.item_view_sel {
				background: #FFFFFF;
				display: flex;
				justify-content: center;
				width: 49%;
				align-items: center;
				border: #FF4701 4upx solid;
				padding: 15rpx 0;
				border-radius: 15rpx;

				.name {
					font-size: 28rpx;
					color: #000000;
					margin-left: 15rpx;
				}

				image {
					width: 50rpx;
					height: 50rpx;
					border-radius: 50rpx;
				}
			}
		}
	}

	.zuidimoney {
		font-size: 24rpx;
		color: #333333;
		margin-left: 20rpx;
		margin-top: 5rpx;
	}

	.container {
		padding: 0 4%;

		.top {
			background: #fff;
			margin: 30rpx 0;
			border-radius: 20rpx;
			padding: 20rpx;
			position: relative;

			.topTitle {
				display: flex;
				font-size: 30rpx;
				line-height: 60rpx;

				.bangding {
					position: absolute;
					right: 30rpx;
					font-size: 24rpx;
					color: #666;

					.icon {
						width: 30rpx;
					}
				}
			}

			.inputs {
				display: flex;
				margin: 10rpx 0;
				font-size: 44rpx;
				color: #333;
				height: 70rpx;
				align-items: center;
				font-weight: bold;
				border-bottom: 1rpx solid #E6E6E6;
				/* position: relative; */

				.chongzhi {
					width: 80%;
					height: 70rpx;
				}
			}

			.one {
				border-bottom: none;

				.yuan {
					font-size: 28rpx;
				}

				.kyje {
					position: absolute;
					bottom: 0;
					right: 0;
					font-size: 24rpx;
					color: #666;

					.je {
						color: #FF4701;
					}
				}
			}

			.ktxje {
				span {
					color: #FF4701;
				}
			}
		}

		.btnCz {
			margin-top: 50rpx;
			border: none;
			background: #FF4701;
			color: #fff;

		}
	}
</style>
