<template>
	<view class="register">
		<view class="wrapper">
			<view class="input-content">
				<view class="cu-form-group" style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;">
					<view class="title">手机号</view>
					<input type="number" :disabled="true" :value="phone" placeholder="请输入手机号" maxlength="11" data-key="phone" @input="inputChange" />
				</view>
				<view class="cu-form-group" style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;">
					<text class="title">设置密码</text>
					<input type="password" :value="password" placeholder="请设置密码" placeholder-class="input-empty" maxlength="20"
					 minlength="6" data-key="password" @input="inputChange" />
				</view>
				<!-- <view class="cu-form-group" style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;" v-if="required == '是'">
					<text class="title">邀请码</text>
					<input type="number" maxlength="6" :value="invitation" placeholder="请填写邀请码(必填)" data-key="invitation" @input="inputChange" />
				</view>
				<view class="cu-form-group" style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;" v-if="required == '否'">
					<text class="title">邀请码</text>
					<input type="number" maxlength="6" :value="invitation" placeholder="请填写邀请码(选填)" data-key="invitation" @input="inputChange" />
				</view> -->
			</view>
			<button class="confirm-btn" @click="startReg">绑定手机号</button>
		</view>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				userName: '',
				showAgree: false,
				code: '',
				phone: '',
				password: '',
				required: '否',
				sending: false,
				sendTime: '获取验证码',
				count: 60,
				relation: "",
				state: '',
				invitation: '',
				platform: 'app',
				sysPhone:1
			}
		},
		onLoad(d) {
			console.log(d.phone)
			if(d.phone){
				this.phone = d.phone;
			}
			this.invitationMa();
			let a = this.$queue.getData("isEnable")
			if (a) {
				this.isEnable = a;
			}
			// #ifdef APP-PLUS
			switch (uni.getSystemInfoSync().platform) {
				case 'android':
					break;
				case 'ios':
					this.sysPhone = 2;
					break;
				default:
					break;
			}
			// #endif
		},

		mounted() {
			_this = this;
		},
		methods: {
			showMa() {
				//查询官方邀请码
				this.$Request.getT('/app/common/type/4').then(res => {
					if (res.code == 0) {
						this.invitationCode = res.data.value;
				
					}
				});
			},
			// 注册邀请码必填
			invitationMa() {
				this.$Request.getT('/app/common/type/3').then(res => {
					if (res.code == 0) {
						this.required = res.data.value;
				
					}
				});
			},
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack() {
				uni.navigateBack();
			},
			countDown() {
				const {
					count
				} = this;
				if (count === 1) {
					this.count = 60;
					this.sending = false;
					this.sendTime = '获取验证码';
				} else {
					this.count = count - 1;
					this.sending = true;
					this.sendTime = count - 1 + '秒后重新获取';
					setTimeout(this.countDown.bind(this), 1000);
				}
			},
			sendMsg() {
				const {
					phone
				} = this;
				if (!phone) {
					this.$queue.showToast("请输入手机号");
				} else if (phone.length !== 11) {
					this.$queue.showToast("请输入正确的手机号");
				} else {
					this.$queue.showLoading("正在发送验证码...");
					this.$Request.getT("/msg/sendMsg/" + phone + "/weixin").then(res => {
						if (res.code === 0) {
							this.sending = true;
							this.$queue.showToast('验证码发送成功请注意查收');
							this.countDown();
							uni.hideLoading();
						} else {
							console.log(JSON.stringify(res))
							uni.hideLoading();
							uni.showModal({
								showCancel: false,
								title: '短信发送失败',
								content: res.msg ? res.msg : '请一分钟后再获取验证码',
							});
						}
					});
				}
			},
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			startReg() {
				if (this.phone.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				
				if (!this.password) {
					this.$queue.showToast('请设置密码');
					return false;
				}
				
				if (this.password.length < 6) {
					this.$queue.showToast('密码位数必须大于六位');
					return false;
				}
				
				if (!_this.invitation && this.required == '是') {
					this.$queue.showToast('请填写邀请码');
					return false;
				}
				this.$queue.showLoading('登陆中...')
				_this.isRotate = true;
				let qdCodeion = '';
				if (uni.getStorageSync('qdCodeion')) {
					qdCodeion = uni.getStorageSync('qdCodeion')
				}
				this.$Request.postT("/app/Login/bindMobile", {
					password: _this.password,
					phone: _this.phone,
					inviterCode: _this.invitation,
					qdCode: qdCodeion,
					sysPhone:_this.sysPhone
				}).then(res => {
					console.log(JSON.stringify(res))
					if (res.code === 0) {
						this.$queue.setData("mobile", _this.phone);
						this.$queue.setData("token", res.token);
						this.$queue.setData("userId", res.user.userId);
						uni.switchTab({
							url: '/pages/index/index'
						})
					} else {
						_this.isRotate = false;
						uni.hideLoading();
						uni.showModal({
							showCancel: false,
							title: '绑定失败',
							content: res.msg,
						});
					}
				});
			},
			getUserInfo(userId, token) {
				this.$Request.get("/app/user/selectUserById").then(res => {
					if (res.code == 0) {
						uni.setStorageSync('token', token)
						uni.setStorageSync('userId', res.data.userId)
						uni.setStorageSync('avatar', res.data.avatar)
						uni.setStorageSync('invitationCode', res.data.invitationCode)
						uni.setStorageSync('zhiFuBao', res.data.zhiFuBao)
						uni.setStorageSync('zhiFuBaoName', res.data.zhiFuBaoName)
						this.$Request.get("/app/UserVip/isUserVip").then(res => {
							if (res.code == 0 && res.data && res.data.isVip == 2) {
								uni.setStorageSync('isVIP', true)
							} else {
								uni.setStorageSync('isVIP', false)
							}
						});
						uni.switchTab({
							url: '/pages/index/index'
						});
					} else {
						uni.showModal({
							showCancel: false,
							title: '登录失败',
							content: res.msg
						});
						this.$queue.logout();
					}
					uni.hideLoading();
				});
			}
		}
	}
</script>

<style lang='scss'>

	page {
		background: #ffffff;
	}
	
	.footer {
		padding-left: 140upx;
		margin-top: 32upx;
		text-align: center;
		display: flex;
	}
	
	.send-msg {
		border-radius: 30px;
		color: white;
		height: 30px;
		font-size: 14px;
		line-height: 30px;
		background: #5074FF;
	}
	
	.send-msgs {
		border-radius: 30px;
		color: #999999;
		height: 30px;
	
		font-size: 14px;
		line-height: 30px;
		background: white;
	}
	
	.container {
		padding-top: 32upx;
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;
		background: #ffffff;
	}
	
	.wrapper {
		position: relative;
		z-index: 90;
		background: #ffffff;
		padding-bottom: 20px;
	}
	
	.input-content {
		padding: 0 20px;
	}
	
	
	.confirm-btn {
		width: 300px;
		height: 42px;
		line-height: 42px;
		border-radius: 30px;
		margin-top: 40px;
		background: #5074FF;
		color: #ffffff;
		font-size: 32rpx;
	
		&:after {
			border-radius: 60px;
		}
	}
</style>
