<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="username">
			<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 v-model="form.autologin" :label="$t('login.rememberMe')"></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>
import useStore from '@/store'
const { user } = useStore()

export default {
	data() {
		return {
			form: {
				username: 'admin',
				password: '100607',
				autologin: false
			},
			rules: {
				username: [{ required: true, message: this.$t('login.userError'), trigger: 'blur' }],
				password: [{ required: true, message: this.$t('login.PWError'), trigger: 'blur' }]
			},
			loading: false
		}
	},
	mounted() {},
	methods: {
		async login() {
			let validate = await this.$refs.loginForm.validate().catch(() => {})
			if (!validate) {
				return false
			}
			this.loading = true
			console.log(this.form);
			user.login(this.form).finally(() => {
				this.loading = false
				this.$message.success('Login Success 登录成功')
			})
		}
	},
	watch: {
		'$route.query': {
			handler(query) {
				user.$patch({
					loginQuery: query
				})
			},
			immediate: true
		}
	},
}
</script>

<style></style>
