<template>
	<view class="page">
		<uni-nav-bar @clickLeft="goto(1, 1)" left-icon="back"  :border="false" :shadow="false" :fixed="true"
		:title="$t('signup.title')" backgroundColor="#fff"></uni-nav-bar>
		<view style="width: 92%; margin: auto; padding: 30rpx 20rpx;">
			<view style="font-size: 64rpx; color: #333; font-weight: 700;">{{$t('signup.title')}}</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 28rpx; margin-bottom: 40rpx; margin-top: 20rpx;">{{$t('signup.text1')}}</view>
			<uni-forms label-position="top" label-width="180rpx" :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="$t('signup.text2')"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item :label="$t('signup.text3')" name="code" label-width="400rpx">
					<uni-easyinput type="number" v-model="signupModel.code" :placeholder="$t('signup.text4')">
						<template #right>
							<button type="primary" 
									:disabled="isSend" 
									@click="sendCode()" 
									style="font-size: 28rpx; padding: auto 20rpx; width: 220rpx;">{{sendText}}</button>
						</template>
					</uni-easyinput>
				</uni-forms-item>
				<uni-forms-item :label="$t('forgot.text11')" name="password" label-width="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="signupModel.password" :placeholder="$t('login.text5')" />
				</uni-forms-item>
				<uni-forms-item :label="$t('signup.text6')" name="trade_password" label-width="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="signupModel.trade_password" :placeholder="$t('signup.text7')" />
				</uni-forms-item>
				<uni-forms-item :label="$t('signup.text11')" name="invite_code" label-width="400rpx">
					<uni-easyinput type="text" v-model="signupModel.invite_code" :placeholder="$t('signup.text9')"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<view class="f">{{$t('login.text6')}}</view>
			<button type="primary" round style="width: 100%;" @click="signup">{{$t('signup.title')}}</button>
		</view>
		<view style="bottom: 0rpx; width:100vw; height: 60rpx;">
			<view style="font-size: 24rpx; color: #6C7278; font-weight: 500; text-align: center;">
				{{$t('signup.text13')}}
				<span style="color: #4D81E7; margin-left: 20rpx;" @click="onPageJump('/pages/mine/login')">{{$t('forgot.text16')}}</span>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				sendText: this.$t('setPasswork.text8'),
				isSend: false,
				signupModel:{
					email:"",
					code:"",
					password:"",
					invite_code:"",
					trade_password:"",
				},
				rules: {
				    email: {rules:[{required: true,errorMessage: this.$t('forgot.text2')}]},
					code:{rules:[{required: true,errorMessage: this.$t('setPasswork.text12')}]},
				    password: {rules:[{required: true,errorMessage: this.$t('login.text10')}]},
					trade_password:{
						rules:[{required: true,errorMessage: this.$t('putforward.text5')},
							{minLength: 6, maxLength: 6, errorMessage: this.$t('putforward.text6') }],
					},
					invite_code: {rules:[{required: true,errorMessage: this.$t('putforward.text9')}]}
				},
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onLoad(e) {
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			...mapMutations(['setUserInfo']),
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
			goto(url, type) {
			    if (type == 2) {
			        return uni.switchTab({ url: url })
			    }
			    if (type == 1) {
			        return uni.navigateBack({ delta: url });
			    }
			    uni.navigateTo({
			        url: url
			    })
			},
			sendCode(){
				this.isSend = true;
				if (this.signupModel.email == '') {
					uni.showToast({title: this.$t('forgot.text19'), icon:'error'});
					this.isSend = false;
					return;
				}
				if (!this.$base.mailRegular.test(this.signupModel.email)) {
					uni.showToast({title: this.$t('forgot.text20'), icon:'error'});
					this.isSend = false;
					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 =  this.$t('forgot.text4',{par: s});
						let intervalId = setInterval(() => {
							s = s-1;
							if (s == 0) {
							    clearInterval(intervalId);
								this.isSend = false;
								this.sendText = "Get code";
							}
							else{
								this.sendText = this.$t('forgot.text4',{par: s});
							}
						}, 1000);
					}
				});
			},
			signup(){
				this.$refs.signupModel.validate().then(res=>{
					if (!this.$base.mailRegular.test(this.signupModel.email)) {
						uni.showToast({title:this.$t('signup.text12'),icon:'error'});
						return;
					}
					if(!this.$base.passwordRegular.test(this.signupModel.password)){
						uni.showToast({title:this.$t('signup.text14'),icon:'error'});
						return;
					}
					let data = {
						email: this.signupModel.email,
						code: this.signupModel.code,
						password: this.signupModel.password,
						invite_code: this.signupModel.invite_code,
						trade_password: this.signupModel.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: 32rpx;color: #999999;line-height: 46rpx; text-align: center; margin-top: 60rpx;
	}
	.rechargeNet{
		font-weight: 600;font-size: 40rpx;color: #333;line-height: 46rpx; text-align: center; margin-top: 40rpx;
	}
	.txt{
		font-size: 28rpx; color:#999; font-weight: 500;
	}
	.cell_right{
		font-size: 28rpx; color:#333; font-weight: 500;
	}
	.f{
		font-size: 24rpx; color:#4D81E7; font-weight: 500; text-align: right; margin-bottom: 60rpx;
	}
</style>