<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="user">
			<el-input v-model="form.account" prefix-icon="el-icon-user" clearable :placeholder="$t('login.userPlaceholder')">
			</el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input v-model="form.password" prefix-icon="el-icon-lock" clearable show-password :placeholder="$t('login.PWPlaceholder')"></el-input>
		</el-form-item>
		<!-- <el-form-item style="margin-bottom: 10px;">
				<el-col :span="12">
					<el-checkbox :label="$t('login.rememberMe')" v-model="form.autologin"></el-checkbox>
				</el-col>
				<el-col :span="12" class="login-forgot">
					<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>
				</el-col>
		</el-form-item> -->
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
		<div class="login-reg">
			<!-- {{$t('login.noAccount')}} <router-link to="/user_register">{{$t('login.createAccount')}}</router-link> -->
		</div>
	</el-form>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					account: "",
					password: "",
					autologin: true
				},
				rules: {
					account: [
						{required: true, message: this.$t('login.userError'), trigger: 'blur'}
					],
					password: [
						{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
					]
				},
				islogin: false,
			}
		},
		watch:{

		},
		mounted() {

		},
		methods: {
			async login(){

				var validate = await this.$refs.loginForm.validate().catch(()=>{})
				if(!validate){ return false }

				this.islogin = true

				var data = {
					account: this.form.account,
					password: this.$tool.crypto.BASE64.encrypt( this.$tool.crypto.BASE64.encrypt( this.form.password ) + this.$tool.crypto.BASE64.encrypt( this.form.account ) )
				}

				//获取token
				var user = await this.$api.auth.token.post(data)
				this.islogin = false
				if(user.code !== 200){
					this.$message.warning(user.message)
					return false
				}

				this.$tool.cookie.set(this.$config.SESSIONNAME, user.data.token, {
					expires: 86400*360
				})
				
				//获取菜单
				// var menu = null
				// menu = await this.$api.system.index.get()

				// if(menu.code == 200){

					// if(menu.data.menu.length==0){
					// 	this.islogin = false
					// 	this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
					// 		type: 'error',
					// 		center: true
					// 	})
					// 	return false
					// }

					// for (const key in menu.data) {
					//     if (Object.prototype.hasOwnProperty.call(menu.data, key)) {
					//         this.$tool.data.set(key, menu.data[key]);
					//     }
					// }
					// 
					// 

					// 获取登录前的URL
					const redirect = this.$route.query.redirect || '/'
					this.$router.replace({
						path: redirect
					})
					
					this.$message.success("Login Success 登录成功")
					this.islogin = false
					return false

				// }
				// this.islogin = false
				// this.$message.warning(menu.message)
				// return false
			},
		}
	}
</script>

<style>
</style>
