<template>
	<view class="container">
		<view class="title-img">
			<image class="title-image" src="../../static/img/hlogo.png" mode="widthFix"></image>
		</view>
		<view class="title-font">
			挺酒商城
		</view>
		<view class="login_text">
			<view class="login_input flex">
				<image class="login_img" src="../../static/img/user.png" mode="widthFix"></image>
				<view class="login_name"><input class="uni-input" v-model="phone" focus placeholder="请输入手机号" /></view>
			</view>
			<view class="login_input flex">
				<image class="login_img" src="../../static/img/pwd.png" mode="widthFix"></image>
				<view class="login_name"><input class="uni-input" type="password" v-model="password" focus
						placeholder="请输入密码" /></view>
			</view>
			<view class="login_input flex">
				<image class="login_img" src="../../static/img/pwd.png" mode="widthFix"></image>
				<view class="login_name"><input class="uni-input" type="password" v-model="repassword" focus
						placeholder="请重复输入密码" /></view>
			</view>
			<view class="login_input flex">
				<image class="login_img" src="../../static/img/send.png" mode="widthFix"></image>
				<view class="login_name"><input class="uni-input" type="text" v-model="invitation" focus
						placeholder="请输入邀请人手机号" /></view>
			</view>
			<!-- <view class="login_input flex">
				<image class="login_img" src="../../static/img/send.png" mode="widthFix"></image>
				<view class="login_name flex">
					<input class="uni-input width" v-model="code" focus placeholder="请输入验证码" />
					<view class="code" @click="verification">{{ countDown == 0 ? '验证码' : countDown }}</view>
				</view>
			</view> -->
			<view><button type="green" @click="register" class="uni-button uni-button-green">注册账号</button></view>
			<view><button class="uni-button uni-button-green-plain" type="green" plain="true" hover-class="none"
					@click="login">返回登录</button></view>
		</view>
	</view>
</template>
<script>
	import {
		register,
		verify
	} from '@/api/login.js';
	export default {
		data() {
			return {
				phone: '', //用户
				password: '', //密码
				repassword: '',
				invitation: '', //邀请码
				code: '', //验证码
				time: '', //保存倒计时对象
				countDown: 0, //倒计时
				iszhu: 0,
			};
		},
		onLoad(options) {
			// 获取扫码邀请人id
			this.invitation = uni.getStorageSync('spread') || '';
			if (options.spread) {
				this.invitation = options.spread;
			}
		},
		watch: {
			// 监听倒计时
			countDown(i) {
				if (i == 0) {
					clearInterval(this.time);
				}
			}
		},
		methods: {
			// 注册
			register() {
				let obj = this;
				if (obj.phone == '') {
					obj.$api.msg('请输入电话号码');
					return;
				}
				if (!/(^1[2|3|4|5|6|7|8|9][0-9]{9}$)/.test(this.phone)) {
					obj.$api.msg('请输入正确的手机号');
					return;
				}
				if (obj.password == '') {
					obj.$api.msg('请输入密码');
					return;
				}
				if (obj.repassword == '') {
					obj.$api.msg('请再次输入密码');
					return;
				}
				if (obj.repassword != obj.password) {
					obj.$api.msg('两次密码不正确');
					return;
				}
				// if ((obj.invitation = '')) {
				// 	obj.$api.msg('请输入邀请码');
				// 	return;
				// }
				// if (obj.code == '') {
				// 	obj.$api.msg('请输入验证码');
				// 	return;
				// }
				if (obj.iszhu == 1) {
					return
				}
				obj.iszhu = 1
				uni.showLoading({
					title: '注册中'
				})
				register({
					account: obj.phone, //账号
					captcha: '123456', //验证码
					password: obj.password, //密码
					spread: this.invitation //上级推广人
				}).then(function(e) {
					obj.iszhu = 0;
					uni.showToast({
						title: '注册成功',
						duration: 2000,
						position: 'top'
					});
					uni.hideLoading()
					setTimeout(function() {
						uni.navigateTo({
							url: '/pages/public/login'
						});
					}, 1000);
				}).catch(e => {
					uni.hideLoading()
					obj.iszhu = 0
				});
				//调用注册接口，成功跳转登录页
			},
			//发送验证码
			verification() {
				let obj = this;
				if (this.phone == '') {
					this.$api.msg('请输入电话号码');
					return;
				}
				if (this.phone.length < 11) {
					this.$api.msg('请输入正确的手机号');
					return;
				}
				// 判断是否在倒计时
				if (obj.countDown > 0) {
					return false;
				} else {
					obj.countDown = 60;
					obj.time = setInterval(() => {
						obj.countDown--;
					}, 1000);
					//调用验证码接口
					verify({
							phone: obj.phone,
							type: 'register'
						})
						.then(({
							data
						}) => {})
						.catch(err => {
							console.log(err);
						});
				}
			},
			login() {
				//返回登录
				uni.navigateTo({
					url: '/pages/public/login'
				});
			}
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
		background: #ffffff;
	}

	.title-img {
		margin: 100rpx auto 0;
		width: 160rpx;

		.title-image {
			width: 160rpx;
		}
	}

	.title-font {
		margin-top: 10rpx;
		text-align: center;
		font-size: 32rpx;
		font-family: Source Han Sans CN;
		font-weight: bold;
		color: #333333;
	}

	.login_text {
		position: relative;
		padding: 100rpx 102rpx;
		background-color: #ffffff;

		.login_input {
			margin-top: 20rpx;
			padding: 26rpx;
			border-radius: 21rpx;
			border-bottom: 1px solid #eeeeee;

			.login_img {
				width: 30rpx;
				flex-shrink: 0;
			}

			.uni-input {
				text-align: left;
				width: 470rpx;
				font-size: 28rpx !important;
			}

			.login_name {
				margin-left: 20rpx;
				color: #333333;
			}
		}

		.other {
			margin-top: 60rpx;

			.fenge {
				width: 30%;
				height: 2rpx;
				background-color: #eeeeee;
			}

			.qita {
				font-size: 28rpx;
				color: #999999;
			}
		}

		.weixin {
			width: 75rpx;
			height: 75rpx;
			margin: 25rpx auto;
		}

		.weixin image {
			width: 100%;
			height: 100%;
		}

		.weixin_text {
			text-align: center;
			font-size: 28rpx;
			color: #999999;
		}

		.forget {
			font-size: 28rpx;
			width: 100%;
			text-align: right;
			color: #999999;
		}

		.uni-button-green {
			margin: 40rpx 10rpx;
			background: linear-gradient(90deg, #CA57DC, #65B2E9);
			border-radius: 10rpx;
			color: #ffffff;
		}

		.uni-button-green-plain {
			margin: 40rpx 10rpx;
			border-radius: 10rpx;
			color: #CA57DC;
			border: 1px solid #CA57DC;
		}

		.uni-button {
			height: 85rpx;
			line-height: 85rpx;
		}
	}


	.forget {
		width: 100rpx;
		font-size: 24rpx;
		color: #ffffff;
		margin: 0px auto;
		border-bottom: 1px solid #ffffff;
	}

	.width {
		width: 325rpx !important;
	}

	.code {
		padding-right: 30rpx;
		font-size: 23rpx;
		width: 150rpx;
		flex-shrink: 0;
		text-align: center;
		color: #2E58FF;

		background: linear-gradient(0deg, #CA57DC 0%, #65B2E9 100%);
		-webkit-background-clip: text;
		-webkit-text-fill-color: transparent;
	}

	uni-button {
		height: 80rpx !important;
		line-height: 80rpx !important;
	}
</style>