<template>
	<view class="page">
		<nav-bar title="Change login password" bgColor="#fff"></nav-bar>
		<view style="width: 92%; margin: 10px auto; padding: 15px 10px;">
			<uni-forms label-position="top" label-width="80px" :model="model" :rules="rules" ref="model">
				<uni-forms-item label="Email" name="email" label-width="400rpx">
					<uni-easyinput type="text" v-model="model.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="model.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">
					<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>
		</view>
		<view style="bottom: 0px; position: absolute; width:100vw; height: 60px;">
			<view class="cell_list">
				<button type="primary" round style="width: 100%;" @click="submit">Submit</button>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				sendText: 'Get code',
				isSend: false,
				model: {
				    email: '',
				    password: '',
				    cpassword: '',
					code: ''
				},
				rules: {
					email: {rules:[{required: true,errorMessage: 'Please input your email address'}]},
				    password: {rules:[{required: true,errorMessage: 'Please input password'}]},
				    cpassword: {rules:[{required: true,errorMessage: 'Please input confirm password'}]},
					code: {rules:[{required: true,errorMessage: 'Please input verification code'}]},
				}
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onLoad(e) {
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
			sendCode(){
				this.isSend = true;
				if (!this.$base.mailRegular.test(this.model.email)) {
					uni.showToast({title: 'Email address error', icon:'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){
						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(){
				this.$refs.model.validate().then(res=>{
					if (!this.$base.mailRegular.test(this.model.email)) {
						uni.showToast({title: 'Email address error', icon:'error'});
						return;
					}
					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.navigateBack();
							}, 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: 95vh;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>