<template>
	<view class="page">
		<nav-bar title="Sign up" bgColor="#fff"></nav-bar>
		<view style="width: 92%; margin: auto; padding: 15px 10px;" v-if="pageShow==1">
			<view style="font-size: 32px; color: #333; font-weight: 700;">Sign up</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 14px; margin-bottom: 20px; margin-top: 10px;">Create an account to continue!</view>
			<uni-forms label-position="top" label-width="80px" :modelValue="signupModel" :rules="rules" ref="signupModel">
				<uni-forms-item label="Email" name="email" label-width="400rpx">
					<uni-easyinput type="text" v-model="signupModel.email" placeholder="Please enter your email address"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="Verification code" name="code" label-width="400rpx">
					<uni-easyinput type="number" v-model="signupModel.code" placeholder="Enter email verification code">
						<template #right>
							<button type="primary" 
									:disabled="isSend" 
									@click="sendCode()" 
									style="font-size: 28rpx; padding: auto 20rpx; width: 200rpx;">{{sendText}}</button>
						</template>
					</uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="Password" name="password" label-width="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="signupModel.password" placeholder="Please enter your Password" />
				</uni-forms-item>
				<uni-forms-item label="Confirm password" name="cpassword" label-width="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="signupModel.cpassword" placeholder="Please enter your confirm password" />
				</uni-forms-item>
			</uni-forms>
			<view class="f">Forgot Password ?</view>
			<button type="primary" round style="width: 100%;" @click="next">Next</button>
		</view>
		<view style="width: 92%; margin: auto; padding: 15px 10px;" v-if="pageShow==2">
			<view style="font-size: 32px; color: #333; font-weight: 700;">Sign up</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 14px; margin-bottom: 20px; margin-top: 10px;">Create an account to continue!</view>
			<uni-forms label-position="top" label-width="80px" :modelValue="signupModel1" :rules="rules1" ref="signupModel1">
				<uni-forms-item label="Trade password" name="trade_password" label-width="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="signupModel1.trade_password" placeholder="Please enter your trade Password" />
				</uni-forms-item>
				<uni-forms-item label="Confirm Trade password" name="ctrade_password" label-width="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="signupModel1.ctrade_password" placeholder="Please enter your confirm trade Password" />
				</uni-forms-item>
				<uni-forms-item label="Invitation code" name="invite_code" label-width="400rpx">
					<uni-easyinput type="text" v-model="signupModel1.invite_code" placeholder="Please enter your invitation code"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<view style="font-size: 12px; color:#4D81E7; font-weight: 500; text-align: right; margin-bottom: 30px;">Forgot Password ?</view>
			<button type="primary" round style="width: 100%;" @click="signup">Sign up</button>
		</view>
		<view style="bottom: 0px; position: absolute; width:100vw; height: 60rpx;">
			<view style="font-size: 12px; color: #6C7278; font-weight: 500; text-align: center;">
				Already have an account?
				<span style="color: #4D81E7; margin-left: 10px;" @click="onPageJump('/pages/mine/login')">Login</span>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				sendText: "Get code",
				isSend: false,
				pageShow: 1,
				signupModel:{
					email:"",
					code:"",
					password:"",
					cpassword:"",
				},
				signupModel1:{
					invite_code:"",
					trade_password:"",
					ctrade_password:"",
				},
				rules: {
				    email: {rules:[{required: true,errorMessage: 'Please enter your email address'}]},
					code:{rules:[{required: true,errorMessage: 'Please input Verification code'}]},
				    password: {rules:[{required: true,errorMessage: 'Please input a password'}]},
				    cpassword: {rules:[{required: true,errorMessage: 'Please input Confirm password'}]},
				},
				rules1: {
					trade_password:{
						rules:[{required: true,errorMessage: 'Please input trade password'},
							{minLength: 6, maxLength: 6, errorMessage: 'Please enter a 6-digit transaction password', }],
					},
					ctrade_password: {rules:[{required: true,errorMessage: 'Please input Trade password'}]},
				    invite_code: {rules:[{required: true,errorMessage: 'Please input invitation code'}]}
				}
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onLoad(e) {
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			...mapMutations(['setUserInfo']),
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
			next(){
				this.$refs.signupModel.validate().then(res=>{
					if (!this.$base.mailRegular.test(this.signupModel.email)) {
						uni.showToast({title:'Please enter the correct email address',icon:'error'});
						return;
					}
					if(!this.$base.passwordRegular.test(this.signupModel.password)){
						uni.showToast({title:'Please enter a 6-10 digit password',icon:'error'});
						return;
					}
					if (this.signupModel.cpassword !== this.signupModel.password) {
						uni.showToast({title:'Two passwords are inconsistent',icon:'error'});
						return;
					}
					this.pageShow = 2;
				}).catch(err =>{})
			},
			sendCode(){
				this.isSend = true;
				if (this.signupModel.email == '') {
					uni.showToast({title: 'Email is empty', icon:'error'});
					return;
				}
				if (!this.$base.mailRegular.test(this.signupModel.email)) {
					uni.showToast({title: 'Email address error', icon:'error'});
					return;
				}
				let s = 120;
				let data = {
					type: "email",
					event: "register",
					email: this.signupModel.email,
					lang: this.$i18n.locale
				}
				this.$http.post('/api/common/captcha', data).then(res => {
					if(res.code == 0){
						uni.showToast({title: 'Successfully sent'});
						this.sendText =  "Send in " + s + "s";
						let intervalId = setInterval(() => {
							s = s-1;
							if (s == 0) {
							    clearInterval(intervalId);
								this.isSend = false;
								this.sendText = "Get code";
							}
							else{
								this.sendText = "Send in " + s + "s";
							}
						}, 1000);
					}
				});
			},
			signup(){
				this.$refs.signupModel1.validate().then(res=>{
					if (this.signupModel.trade_password !== this.signupModel.ctrade_password) {
						uni.showToast({title:'The transaction passwords entered twice are different',icon:'error'});
						return;
					};
					let data = {
						email: this.signupModel.email,
						code: this.signupModel.code,
						password: this.signupModel.password,
						invite_code: this.signupModel1.invite_code,
						trade_password: this.signupModel1.trade_password,
					}
					this.$http.post('/api/common/register', data).then(res => {
						if(res.code == 0){
							this.setUserInfo(res.data.userinfo);
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/mine/index'
								});
							}, 1000);
						}
					});
				}).catch(err =>{})
				
			}
		},
		//页面卸载
		onUnload() {},
	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';
	body{background-color: #fff;}
	.page{min-height: 100vh;position: relative;}
	.rechargeTitle{
		font-weight: 400;font-size: 16px;color: #999999;line-height: 23px; text-align: center; margin-top: 30px;
	}
	.rechargeNet{
		font-weight: 600;font-size: 20px;color: #333;line-height: 23px; text-align: center; margin-top: 20px;
	}
	.txt{
		font-size: 14px; color:#999; font-weight: 500;
	}
	.cell_right{
		font-size: 14px; color:#333; font-weight: 500;
	}
	.f{
		font-size: 12px; color:#4D81E7; font-weight: 500; text-align: right; margin-bottom: 30px;
	}
</style>