<template>
	<view style="background: #F5F5F5;">
		<!-- <view class="bg" style="margin: 12upx;border-radius: 16upx;position: absolute;width: 96%;height: 380upx"></view> -->
		<view style="color: #FFFFFF;position: relative;width: 100%;height: 200upx;display: block;">
			<view class="user-box">
				<view class="user-info-box">
					<view class="avatar" @tap="checkLogin()">
						<image :src="avatar"></image>
					</view>
					<view class="user-info">
						<view class="nick-wrap">
							<text class="nick">{{ nickName }}</text>
						</view>
						<text class="code" v-if="zhifubaoName === ''">支付宝未实名</text>
					</view>
					<view class="user-info">
						<view class="nick-wrap" style="widh:100%;text-align: right;display: block">
							<view class="nick">{{total}}</view>
						</view>
						<text class="code"
							style="widh:100%;text-align: right;display: block;line-height: 50upx;">可用积分</text>
					</view>
				</view>
			</view>
		</view>
		<view style="padding: 32upx;background: #fff;border-radius: 30upx 30upx 0 0;">
			<view>兑换金额</view>
			<view class="main"
				style="position: relative;padding: 20upx 0;border-bottom: 1px solid #ccc;align-items: center;">
				<view style="position: absolute;font-weight: 600;font-size: 30upx;">¥</view>
				<input style="margin-left: 40upx;width: 60%;position: absolute;" v-model="jifen" type="number"
					maxlength="11" placeholder="请输入要兑换的积分" />
				<view style="margin-left: 65%;text-align: right;" @click="dhBtn">全部兑换</view>
				<!-- <wInput v-model="jifen" type="number" maxlength="11" placeholder="请输入要兑换的积分"></wInput> -->
			</view>
			<view style="color: red;font-size: 24rpx;" v-if="type == 2">*注微信单笔限制最大兑换额度为{{WxMaxMoney}}积分</view>

			<view class="duihuan_view" v-if="wxCash != '否'">
				<view>兑换方式</view>
				<view class="duihuan_item">
					<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 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>
			</view>


			<button style="background: #ffc705;color: #FFFFFF;border-radius: 16rpx;margin-top: 47rpx;"
				@click="duihuan">立即兑换</button>
			<!-- <wButton text="立即兑换" :rotate="isRotate" @click.native="duihuan()"></wButton> -->
			<view class="duihuan_title">积分兑换规则</view>
			<view style="margin-top: 30upx;font-size: 26upx;color: #999;">1.{{bili}}积分=1元，最低可提现积分为{{zuidi}}积分</view>
			<view style="margin-top: 30upx;font-size: 26upx;color: #999;">2.申请兑换后3-7个工作日内到账</view>
			<view style="margin-top: 30upx;font-size: 26upx;color: #999;">3.仅支持整数兑换，兑换手续费{{shouxufei * 100}}%</view>
		</view>
		<!-- <button class="btn" :style="type == 2 ? 'background-color: #04BE02;' : 'background-color: #3F64FF;'" @tap="getOut">{{type == 2 ? '微信提现' : '支付宝提现'}}</button> -->
		<view class="footer">
			<view class="bottom_view" @click="goDetail">
				<image src="@/static/img/my/bangding.png"></image>
				<view class="text">兑换记录</view>
			</view>
			<view class="payinfo" style="font-size: 50upx;color: #e6e6e6;width: 2%;">|</view>
			<view class="bottom_view" @click="duihuanjilu">
				<image src="@/static/img/my/duihuan.png"></image>
				<view class="text">积分明细</view>
			</view>
		</view>
	</view>
</template>

