<template>
	<view class="container">
		<view class="status_bar"><!-- 这里是状态栏 --></view>
		<view class="loginTitle">欢迎注册和合商城</view>
		<view class="loginText">请认真填写个人信息</view>
		<view class="login-box">
			<view class="username">账号</view>
			<input class="input-box" type="text" v-model="phone" placeholder="请输入电话" />
		</view>
		<view class="login-box">
			<view class="username">验证码</view>
			<view class="flex">
				<input class="input-box" type="number" v-model="code" placeholder="请输入验证码" />
				<view class="code" @click="verification">{{ countDown == 0 ? '获取验证码' : countDown }}</view>
			</view>
		</view>
		<view class="login-box">
			<view class="username">登录密码</view>
			<input class="input-box" type="password" v-model="loginPass" placeholder="请输入登录密码" />
		</view>
		<!-- <view class="login-box">
			<view class="username">交易密码</view>
			<input class="input-box" type="password" v-model="payPass" placeholder="请输入6位数的交易密码" />
		</view> -->
		<view class="login-box" style="margin-bottom: 50rpx;">
			<view class="username">邀请码</view>
			<input class="input-box" type="text" v-model="invitation" placeholder="请输入邀请码" />
		</view>
		
		<label class="two" @click="checkboxChange">
			<radio style="transform:scale(0.7)" color="#30d9da" :checked="istype" />
			我已阅读并同意
			<text @click.stop="navTo('/pages/contract/agreement')">《用户协议》</text>
			&nbsp
			<text @click.stop="navTo('/pages/contract/privacy')">《隐私政策》</text>
		</label>
		
		<view class="login" @click="register">注册</view>
	</view>
</template>

<script>
import { register, verify } from '@/api/login.js';
export default {
	data() {
		return {
			istype: false,
			phone: '', //用户
			loginPass: '', //密码
			payPass: '',
			invitation: '', //邀请码
			code: '', //验证码
			time: '', //保存倒计时对象
			countDown: 0 //倒计时
		};
	},
	onLoad() {
		// 获取扫码邀请人id
		this.invitation = uni.getStorageSync('spread') || '';
	},
	watch: {
		// 监听倒计时
		countDown(i) {
			if (i == 0) {
				clearInterval(this.time);
			}
		}
	},
	methods: {
		navTo(url){
			uni.navigateTo({
				url
			})
		},
		// 注册
		register() {
			let obj = this;
			if (obj.phone == '') {
				obj.$api.msg('请输入账号');
				return;
			}
			if (!/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(obj.phone) && (!/(^1[3|4|5|6|7|8|9][0-9]{9}$)/.test(this.phone))) {
				obj.$api.msg('请输入正确的手机');
				return;
			}
			if (obj.loginPass == '') {
				obj.$api.msg('请输入登录密码');
				return;
			}
			// if (obj.payPass == '') {
			// 	obj.$api.msg('请输入交易密码');
			// 	return;
			// }
			// if (obj.payPass.length != 6) {
			// 	obj.$api.msg('请输入交易密码只能为6位数');
			// 	return;
			// }
			if (obj.code == '') {
				obj.$api.msg('请输入验证码');
				return;
			}
			if (!obj.istype) {
				obj.$api.msg('请查看并同意用户协议和隐私政策');
				return;
			}
			if (obj.invitation == '') {
				obj.$api.msg('请输入邀请码');
				return;
			}
			register({
				account: obj.phone, //账号
				captcha: obj.code, //验证码
				password: obj.loginPass,
				trade_password: obj.payPass,
				spread: obj.invitation
			}).then(function(e) {
				uni.showToast({
					title: '注册成功',
					duration: 2000,
					position: 'top',
					icon: 'none'
				});
				setTimeout(function() {
					uni.navigateTo({
						url: '/pages/public/login'
					});
				}, 1000);
			}) ;
			//调用注册接口，成功跳转登录页
		},
		checkboxChange(){
			this.istype = !this.istype
			console.log(this.istype);
		},
		//发送验证码
		verification() {
			let obj = this;
			if (this.phone == '') {
				this.$api.msg('请输入账号');
				return;
			}
			if (!/^([a-zA-Z]|[0-9])(\w|\-|\.)+@[a-zA-Z0-9]+(\.([a-zA-Z]{2,4}))+$/.test(obj.phone) && (!/(^1[3|4|5|6|7|8|9][0-9]{9}$)/.test(this.phone))) {
				obj.$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 }) => {
						uni.showToast({
							title: '验证码已发送',
							duration: 2000,
							position: 'top',
							icon: 'none'
						});
					})
					.catch(err => {
						console.log(err);
					});
			}
		}
	}
};
</script>

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

	.container {
		width: 100%;
		padding: 10% 60rpx 0rpx 60rpx;
	}
}
.status_bar {
	height: var(--status-bar-height);
	width: 100%;
}
.loginTitle {
	font-weight: bold;
	color: #333333;
	font-size: 58rpx;
	padding-bottom: 25rpx;
}

.loginText {
	font-weight: 500;
	color: #333333;
	font-size: 34rpx;
}

.login-box {
	padding-top: 70rpx;
	.username {
		padding-bottom: 25rpx;
		font-weight: 500;
		color: #333333;
		font-size: 32rpx;
	}
}

.login {
	background: linear-gradient(0deg, #2E58FF, #1dcbe4);
	margin-top: 20rpx;
	color: #ffffff;
	text-align: center;
	padding: 26rpx 0rpx;
	border-radius: 50rpx;
	margin-top: 60rpx;
}
.code {
	background: linear-gradient(0deg, #2E58FF, #1dcbe4);
	color: #ffffff;
	border-radius: 10rpx;
	font-weight: 500;
	color: #ffffff;
	font-size: 26rpx;
	padding: 12rpx 19rpx;
}
.two {
	margin: 20rpx 0;
	font-size: 28rpx;
	text-align: center;
	font-family: PingFang SC;
	font-weight: 500;
	color: #999999;
	text {
		color: #30d9da;
	}
}
// /* input 样式 */
// .input-placeholder {
// 	color: #ffffff;
// }

// .placeholder {
// 	color: #ffffff;
// }
</style>
