<template>
	<div class="login-wrapper">
		<div class="login-box">
			<!-- 头像容器 -->

			<!-- 表单容器 -->
			<div class="login-form-box">
				<div class="flex nav" v-if="currentIndex == 0 || currentIndex == 2">
					<div :class="{'action': 0 == currentIndex}" class="nav-item hand" @click="currentIndex = 0">登录</div>
					
					<div :class="{'action': 2 == currentIndex}" class="nav-item hand" @click="currentIndex = 2">代理入口</div>
				</div>
				<div v-else class="flex nav">
					<div  class="nav-item hand" @click="currentIndex = 1">注册</div>
				</div>
				<el-form label-width="0px" ref="loginFormRef" class="login-form" :model="loginForm" v-if="currentIndex == 0 ||currentIndex ==  2" >
					<!--用户名-->
					<el-form-item prop="account">
						<el-input placeholder="请输入用户名" prefix-icon="el-icon-s-custom"
							v-model="loginForm.username" @blur="accountGa"></el-input>
					</el-form-item>
					<!--密码-->
					<el-form-item prop="password">
						<el-input placeholder="请输入密码" prefix-icon='el-icon-s-goods' v-model="loginForm.password"
							type="password"></el-input>
					</el-form-item>
					<!--二次验证-->
					<el-form-item prop="password" v-if="isBinding == 1">
						<el-input placeholder="请输入二次验证" prefix-icon='el-icon-s-goods' v-model="loginForm.binding"
							type="text"></el-input>
					</el-form-item>
					<!--按钮-->
					<el-form-item class="btns">
						<span class="go-new hand" @click="currentIndex = 1" v-if="currentIndex == 0">没有账号？立即注册</span>
						<el-button type="primary" @click="login">登录</el-button>
						<!-- <el-button type="info" @click="resetLoginForm">重置</el-button> -->
					</el-form-item>
				</el-form>
				<el-form label-width="0px" ref="loginFormReg" class="login-form" :model="loginForm" v-if="currentIndex == 1">
					<!--用户名-->
					<el-form-item prop="account">
						<el-input placeholder="请输入用户名" prefix-icon="el-icon-s-custom"
							v-model="loginForm.username" type="text"></el-input>
					</el-form-item>
					<!-- <el-form-item prop="email">
						<el-input placeholder="请输入邮箱" prefix-icon="el-icon-message"
							v-model="loginForm.email" type="email"></el-input>
					</el-form-item> -->
					<!-- 注册码 -->
					<el-form-item prop="email">
						<el-input placeholder="请输入注册码" prefix-icon="el-icon-user"
							v-model="loginForm.code" type="text"></el-input>
					</el-form-item>
					<!--密码-->
					<el-form-item prop="password">
						<el-input placeholder="请输入密码" prefix-icon='el-icon-s-goods' v-model="loginForm.password"
							type="password"></el-input>
					</el-form-item>
					<el-form-item prop="repassword">
						<el-input placeholder="请重新输入密码" prefix-icon='el-icon-s-goods' v-model="loginForm.repassword"
							type="password"></el-input>
					</el-form-item>
					<!--按钮-->
					<el-form-item class="btns">
						<span class="go-new hand" @click="currentIndex = 0" v-if="currentIndex == 1">已有账号，返回登录</span>
						<el-button type="primary" @click="reg">注册</el-button>
						<!-- <el-button type="info" @click="resetLoginForm">重置</el-button> -->
					</el-form-item>
				</el-form>
			</div>
		</div>
	</div>