<script>
	import wInput from '@/components/watch-login/watch-input.vue'; //input
	import wButton from '@/components/watch-login/watch-button.vue'; //button
	export default {
		data() {
			return {
				contents: [],
				modalName: '',
				fen: 0,
				zuidi: 0,
				wxCash: '否',
				jifen: '',
				isRotate: false, //是否加载旋转
				isLogin: false,
				page: 1,
				bili: 100,
				shouxufei: 0.01,
				percent: 0,
				total: 0,
				type: 1,
				WxMaxMoney: 0,
				phone: '',
				isStudent: 1,
				sex: 0,
				avatar: '',
				nickName: '',
				zhifubao: '',
				zhifubaoName: '',
				arr: [],
				showModal: true
			};
		},
		components: {
			wInput,
			wButton
		},
		onLoad() {
			this.WxMaxMoney = this.$queue.getData('WxMaxMoney');
			this.wxCash = this.$queue.getData('wxCashSelect');
			let avatar = this.$queue.getData('avatar');
			if (avatar && avatar !== 'undefined') {
				this.avatar = avatar;
			} else {
				this.avatar = '/static/img/logo.png';
			}
			let nickName = this.$queue.getData('nickName');
			if (nickName && nickName !== 'undefined') {
				this.nickName = nickName;
			} else {
				this.nickName = '';
			}
			
			let that = this;
			that.$Request.getT('/common/type/203').then(res => { //订单审核提醒
				if (res.code == 0) {
					if (res.data && res.data.value) {
						that.arr.push(res.data.value)
					}
				}
			})
			that.$Request.getT('/common/type/206').then(res => { //积分变动提醒
				if (res.code == 0) {
					if (res.data && res.data.value) {
						that.arr.push(res.data.value)
					}
				}
			})
			
			this.$Request.getT('/common/type/87').then(res => {
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.zuidi = res.data.value;
					}
				}
			})
			this.$Request.getT('/common/type/116').then(res => {
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.bili = res.data.value;
					}
				}
			})
			this.$Request.getT('/common/type/113').then(res => {
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.shouxufei = res.data.value;
					}
				}
			})
			this.$Request.getT('/common/type/102').then(res => {
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.fen = res.data.value;
					}
				}
			})
		},
		onShow() {
			let userId = this.$queue.getData('userId');
			if (userId) {
				this.getUserInfodetail(userId);
				this.getUserInfointegral(userId);
				
				//订阅
				if(this.showModal) {
					// this.openMsg()
				}
			}
		},
		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) {
				this.type = type;
			},
			getUserInfodetail(userId) {
				this.$Request.postT("/app/selectUserById?userId=" + userId).then(res => {
					if (res.code === 0) {
						this.zhifubao = res.data.zhifubao;
						this.zhifubaoName = res.data.zhifubaoName;
						this.phone = res.data.phone;
					}
					uni.hideLoading();
				});
			},
			//获取用户积分信息
			getUserInfointegral(userId) {
				this.$Request.getT('/userMoney/selectUserMoney?userId=' + userId).then(res => {
					if (res.code === 0) {
						this.total = res.data.money;
					}
				});
			},
			duihuan() {
				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 (!this.phone) {
					uni.navigateTo({
						url: '/pages/public/mobile'
					});
				} else if (!this.jifen) {
					this.$queue.showToast('请输入要兑换的积分');
					return;
				} else if (this.jifen > this.total) {
					uni.showModal({
						title: '积分提醒',
						content: '积分不足，当前可用积分为' + this.total + '！',
						showCancel: false
					});
					return;
				} else if (this.jifen < this.zuidi) {
					this.$queue.showToast('最低可兑换积分为' + this.zuidi);
					return;
				} else if (this.type == 2 && parseInt(this.jifen) > parseInt(this.WxMaxMoney)) {
					this.$queue.showToast('微信单次最大积分兑换额度为' + this.WxMaxMoney);
					return;
				} else if (this.type == 2) {
					let userId = this.$queue.getData('userId');
					this.$Request.postT('/userMoney/cashMoney?userId=' + userId + '&money=' + this.jifen + '&type=' + this
						.type).then(
						res => {
							if (res.code === 0) {
								this.getUserInfointegral(userId)
								uni.showModal({
									title: '积分提醒',
									content: '兑换成功！',
									showCancel: false
								});
							} else {
								this.$queue.showToast(res.msg);
							}
						});
				} else if (this.zhifubao && this.zhifubaoName && this.type == 1) {
					let userId = this.$queue.getData('userId');
					this.$Request.postT('/userMoney/cashMoney?userId=' + userId + '&money=' + this.jifen + '&type=' + this.type).then(res => {
							if (res.code === 0) {
								this.getUserInfointegral(userId)
								uni.showModal({
									title: '积分提醒',
									content: '兑换成功！',
									showCancel: false
								});
							} else {
								this.$queue.showToast(res.msg);
							}
						});
				} else {
					this.bangding();
				}
			},
			duihuanjilu() {
				uni.navigateTo({
					url: '/pagesB/pages/member/jifen'
				});
			},
			goBack() {
				uni.navigateBack({});
			},
			getToatal(userId) {
				this.$Request.postT('/user/getUserJifenList/' + userId).then(res => {
					if (res.status === 0) {
						this.contents = res.data;
					} else {
						this.contents = [];
					}
				});
			},
			// 全部兑换
			dhBtn() {
				this.jifen = this.total
			},
			// 绑定支付宝
			bangding() {
				uni.navigateTo({
					url: '/pagesB/pages/member/zhifubao'
				});
			},
			goDetail() {
				uni.navigateTo({
					url: '/pagesB/pages/member/cashList'
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.duihuan_title {
		font-size: 34upx;
		color: #333;
		margin-top: 47rpx;
		font-weight: 600;
	}

	.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: #ffc705 3upx solid;
				padding: 15rpx 0;
				border-radius: 15rpx;

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

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

	.btn {
		margin-top: 120upx;
		background-color: #3F64FF;
		color: #fff;
		width: 688upx;
		height: 102upx;
		line-height: 102upx;
		text-align: center;
		font-size: 32upx;
		border-radius: 50upx;
	}

	.news_content {
		display: flex;
		text-align: left;
		padding: 8px 16px 8px 16px;
		background: #ffffff;
		margin-top: 1px;
	}

	.news_content_text {
		margin-top: 8px;
		width: 200px;
		overflow: hidden;
		font-size: 16px;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	/*  */
	.user-box {
		width: 100%;
		height: 200upx;
		/* position: absolute; */
		z-index: 15;
		padding: 30upx 30upx 0;
		display: flex;
		align-items: center;
	}

	.user-info-box {
		flex: 1;
		height: 100%;
		display: flex;
		align-items: center;


	}

	.info-icon {
		width: 46upx;
		height: 48upx;

		image {
			width: 100%;
			height: 100%;
			display: block;
		}
	}

	.avatar {
		width: 110upx;
		height: 110upx;
		border-radius: 50%;
		overflow: hidden;

		image {
			width: 100%;
			height: 100%;
			display: block;
		}
	}

	.noLogin {
		display: flex;
		display: flex;
		flex-direction: column;
		margin-left: 20upx;
		font-size: 40upx;
		font-weight: 800;
		color: #FEFEFE;
	}

	.user-info {
		display: flex;
		display: flex;
		flex-direction: column;
		margin-left: 20upx;
		width: 260upx;

	}

	.nick-wrap {
		display: flex;
		align-items: center;
		height: 45upx;

	}

	.nick {
		font-size: 38upx;
		height: 38upx;
		line-height: 38upx;
		font-weight: 800;
		color: #333;
	}

	.nick_image {
		width: 38upx;
		height: 38upx;
		margin-left: 20upx;
	}

	.activation {
		width: 100upx;
		height: 38upx;
		line-height: 38upx;
		text-align: center;
		background: #FFFFFF;
		border-radius: 19upx;
		margin-left: 20upx;
		font-size: 24upx;
		font-weight: bold;
		color: #FF332F;
	}

	.code {
		display: flex;
		align-items: center;
		height: 45upx;
		font-size: 24upx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #999;
	}

	.bottom_view {
		display: flex;
		text-align: center;
		width: 49%;
		justify-content: center;
		align-items: center;

		image {
			width: 43rpx;
			height: 43rpx;
		}

		.text {
			margin-left: 15rpx
		}
	}


	/* 底部按钮 */
	// 底部按钮
	.footer {
		width: 100%;
		height: calc(98upx + env(safe-area-inset-bottom));
		height: calc(98upx + constant(safe-area-inset-bottom));
		padding-bottom: env(safe-area-inset-bottom);
		/*兼容IOS>11.2*/
		padding-bottom: constant(safe-area-inset-bottom);
		/*兼容IOS<11.2*/
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		padding: 0 30upx;
		z-index: 10;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.payinfo {
		display: flex;
		width: 48%;
		color: #333333;
		font-size: 28rpx;
		text-align: center;
		align-items: center;

	}

	.payinfo image {
		width: 32upx;
		position: absolute;
		left: 20%;
	}
</style>
