<template>
	<view class="container">
		<view class="wjmm" style="text-align: right;padding-right: 30rpx;font-size: 36rpx;" @click="zc()">
			注冊
		</view>
		<view class="tit" style="padding-top:120rpx;">
			郵箱
		</view>
		<input type="text" class="shuru" placeholder="請翰入郵箱" v-model="email">
		<view class="tit">
			密碼
		</view>
		<input type="password" class="shuru" placeholder="請翰入密碼" v-model="lpwd" >
		<view class="" style="padding:20rpx 48rpx;">
			<view class="wjmm" @click="wjmm()">
				忘記密碼?
			</view>
		</view>
		<view class="flex" style="justify-content: flex-end;padding: 0 48rpx;">
			<view class="gologin" @click="gologin">
				登錄
			</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() {
			
		},
		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('請輸入正確的郵箱！')
				}
				obj.loadingType = 'loading'
				gologin({
					email: obj.email,
					lpwd: obj.lpwd
				}).then(res => {
					console.log(res)
					obj.loadingType = 'no'
					uni.showToast({
						title: '登錄成功',
						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>
