<template>
	<view class="page">
		<uni-nav-bar @clickLeft="goto(1, 1)" left-icon="back"  :border="false" :shadow="false" :fixed="true" :title="$t('forgot.title')" backgroundColor="#fff"></uni-nav-bar>
		<view style="width: 92%; margin: 20rpx auto; padding: 30rpx 20rpx;" v-if="show==1">
			<view style="font-size: 64rpx; color: #333; font-weight: 700; margin-top: 40rpx;">{{$t('forgot.title')}}</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 28rpx; margin-bottom: 40rpx; margin-top: 20rpx;">{{$t('forgot.text1')}}</view>
			<uni-forms label-position="top" label-width="180rpx" :model="model">
				<uni-forms-item label="Email" name="email">
					<uni-easyinput type="text" v-model="model.email" :placeholder="$t('forgot.text2')"></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: 20rpx auto; padding: 30rpx 20rpx;" v-if="show==2">
			<view style="font-size: 64rpx; color: #333; font-weight: 700; margin-top: 40rpx;">{{$t('forgot.text5')}}</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 28rpx; margin-bottom: 40rpx; margin-top: 20rpx;">{{$t('forgot.text6')}}</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: 28rpx; font-weight: 400; color: #6C7278; text-align: right; line-height: 120rpx;">{{$t('forgot.text7')}}
			<span style="color: #1D61E7; margin:0rpx 20rpx;" @click="sendcode">{{$t('forgot.text8')}}</span></view>
			<button type="primary" round style="width: 100%;" :disabled="codedisabled" @click="submit()">{{$t('buttonSubmit')}}</button>
		</view>
		<view style="width: 92%; margin: 20rpx auto; padding: 30rpx 20rpx;" v-if="show==3">
			<view style="font-size: 64rpx; color: #333; font-weight: 700; margin-top: 40rpx;">{{$t('forgot.text9')}}</view>
			<view style="color: #6C7278; font-weight: 500; font-size: 28rpx; margin-bottom: 40rpx; margin-top: 20rpx;">{{$t('forgot.text10')}}</view>
			<uni-forms label-position="top" label-width="160rpx" :model="model" :rules="rules" ref="model">
				<uni-forms-item :label="$t('forgot.text11')" name="password">
					<uni-easyinput type="password" :passwordIcon="true" v-model="model.password" :placeholder="$t('forgot.text12')" />
				</uni-forms-item>
				<uni-forms-item :label="$t('forgot.text13')" name="cpassword" labelWidth="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="model.cpassword" :placeholder="$t('forgot.text14')" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" round style="width: 100%;" @click="changePossword()">{{$t('buttonSubmit')}}</button>
		</view>
		<view style="bottom: 0rpx; position: absolute; width:100vw; height: 100rpx;">
			<view style="font-size: 24rpx; color: #6C7278; font-weight: 500; text-align: center;">
				{{$t('forgot.text15')}}
				<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 {
				show: 1,
				sendText: this.$t('forgot.text3'),
				isSend: false,
				codedisabled: true,
				shouldFocus: true,
				code1: '',
				code2: '',
				code3: '',
				code4: '',
				model: {
				    email: '',
				    password: '',
				    cpassword: '',
					code: ''
				},
				rules: {
				    password: {rules:[{required: true,errorMessage: this.$t('forgot.text17')}]},
				    cpassword: {rules:[{required: true,errorMessage: this.$t('forgot.text18')}]},
				}
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		watch: {
			code4(newVal) {
				if (newVal) {
					this.codedisabled = false;
				}
			}
		},
		//第一次加载
		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(){
				if (this.model.email == '') {
					uni.showToast({title: this.$t('forgot.text19')});
					return;
				}
				if (!this.$base.mailRegular.test(this.model.email)) {
					uni.showToast({title: this.$t('forgot.text20')});
					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 =  this.$t('forgot.text4',{par: s});
						let intervalId = setInterval(() => {
							s = s-1;
							if (s == 0) {
							    clearInterval(intervalId);
								this.isSend = false;
								this.sendText = this.$t('forgot.text3');
							}
							else{
								this.sendText = this.$t('forgot.text4',{par: 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: this.$t('forgot.text21'), icon:'error'});
						return;
					}
					if (this.model.cpassword !== this.model.password) {
						uni.showToast({title: this.$t('forgot.text22'), 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: 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;
	}
</style>