<template>
	<view>
		<view style="margin: 0 0 30rpx 0;">
			<img :src="info.share_img" alt="" style="width: 100%;">
		</view>
		<view class="in_box">
			<view class="in_text">我的邀请码</view>
			<input class="in_input" type="number" disabled="true" v-model="info.code" @click="copy">
		</view>
		<view class="in_box" v-if="info.state == 1">
			<view class="in_text">我的邀请人</view>
			<input class="in_input" type="text" disabled="true" v-model="info.invite" >
		</view>
		<view class="in_box" v-else>
			<view class="in_text">输入邀请码</view>
			<input class="in_input" type="number" v-model='user_id' style="width: 46%;">
			<button style="height: 65rpx;line-height: 65rpx;font-size: 30rpx;    margin: 0 0 0 18rpx;
    background-color: #4CAF50;
    color: #fff;" @click="bind">绑定</button>
		</view>
		<view class="in_info">
			<view style="margin: 20rpx 0;">活动规则: </view>
			<view>1. 邀请人成功邀请到用户后会赠送课程优惠券</view>
			<view>2. 被邀请用户可以免费获得体验会员</view>
			<view>3. 每位用户限定绑定一次</view>
			<view>4. 用户之间无法互相绑定</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				info: {},
				user_id: '',
			}
		},
		onLoad() {
			if (uni.getStorageSync('id')) {
				this.getInfo();
				this.show = true;
			} else {
				// #ifdef MP
				this.mpLogin()
				return
				uni.showToast({
					title: '请先登录'
				})
				// #endif
			}
		},
		methods: {
			copy() {
				uni.setClipboardData({
					data: this.info.code,
					success: function() {
						uni.showToast({
							title: '复制成功',
							icon: 'none',
						})
					}
				});
			},
			bind() {
				let user_id = uni.getStorageSync('id');
				let invite_id = this.user_id.substring(4);

				if (!invite_id) {
					uni.showToast({
						title: '请输入邀请码',
						icon: 'none',
					})
					return false;
				}
				if (user_id == invite_id) {
					uni.showToast({
						title: '无法绑定自己',
						icon: 'none',
					})
					return false;
				}

				uni.showModal({
					title: '提示',
					content: '每个账号只能绑定一个邀请人, 您确定绑定么',
					success: (res) => {
						this.$post({
							data: {
								do: 'UserInvite',
								data: {
									invite_id: invite_id,
									user_id: user_id,
								}
							}
						}).then(res => {
							if(res.data.status != 1){
								uni.showToast({
									title: '绑定失败',
									icon: 'none',
								})
								return false;
							}else{
								uni.showToast({
									title: '绑定成功',
									icon: 'none',
								})
								
								this.$post({
									data: {
										do: 'Vip',
										data: {
											user_id: user_id,
										}
									}
								}).then(res3 => {
									
									if(res3.data.is_vip == 1){
										uni.setStorageSync('is_vip',1);
									}
									
								}).catch(err => {})
								
								this.getInfo();
								return false;
							}
							
						}).catch(err => {})
					}
				})
			},
			getInfo() {
				let that = this;
				let user_id = uni.getStorageSync('id');
				this.$post({
					data: {
						do: 'GetShareInfo',
						data: {
							user_id: user_id,
						}
					}
				}).then(res => {
					that.info = res.data
				})
			},
			// 小程序登录
			mpLogin() {
				let that = this;
				uni.login({
					provider: 'weixin',
					success: (res) => {
						console.log(res, 'rrrrrr')
						const code = res.code
						this.$post({
							data: {
								do: 'Login',
								data: {
									code
								}
							}
						}).then(res => {
							this.logininfo = res.data
							uni.showModal({
								title: '提示',
								content: '是否进行头像和昵称授权',
								success: (res) => {
									if (res.confirm) {
										this.getnickImage()
									}
								}
							})
						})
					}
				})
			},
			getnickImage() {
				console.log(this.logininfo.id)
				uni.getUserProfile({
					desc: '用于昵称和头像的展示',
					success: res => {
						console.log(res, '++++')
						console.log({
							user_id: this.logininfo.id,
							nickName: res.userInfo.nickName,
							avatarUrl: res.userInfo.avatarUrl
						})
						this.$post({
							data: {
								do: 'UpdateUser',
								data: {
									user_id: this.logininfo.id,
									nickName: res.userInfo.nickName,
									avatarUrl: res.userInfo.avatarUrl
								}
							}
						}).then(res => {
							for (let i in this.logininfo) {
								uni.setStorageSync(i, this.logininfo[i])
							}
							this.type == 0;
							this.getInfo();
						})
						// this.nickName = res.userInfo.nickName
						// this.avatarUrl = res.userInfo.avatarUrl
						// this.country = res.userInfo.country
						// this.province = res.userInfo.province
						// this.city = res.userInfo.city
						// this.gender = res.userInfo.gender
						// uni.login({
						// 	provider: 'weixin',
						// 	success: (res) => {
						// 		this.loginCode = res.code
						// 		uni.hideLoading()
						// 		this.popShow = true
						// 	},
						// })
					},
					fail: (err) => {
						uni.hideLoading()
					}
				})
			},
		}
	}
</script>

<style>
	.in_input {
		margin: 0 0 0 30rpx;
		background-color: #f1f1f1;
		height: 65rpx;
		border-radius: 10rpx;
		width: 64%;
		padding: 0 15rpx;
	}

	.in_text {
		margin: 0 0 0 29rpx;
	}

	.in_box {
		display: flex;
		height: 85rpx;
		align-items: center;
	}
	.in_info{
		margin: 20rpx;
		padding: 10rpx;
		border-radius: 20rpx;
		background-color: #fff;
	}
</style>
