<template>
	<view class="container">
		<view class="wjmm" style="text-align: right;padding-right: 30rpx;font-size: 36rpx;" @click="zc()">{{ $t('login.zc') }}</view>
		<view class="tit" style="padding-top:120rpx;">{{ $t('login.yx') }}</view>
		<input type="text" class="shuru" :placeholder="$t('login.qsryx')" v-model="email" />
		<view class="tit">{{ $t('login.mm') }}</view>
		<input type="password" class="shuru" :placeholder="$t('login.qsrmm')" v-model="lpwd" />
		<view class="" style="padding:20rpx 48rpx;">
			<view class="wjmm" @click="wjmm()">{{ $t('login.wjmm') }}?</view>
		</view>
		<view class="flex" style="justify-content: flex-end;padding: 0 48rpx;">
			<view class="gologin" @click="gologin">{{ $t('login.dl') }}</view>
		</view>
	</view>
</template>

<script>
import { mapMutations, mapState } from 'vuex';
import { loginWx, gologin, getUser } from '@/api/login.js';
export default {
	data() {
		return {
			email: '',
			lpwd: ''
		};
	},
	onLoad() {
		uni.setNavigationBarTitle({ title: this.$t('login.dl') });
	},
	methods: {
		...mapMutations('user', ['login', 'setUserInfo']),
		zc() {
			uni.navigateTo({
				url: '/pages/public/register'
			});
		},
		wjmm() {
			uni.navigateTo({
				url: '/pages/public/forget'
			});
		},
		gologin() {
			let obj = this;
			if (obj.loadingType == 'loading') {
				return;
			}
			if (!obj.isEmailAvailable(obj.email)) {
				return obj.$api.msg(this.$t('login.qsrzqdyx'));
			}
			obj.loadingType = 'loading';
			gologin({
				email: obj.email,
				lpwd: obj.lpwd
			})
				.then(res => {
					console.log(res);
					obj.loadingType = 'no';
					uni.showToast({
						title: this.$t('login.dlcg'),
						duration: 2000,
						position: 'top'
					});
					getUser().then(re => {
						obj.login();
						obj.setUserInfo(re.data);
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							});
						}, 1500);
					});
				})
				.catch(err => {
					console.log(err);
					obj.loadingType = 'no';
				});
		},

		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;
		}
	}
};
</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: 60rpx 48rpx 20rpx;
}
.gologin {
	width: 204rpx;
	height: 98rpx;
	line-height: 98rpx;
	background-color: #fcd535;
	text-align: center;
	border-radius: 20rpx;
}
.wjmm {
	color: #fcd535;
}
</style>
