<template>
	<el-form ref="loginForm" @submit.prevent="login" :model="form" :rules="rules" label-width="0" size="large">
		<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>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
	</el-form>
</template>

<script>
	import menuData from '@/mock/menu.json'
	export default {
		data() {
			return {
				userType: 'admin',
				form: {
					password: "",
					autologin: false
				},
				rules: {
					password: [
						{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
					]
				},
				islogin: false,
			}
		},
		watch:{
		},
		mounted() {

		},
		methods: {
			handleEnter(event){
				event.preventDefault(); // 阻止默认的表单提交行为
				this.login();
			},
			async login(){

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

				this.islogin = true
				var data = {
					password: this.$TOOL.crypto.MD5(this.form.password)
				}
				//获取token
				var user = await this.$API.auth.token.post(data)
				if(user.code == 200){
					this.$TOOL.cookie.set("TOKEN", user.data.tokenValue, {
						expires: this.form.autologin? 24*60*60 : 0
					})
				}else{
					this.islogin = false
					this.$message.warning(user.message)
					return false
				}
				//获取菜单
				var menu = menuData
				console.log(menu);
				this.$TOOL.data.set("MENU", menu)
				this.$TOOL.data.set("PERMISSIONS", null)
				this.$TOOL.data.set("DASHBOARDGRID", null)
				this.$router.replace({
					path: '/'
				})
				this.$message.success("Login Success 登录成功")
				this.islogin = false
			},
		}
	}
</script>

<style>
</style>
