<template>
	<view class="content">
		<view class="myBalance">
			<view class="myBalanceTopModel">
				<view class="myBalanceNum">
					{{userInfo.total_revenue}}
				</view>
				<view class="myBalanceTitle">
					总金额(元)
				</view>
			</view>
		</view>
		<view class="withdrawalModel">
			<view class="withdrawalTitle">
				提现金额<span class="withdrawalTitleTipicon">*</span><span class="withdrawalTitleTip"> 起提金额1元起，服务费为1%
				</span>
			</view>
			<view class="withdrawalInitModel">
				<u--input placeholder="请输入提现金额" v-model="money" border="none"></u--input>
			</view>
			<view class="withdrawalTitle" v-if="openid == ''">
				提现到微信零钱
			</view>
			<view class="withdrawalInitModel" v-if="openid == ''">
				<view class="wxIcon" v-show="openid == ''">
					<image class="init_image" src="http://amy.yyinhong.cn/uploads/wximg/wxIcon.png" mode=""></image>
				</view>
				<view class="wxText" v-show="openid == ''">
					微信零钱
				</view>
				<view class="winxinApply" @click="gotosq" v-show="openid == ''">
					微信授权
				</view>
			</view>
			<view class="withdrawalTip">
				提现须知
			</view>
			<view class="withdrawalTipInfo">
				<u-parse :content="intro"></u-parse>
			</view>

		</view>
		<view class="applyModel">
			<view class="applyBtn" @click="applyWithdrawal">提交</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				money: null,
				intro: null,
				userInfo: null,
				openid: null,
				globalTimer: null
			}
		},
		onShow() {
			this.wxlogin()
			this.globalTimer = setInterval(() => {
				this.wxlogin()
			}, 299990)
			this.getUserInfo();
		},
		// 离开页面 清除定时器
		onHide() {
			if (this.globalTimer) {
				clearInterval(this.globalTimer)
			}
		},
		methods: {
			wxlogin() {
				uni.login({
					success: function(loginRes) {
						uni.setStorageSync('userCode', loginRes.code);
					}
				})
			},
			//获取用户信息
			getUserInfo() {
				this.$request('/merchant-info', 'GET', {}).then(res => {
					console.log('请求成功')
					this.userInfo = res.data;
					this.openid = res.data.openid;
					this.getTip()
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			},
			gotosq() {
				this.$request('/merchant-login', 'POST', {
					code: uni.getStorageSync('userCode')
				}).then(res => {
					uni.showToast({
						icon: 'success',
						title: '授权成功！'
					})
					this.getUserInfo();
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			},
			//申请提现
			applyWithdrawal() {
				if (this.openid == '') {
					uni.showToast({
						icon: 'error',
						title: '请先微信授权！'
					})
					return
				}
				if (this.money == null || this.money == '') {
					uni.showToast({
						icon: 'error',
						title: '请输入提现金额！'
					})
					return
				}
				this.$request('/income-add', 'POST', {
					type: '2',
					money: '' + this.money
				}).then(res => {
					console.log(res.data)
					this.money = null
					uni.showToast({
						icon: 'success',
						title: '提现申请，已提交！'
					})
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})

			},
			//获取体现须知
			getTip() {
				this.$request('/cashout-intro', 'GET', {}).then(res => {
					console.log('请求成功');
					this.intro = res.data.intro;
				}).catch(err => {
					uni.showToast({
						icon: 'error',
						title: '' + err.message
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100vw;
		height: 100vh;
		background: linear-gradient(to bottom, #f3d4d7, #f8f8f8, #f8f8f8);
	}

	.myBalance {
		width: 100%;
		height: 126rpx;
		padding: 78rpx 0;
		text-align: center;
		display: flex;
		flex-direction: row;
		justify-content: center;

		.myBalanceTopModel {
			width: calc(100% / 3);

			.myBalanceNum {
				line-height: 88rpx;
				font-size: 56rpx;
				color: #D3195E;
				font-weight: 500;
				font-style: normal;
			}

			.myBalanceTitle {
				line-height: 38rpx;
				font-size: 24rpx;
				color: #666666;
			}
		}

	}


	.withdrawalModel {
		width: calc(100% - 60rpx);
		padding: 0 30rpx;
		height: auto;
		overflow-y: scroll;
		padding-top: 40rpx;
		background-color: #ffffff;
		border-radius: 40rpx 40rpx 0 0;

		.withdrawalTitle {
			font-size: 28rpx;
			color: #333333;
			height: 60rpx;
		}

		.withdrawalTitleTip {
			font-size: 24rpx;
		}

		.withdrawalTitleTipicon {
			color: #FF3141;
			font-size: 24rpx;
			margin-left: 10rpx;
		}

		.withdrawalInitModel {
			width: calc(100% - 60rpx);
			padding: 30rpx;
			background-color: #F6F7F8;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			min-height: 60rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
		}

		.wxIcon {
			width: 50rpx;
			height: 50rpx;
		}

		.wxText {
			flex: 1;
			margin-left: 20rpx;
			font-size: 30rpx;
		}

		.winxinApply {
			text-align: center;
			width: 140rpx;
			height: 58rpx;
			font-size: 24rpx;
			background-color: #E9799F;
			color: #ffffff;
			line-height: 58rpx;
			border-radius: 58rpx;
		}

		.withdrawalTip {
			font-size: 32rpx;
			color: #333333;
			height: 60rpx;
			font-weight: 600;
			margin-bottom: 20rpx;
		}

		.withdrawalTipInfo {
			padding-bottom: 200rpx;
		}
	}
</style>