<template>
	<view class="container">
		<view class="" style="text-align: left;padding-left: 30rpx;font-size: 52rpx;padding-top: 50rpx;">{{ $t('login.czmm') }}</view>
		<view class="tit">{{ $t('login.yx') }}</view>
		<input type="text" class="shuru" :placeholder="$t('login.yx')" v-model="email" />
		<view class="tit">{{ $t('login.xmm') }}</view>
		<input type="text" class="shuru" :placeholder="$t('login.xmm')" v-model="lpwd" />
		<view class="tit">{{ $t('login.yxyzm') }}</view>
		<view class="flex" style="width: 673rpx;justify-content: space-between;margin: auto;">
			<input type="text" class=" yzm" :placeholder="$t('login.yzm')" v-model="ecode" />
			<view class="code yzmbtn" @click="verification">{{ countDown == 0 ? $t('login.yzm') : countDown }}</view>
		</view>
		<view class="flex" style="justify-content: space-between;padding: 50rpx 30rpx;align-items: flex-start;">
			<view class=""></view>
			<image src="../../static/icon/goto.png" mode="" style="width: 97rpx;height: 97rpx;" @click="resetpwd()"></image>
		</view>
	</view>
</template>
<script>
import { mapMutations } from 'vuex';
import { verify, loginMobile, getUserInfo, getForget, resetpwd } from '@/api/login.js';
export default {
	data() {
		return {
			ecode: '',
			email: '',
			lpwd: '',
			phone: '', //用户
			code: '', //验证码
			time: '', //保存倒计时对象
			countDown: 0 //倒计时
		};
	},
	onLoad() {
		uni.setNavigationBarTitle({ title: this.$t('login.czmm') });
	},
	watch: {
		// 监听倒计时
		countDown(i) {
			if (i == 0) {
				clearInterval(this.time);
			}
		}
	},
	methods: {
		...mapMutations('user', ['setUserInfo', 'login']),
		resetpwd() {
			let obj = this;
			if (obj.email == '') {
				return obj.$api.msg(this.$t('login.qsryx'));
			}
			if (!obj.isEmailAvailable(obj.email)) {
				return obj.$api.msg(this.$t('login.qsrzqdyx'));
			}
			if (obj.ecode == '') {
				return obj.$api.msg(this.$t('login.qsryzm'));
			}
			if (obj.lpwd == '') {
				return obj.$api.msg(this.$t('login.qsrxmm'));
			}
			resetpwd({
				email: obj.email,
				ecode: obj.ecode,
				lpwd: obj.lpwd
			})
				.then(res => {
					console.log(res);
					obj.login();
				})
				.catch(err => {});
		},
		// 手机登录
		register() {
			let obj = this;
			if (obj.phone == '') {
				obj.$api.msg('请输入电话号码');
				return;
			}
			if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(this.phone)) {
				obj.$api.msg('请输入正确的手机号');
				return;
			}
			if (obj.code == '') {
				obj.$api.msg('请输入验证码');
				return;
			}

			loginMobile({
				phone: obj.phone, //账号
				captcha: obj.code
			})
				.then(function(e) {
					uni.setStorageSync('token', e.data.token);
					getUserInfo({}).then(e => {
						obj.login();
						// 保存返回用户数据
						obj.setUserInfo(e.data);
						//成功跳转首页
						uni.switchTab({
							url: '/pages/index/index'
						});
					});
				})
				.catch(e => {
					console.log(e);
				});
		},
		isEmailAvailable(obj) {
			let email = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
			if (!email.test(obj)) {
				return false;
			}
			return true;
		},
		//发送验证码
		verification() {
			let obj = this;
			if (obj.email == '') {
				return obj.$api.msg(this.$t('login.qsryx'));
			}
			if (!obj.isEmailAvailable(obj.email)) {
				return obj.$api.msg(this.$t('login.qsrzqdyx'));
			}
			// 判断是否在倒计时
			if (obj.countDown > 0) {
				return false;
			} else {
				obj.countDown = 60;
				obj.time = setInterval(() => {
					obj.countDown--;
				}, 1000);
				//调用验证码接口
				getForget({
					email: obj.email
					// type: 'login'
				})
					.then(({ data }) => {})
					.catch(err => {
						console.log(err);
					});
			}
		},
		login() {
			//返回登录
			uni.navigateTo({
				url: '/pages/public/login'
			});
		}
	}
};
</script>

<style lang="scss">
page {
	height: 100%;
	background-color: #fff;
	font-size: 32rpx;
}

.container {
	width: 100%;
	height: 100%;
	background-size: 100%;
}

.shuru {
	display: block;
	background-color: #f5f5f5;
	width: 673rpx;
	height: 95rpx;
	margin: auto;
	padding-left: 30rpx;
	text-align: 95rpx;
}

.tit {
	padding: 40rpx 48rpx 20rpx;
}

.gologin {
	width: 204rpx;
	height: 98rpx;
	line-height: 98rpx;
	background-color: #fcd535;
	text-align: center;
	border-radius: 20rpx;
}

.wjmm {
	color: #fcd535;
}

.yzm {
	width: 400rpx;
	height: 95rpx;
	padding-left: 30rpx;
	background-color: #f5f5f5;
}

.yzmbtn {
	width: 234rpx;
	height: 95rpx;
	background-color: #f5f5f5;
	text-align: center;
	line-height: 95rpx;
	color: #fcd535;
}
</style>
