<template>
	<view class="container">
		<view class="wrapper">
			<view class="input-content">
				<view class="cu-form-group" style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;">
					<view class="title text-black">手机号</view>
					<input type="number" :value="phone" placeholder="请输入手机号" maxlength="11" data-key="phone"
						@input="inputChange" />
				</view>
				<view class="cu-form-group padding-right-xs"
					style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;">
					<text class="title text-black">验证码</text>
					<input type="number" :value="code" placeholder="请输入验证码" maxlength="6" data-key="code"
						@input="inputChange" @confirm="startReg" />
					<button class="send-msg" @click="sendMsg" :disabled="sending">{{ sendTime }}</button>
				</view>

				<view class="cu-form-group" style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;">
					<text class="title text-black">设置密码</text>
					<input type="password" :value="password" placeholder="请设置密码" placeholder-class="input-empty"
						maxlength="20" minlength="6" data-key="password" @input="inputChange" @confirm="startReg" />
				</view>
				<!-- <view class="cu-form-group" style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;"
					v-if="required == '是'">
					<text class="title text-black">邀请码</text>
					<input type="" maxlength="6" :value="invitation" placeholder="请填写邀请码(必填)" data-key="invitation"
						@input="inputChange" @confirm="startReg" />
				</view>
				<view class="cu-form-group" style="border-bottom: 2upx solid whitesmoke;margin-bottom: 20px;"
					v-if="required == '否'">
					<text class="title text-black">邀请码</text>
					<input type="" maxlength="6" :value="invitation" placeholder="请填写邀请码(选填)" data-key="invitation"
						@input="inputChange" @confirm="startReg" />
				</view> -->
			</view>
			<button class="confirm-btn" @click="startReg">立即注册</button>
			<view class="footer">
				<text @tap="isShowAgree" class="cuIcon"
					:class="showAgree ? 'cuIcon-radiobox' : 'cuIcon-round'">同意</text>
				<!-- 协议地址 -->
				<navigator url="/me/setting/mimi" open-type="navigate">《隐私政策》</navigator>
				和
				<navigator url="/me/setting/xieyi" open-type="navigate">《用户服务协议》</navigator>
			</view>
		</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() {
			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("/app/Login/sendMsg/" + phone + "/bindIos").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.code.length != 6) {
					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;
				console.error(_this.$queue.getData('appleId'))
				let qdCodeion = '';
				if (uni.getStorageSync('qdCodeion')) {
					qdCodeion = uni.getStorageSync('qdCodeion')
				}
				this.$Request.postT("/app/Login/iosBindMobile", {
					password: _this.password,
					phone: _this.phone,
					code: _this.code,
					inviterCode: _this.invitation,
					qdCode: qdCodeion,
					platform: 'app',
					sysPhone: _this.sysPhone,
					appleId: _this.$queue.getData("appleId")
				}).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)
						uni.setStorageSync('certNum', res.data.certNum)
						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 {
		height: 100%;
		background: #FFFFFF !important;
	}

	.footer {
		padding-left: 140upx;
		margin-top: 32upx;
		text-align: center;
		display: flex;
		color: #333;
	}

	.send-msg {
		border-radius: 30px;
		color: white;
		height: 30px;
		font-size: 10px;
		line-height: 30px;
		background: #5E81F9;
	}


	.container {
		padding-top: 32upx;
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;

	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #FFFFFF;
		padding-bottom: 20px;
	}


	.input-content {
		padding: 32upx 80upx;
	}


	.confirm-btn {
		width: 600upx;
		height: 80upx;
		line-height: 80upx;
		border-radius: 60upx;
		margin-top: 32upx;
		background: #5E81F9;

		color: #fff;
		font-size: 32upx;

		&:after {
			border-radius: 60px;
		}
	}
</style>