<template>
	<view class="page">
		<nav-bar title="forgot password" bgColor="#fff"></nav-bar>
		<view style="width: 92%; margin: 10px auto; padding: 15px 10px;" v-if="show==1">
			<view style="font-size: 32px; color: #333; font-weight: 700; margin-top: 20px;">Forgot Password</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 14px; margin-bottom: 20px; margin-top: 10px;">Don't worry it happens. Please enter the address associate with your account.</view>
			<uni-forms label-position="top" label-width="80px" :model="model">
				<uni-forms-item label="Email" name="email">
					<uni-easyinput type="text" v-model="model.email" placeholder="Please enter your email address"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<button type="primary" round style="width: 100%;" :disabled="isSend" @click="sendcode()">{{sendText}}</button>
		</view>
		<view style="width: 92%; margin: 10px auto; padding: 15px 10px;" v-if="show==2">
			<view style="font-size: 32px; color: #333; font-weight: 700; margin-top: 20px;">Verification code</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 14px; margin-bottom: 20px; margin-top: 10px;">We have to sent the code verification to Your Email.</view>
			<uni-easyinput type="number" :maxlength="1" :clearable="false" :focus="show==2" v-model="code1" style="width: 80rpx; float: left;" ref="myInput0"></uni-easyinput>
			<uni-easyinput type="number" :clearable="false" :focus="code1.length>0" v-model="code2" style="width: 80rpx; float: left; margin-left: 90rpx;" ref="myInput1"></uni-easyinput>
			<uni-easyinput type="number" :clearable="false" :focus="code2.length>0" v-model="code3" style="width: 80rpx; float: left; margin-left: 90rpx;" ref="myInput2"></uni-easyinput>
			<uni-easyinput type="number" :clearable="false" :focus="code3.length>0" v-model="code4" style="width: 80rpx; float: left; margin-left: 90rpx;" ref="myInput3"></uni-easyinput>
			<view style="font-size: 14px; font-weight: 400; color: #6C7278; text-align: right; line-height: 60px;">Didn't get a code? 
			<span style="color: #1D61E7; margin:0px 10px;">Resend</span></view>
			<button type="primary" round style="width: 100%;" :disabled="codedisabled" @click="submit()">Submit</button>
		</view>
		<view style="width: 92%; margin: 10px auto; padding: 15px 10px;" v-if="show==3">
			<view style="font-size: 32px; color: #333; font-weight: 700; margin-top: 20px;">Change password</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 14px; margin-bottom: 20px; margin-top: 10px;">Don't be afraid of forgetting the password, you can change it to a password that you can remember.</view>
			<uni-forms label-position="top" label-width="80px" :model="model" :rules="rules" ref="model">
				<uni-forms-item label="Password" name="password">
					<uni-easyinput type="password" :passwordIcon="true" v-model="model.password" placeholder="Please enter your Password" />
				</uni-forms-item>
				<uni-forms-item label="Confirm password" name="cpassword" labelWidth="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="model.cpassword" placeholder="Please enter your confirm Password" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" round style="width: 100%;" @click="changePossword()">Submit</button>
		</view>
		<view style="bottom: 0px; position: absolute; width:100vw; height: 50px;">
			<view style="font-size: 12px; color: #6C7278; font-weight: 500; text-align: center;">
				You remember your password?
				<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 {
				show: 1,
				sendText: 'Send Code',
				isSend: false,
				codedisabled: true,
				shouldFocus: true,
				code1: '',
				code2: '',
				code3: '',
				code4: '',
				model: {
				    email: '',
				    password: '',
				    cpassword: '',
					code: ''
				},
				rules: {
				    password: {rules:[{required: true,errorMessage: 'Please input password'}]},
				    cpassword: {rules:[{required: true,errorMessage: 'Please input Confirm password'}]},
				}
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		watch: {
			code4(newVal) {
				if (newVal) {
					this.codedisabled = false;
				}
			}
		},
		//第一次加载
		onLoad(e) {
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			...mapMutations(['setUserInfo']),
			onPageJump(url) {
				uni.navigateTo({
					url: url,
				});
			},
			sendcode(){
				if (this.model.email == '') {
					uni.showToast({title: 'Email is empty'});
					return;
				}
				if (!this.$base.mailRegular.test(this.model.email)) {
					uni.showToast({title: 'Email address error'});
					return;
				}
				let s = 120;
				let data = {
					type: "email",
					event: "resetpwd",
					email: this.model.email,
					lang: this.$i18n.locale
				}
				this.$http.post('/api/common/captcha', data).then(res => {
					if(res.code == 0){
						this.show=2;
						this.isSend = true;
						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);
					}
				});
			},
			submit(){
				if(this.code1 === ''|| this.code2==='' || this.code3 === '' || this.code4 === ''){
					uni.showToast({title: this.$t('codeIncorrect'), icon:'error'})
					return;
				}
				this.show=3;
				this.model.code = this.code1 + this.code2 + this.code3 + this.code4;
			},
			changePossword(){
				this.$refs.model.validate().then(res=>{
					if (!this.$base.passwordRegular.test(this.model.password)) {
						uni.showToast({title: 'Please enter a 6-10 digit password', icon:'error'});
						return;
					}
					if (this.model.cpassword !== this.model.password) {
						uni.showToast({title: 'Two passwords are inconsistent', icon:'error'});
						return;
					}
					this.$http.post('/api/common/resetpwd', {email: this.model.email, newpassword: this.model.password, code: this.model.code}).then(res => {
						if(res.code == 0){
							uni.showToast({title: res.data.mes});
							setTimeout(() => {
								uni.navigateTo({url: '/pages/mine/login'});
							}, 1000);
						}
					});
				});
			}
		},
		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		//页面下来刷新
		onPullDownRefresh() {},
		//页面上拉触底
		onReachBottom() {},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</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;
	}
</style>