<template>
	<div class="login-container" @wheel.prevent="handleScroll">
		<div class="login-left">
			<div class="login-left-tbig">欢迎登陆</div>
			<div class="login-left-tlittle">靖安乡村民宿管理端</div>
		</div>
		<div class="login-right">
			<div class="login-right-ttop">靖安乡村民宿</div>
			<div class="login-right-tbottom">管理端</div>
			<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
				<el-form-item prop="username">
					<span class="svg-container">
						<svg-icon icon-class="login_home" />
					</span>
					<el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text" tabindex="1" auto-complete="off" />
				</el-form-item>
				<el-form-item prop="password">
					<span class="svg-container">
						<svg-icon icon-class="password" />
					</span>
					<el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" name="password"
						tabindex="2" auto-complete="off" @keyup.enter.native="handleLogin" />
					<span class="show-pwd" @click="showPwd">
						<svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
					</span>
				</el-form-item>
				<el-button :loading="loading" type="primary" class="btn_submit" @click.native.prevent="handleLogin">登 录</el-button>
			</el-form>
			<div class="ICP">
				<a href="https://beian.mps.gov.cn/#/query/webSearch" target="_blank" class="icpa">赣公网安备36092502000028号</a>
				<a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank" class="icpa">赣ICP备2023009877号-2</a>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		login
	} from '@/api/user'

	export default {
		name: 'Login',
		data() {
			const validateUsername = (rule, value, callback) => {
				if (!value) {
					this.$message.error('请输入账号')
					callback(new Error('请输入账号'))
				} else {
					callback()
				}
			}
			const validatePassword = (rule, value, callback) => {
				if (value.length < 6) {
					this.$message.error('请输入密码，不小于6位')
					callback(new Error('请输入密码，不小于6位'))
				} else {
					callback()
				}
			}
			return {
				loginForm: {
					username: '',
					password: ''
				},
				loginRules: {
					username: [{
						required: true,
						trigger: 'blur',
						validator: validateUsername
					}],
					password: [{
						required: true,
						trigger: 'blur',
						validator: validatePassword
					}]
				},
				loading: false,
				passwordType: 'password',
				redirect: undefined
			}
		},
		mounted() {
			this.$refs.username.focus();
		},
		watch: {
			$route: {
				handler: function(route) {
					this.redirect = route.query && route.query.redirect
				},
				immediate: true
			}
		},
		methods: {
			handleScroll(event) {
				// 在这里可以添加其他逻辑，比如判断是否按下了Ctrl键
				if (event.ctrlKey) {
					event.preventDefault();
				}
			},
			showPwd() {
				if (this.passwordType === 'password') {
					this.passwordType = ''
				} else {
					this.passwordType = 'password'
				}
				this.$nextTick(() => {
					this.$refs.password.focus()
				})
			},
			/**
			 * 登录
			 */
			handleLogin() {
				this.$refs.loginForm.validate(valid => {
					if (valid) {
						this.loading = true
						this.$store.dispatch('user/login', this.loginForm)
							.then((res) => {
								// console.log(res);
								if (res.code == 200) {
									if (res.data.level == 2) {
										this.$router.push({
											path: this.redirect || '/'
										});
									} else {
										this.$router.replace({
											path: '/'
										});
									}
									this.$message.success(res.message);
								} else {
									this.$message.error(res.message);
								}
								this.loading = false;
							}).catch((err) => {
								// console.log(err);
								this.$message.error(err.message);
								this.loading = false;
							});
					} else {
						// this.$message.error('请输入账号或密码！');
						return false;
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		display: flex;


		.login-left {
			width: 40%;
			height: 100vh;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			background: url('../../icons/images/login/login_bg.png') no-repeat;
			background-size: cover;
			box-sizing: border-box;

			.login-left-tbig {
				left: 231px;
				top: 445px;
				width: 320px;
				height: 116px;
				opacity: 1;
				/** 文本1 */
				font-size: 80px;
				font-weight: 500;
				letter-spacing: 0px;
				line-height: 115.84px;
				color: rgba(255, 255, 255, 1);
				text-align: left;
				vertical-align: top;
			}

			.login-left-tlittle {
				left: 218px;
				top: 563px;
				width: 360px;
				height: 58px;
				opacity: 1;
				/** 文本1 */
				font-size: 40px;
				font-weight: 500;
				letter-spacing: 0px;
				line-height: 57.92px;
				color: rgba(255, 255, 255, 1);
				text-align: left;
				vertical-align: top;
			}
		}

		.login-right {
			width: 60%;
			height: 100vh;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			box-sizing: border-box;

			.login-right-ttop {
				width: 300px;
				height: 73px;
				opacity: 1;
				font-size: 50px;
				font-weight: 700;
				letter-spacing: 0px;
				line-height: 72.4px;
				color: rgba(0, 0, 0, 1);
				margin-bottom: 20px;
			}

			.login-right-tbottom {
				width: 240px;
				height: 116px;
				opacity: 1;
				font-size: 80px;
				font-weight: 700;
				letter-spacing: 0px;
				line-height: 115.84px;
				color: rgba(9, 101, 98, 1);
				margin-bottom: 60px;
			}

			.login-form {
				width: 500px;
				max-width: 100%;
				padding: 0 5px;
				overflow: hidden;
				display: flex;
				flex-direction: column;
				align-items: center;

				.svg-container {
					margin-top: -100px;
					font-size: 22px;
					display: inline-block;
				}

				.show-pwd {
					font-size: 20px;
				}

				.btn_submit {
					width: 408px;
					height: 56px;
					opacity: 1;
					border-radius: 56px;
					background: rgba(9, 101, 98, 1);
					font-size: 24px;
					font-weight: 400;
					letter-spacing: 0px;
					line-height: 33.6px;
					color: rgba(255, 255, 255, 1);
					vertical-align: top;
					margin: 30px 0;
				}
			}

			.ICP {
				position: fixed;
				bottom: 30px;

				.icpa {
					margin: 50px 10px;
					color: rgba(0, 0, 0, 0.58);
				}
			}
		}

		.el-input {
			display: inline-block;
			width: 85%;

			input {
				background: transparent;
				border: 0;
				-webkit-appearance: none;
				border-radius: 0px;
				padding: 12px 5px 12px 15px;
				font-size: 20px;

				&:-webkit-autofill {
					box-shadow: 0 0 0px 1000px #283443 inset !important;
					-webkit-text-fill-color: #fff !important;
				}
			}
		}

		.el-form-item {
			width: 400px;
			border: 2px solid rgba(238, 238, 238, 1);
			padding: 0 0 0 20px;
		}
	}
</style>