</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import { validateEmail} from '@/assets/js/tools.js'
	import {
		login,
		reg
	} from '@/request/agent.js'
	import { accountGa } from '@/request/user.js'
	export default {
		name: "Login",
		data() {
			return {
				
				isBinding: 0,
				currentIndex: 0,
				navList: [{
					tit: '登录',
					state: 0,
				},
				{
					tit: '注册',
					state: 1
				}],
				loginForm: {
					binding: '',
					email: '',
					code: '',
					username: '',
					repassword: '',
					password: ''
				}
			}
		},
		created() {
			let a = Date.now();
			this.loginForm.email = a + '@gmail.com'
		},
		methods: {
			...mapMutations(['setUserInfo']),
			accountGa() {
				let that = this
				if(that.loginForm.username) {
					accountGa({
						account: that.loginForm.username
					}).then(res => {
						console.log(res);
						that.isBinding = res.data.is_binding
					})
				}
			},
			resetLoginForm() {
				this.$refs.loginFormRef.resetFields()
			},
			reg() {
				let that = this
				if (that.loginForm.username == '') {
					return that.$msg.error('请输入用户名')
				}
				if(that.loginForm.email == '') {
					return that.$msg.error('请输入邮箱')
				}
				// if(!validateEmail(that.loginForm.email)) {
				// 	return that.$msg.error('请输入正确的邮箱')
				// }
				if (that.loginForm.code == '') {
					return that.$msg.error('请输入注册码')
				}
				if (that.loginForm.password == '') {
					return that.$msg.error('请输入密码')
				}
				if (that.loginForm.repassword == '') {
					return that.$msg.error('请再次输入密码')
				}
				if(that.loginForm.password != that.loginForm.repassword) {
					return  that.$msg.error('两次密码不一致')
				}
				reg(that.loginForm).then(res => {
					that.$msg.success('注册成功')
					that.currentIndex = 0
					that.setUserInfo(res.data.userinfo)
					window.localStorage.setItem('token', res.data.userinfo.token)
					window.localStorage.setItem('user', res.data.userinfo)
					that.$router.push("/home")
				}).catch(err => {
					console.log();
				})
			},
			login() {
				let that = this
				if (that.loginForm.account == '') {
					return that.$msg.error('请输入用户名')
				}
				if (that.loginForm.password == '') {
					return that.$msg.error('请输入密码')
				}
				login({
					account: that.loginForm.username,
					password: that.loginForm.password,
					code: that.loginForm.binding
				}).then(res => {
					that.$msg.success('登录成功')
					console.log(res);
					that.setUserInfo(res.data.userinfo)
					window.localStorage.setItem('token', res.data.userinfo.token)
					
					that.$router.push("/home")
				})
				// splist({
				// 	page: 1
				// })
				// console.log(login);

				// this.$router.push("/home")

			}
		}
	};
</script>

<style lang='scss' scoped>
/* 	body {
	} */
	.login-wrapper {
		/* background-color: ; */
		position: absolute;
		width: 100%;
		height: 100%;
		background-image: url(../assets/img/basebg.png);
		background-size:100% 100%;
	}

	.login-box {
		width: 450px;
		border-radius: 3px;
		display: flex;
		flex-direction: column;
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		margin: auto;
		align-items: center;
		justify-content: center;
	}

	.avatar-box {
		width: 130px;
		height: 130px;
		border: 1px solid #eee;
		border-radius: 50%;
		/* position: absolute; */
		box-shadow: 0 0 10px #ddd;
		align-self: center;
		transform: translate(0, -50%);
		background-color: #fff;
	}

	.avatar-box img {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.login-form-box {
		width: 450px;
		/* height: 200px; */
		justify-self: end;
		transform: translate(0, -10%);
		background-color: #fff;
		border-radius: 20px;
		.login-form {
			padding: 20px ;
		}
	}

	.btns {
		display: flex;
		justify-content: flex-end;
	}
	.nav {
		justify-content: center;
		font-size: 16px;
		color: #444;
		
		border-radius: 20px 20px 0 0;
		overflow: hidden;
		
	}
	.nav-item {
		/* width: 50%; */
		flex-grow: 1;
		position: relative;
		text-align: center;
		line-height: 50px;
		/* width: 64px;
		height: ; */
	}
	.action {
		font-weight: bold;
		color: black;
		background-color: #056de8;
		color: #fff;
		&::after {
			content: '';
			position: absolute;
			width: 100%;
			height: 3px;
			
			bottom: 0px;
			left: 0;
			right: 0;
			margin: auto;
		}
	}
	.go-new {
		padding-right: 20px;
		color:  #056de8;
	}
</style>