<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.username" 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: {
					username: "",
					password: "",
					autologin: false
				},
				rules: {
					username: [
						{required: true, message: this.$t('login.userError'), trigger: 'blur'}
					],
					password: [
						{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
					]
				},
				islogin: false
			}
		},
		mounted() {
			this.rememberMe()
		},
		methods: {
			rememberMe() {
				const data = this.$TOOLS.data.get("ACCOUNT")
				if (data) {
					this.form.username = data.username
					this.form.password = data.password
					this.form.autologin = true
				}
			},
			async login(){
				const validate = await this.$refs.loginForm.validate().catch(() => {});
				if(!validate){ return false }
				this.islogin = true
				const data = {
					username: this.form.username,
					password: this.$TOOLS.crypto.MD5(this.form.password)
				};
				//获取token
				var [res, err] = await this.$TOOLS.go(this.$API.auth.token(data))
				if(err){
					this.islogin = false
					this.$message.warning(err.msg)
					return false
				}
				// 登录成功
				this.$TOOLS.data.set("TOKEN", res.data.token)
				// 记住密码
				if (this.form.autologin) {
					this.$TOOLS.data.set("ACCOUNT", {
						username: this.form.username,
						password: this.form.password
					})
				}
				// 获取账号信息
				await this.getInfo()
				// 获取菜单信息
				await this.getMenu()
			},
			async getInfo() {
				const res = await this.$API.auth.info()
				this.$TOOLS.data.set("USER_INFO", res.data)
			},
			async getMenu() {
				const res = await this.$API.auth.menu()
				if(res.data.menus.length===0){
					this.islogin = false
					this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
						type: 'error',
						center: true
					})
					return false
				}
				this.$TOOLS.data.set("MENU", this.makeMenu(res.data.menus, 0))
				this.$TOOLS.data.set("PERM", res.data.buttons)
				this.$TOOLS.data.set("ROLE", res.data.roles)
				// 登录成功跳转首页
				this.$router.replace({path: '/'})
				this.$message.success("登录成功")
				this.islogin = false
			},
			makeMenu(menus, parent_id = 0) {
				const arr = [];
				for (let item of menus) {
					if(item.parent_id === parent_id){
						// 数据格式处理
						const tmp = {
							name: item['name'],
							path: '/' + item['path'],
							component: item['path'],
							meta: {
								'title': item['title'],
								'icon': item['icon'],
								'hidden': item['hidden'],
								'type': 'menu'
							}
						};
						const children = this.makeMenu(menus, item.menu_id);
						if (children.length > 0) {
							tmp['children'] = children
						}
						arr.push(tmp)
					}
				}
				return arr
			}
		}
	}
</script>

<style>
</style>
