<template>
	<div class="login-bg">
		<!-- <div class="logoNameBox"><span class="plat-name">承揽时代后台管理</span></div> -->
		<video autoplay muted loop class="backgroundVideo">
			<source :src="video" type="video/mp4" />
		</video>
		<div class="login-form">
			<div class="dicText">岑溪人民医院随访系统</div>
			<el-form ref="loginForm" :model="loginForm" :rules="loginRules">
				<el-form-item prop="username">
					<el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" class="intput"/>
				</el-form-item>
				<el-form-item prop="password">
					<el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" show-password/>
				</el-form-item>
				<el-form-item prop="code" v-if="captchaEnabled">
					<el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 62%" @keyup.enter.native="handleLogin" />
					<div class="login-code"><img :src="codeUrl" @click="getCode" class="login-code-img" /></div>
				</el-form-item>
				<el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px">记住密码</el-checkbox>
			  <span class="forget" @click="forget()">忘记密码</span>
      </el-form>
			<el-button type="primary" class="login-form-btn" @click.native.prevent="handleLogin">登 录</el-button>
		</div>
		<div class="footer"></div>
	</div>
</template>

<script>
import { getCodeImg } from '@/api/login';
import Cookies from 'js-cookie';
import { encrypt, decrypt } from '@/utils/jsencrypt';
import video from '@/assets/images/loginbg_1920_B800.9360a14.mp4';
export default {
	name: 'Login',
	data() {
		return {
			video,
			codeUrl: '',
			loginForm: {
				username: '',
				password: '',
				rememberMe: false,
				code: '',
				uuid: ''
			},
			loginRules: {
				username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
				password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
				code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
			},
			loading: false,
			// 验证码开关
			captchaEnabled: true,
			// 注册开关
			register: false,
			redirect: undefined
		};
	},
	watch: {
		$route: {
			handler: function (route) {
				this.redirect = route.query && route.query.redirect;
			},
			immediate: true
		}
	},
	created() {
		this.getCode();
		this.getCookie();
	},
	methods: {
		getCode() {
			getCodeImg().then(res => {
				this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
				if (this.captchaEnabled) {
					this.codeUrl = 'data:image/gif;base64,' + res.img;
					this.loginForm.uuid = res.uuid;
				}
			});
		},
		getCookie() {
			const username = Cookies.get('username');
			const password = Cookies.get('password');
			const rememberMe = Cookies.get('rememberMe');
			this.loginForm = {
				username: username === undefined ? this.loginForm.username : username,
				password: password === undefined ? this.loginForm.password : decrypt(password),
				rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
			};
		},
    forget(){

    },
		handleLogin() {
			this.$refs.loginForm.validate(valid => {
				if (valid) {
					this.loading = true;
					if (this.loginForm.rememberMe) {
						Cookies.set('username', this.loginForm.username, { expires: 30 });
						Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 });
						Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
					} else {
						Cookies.remove('username');
						Cookies.remove('password');
						Cookies.remove('rememberMe');
					}
					this.$store
						.dispatch('Login', this.loginForm)
						.then(() => {
							this.$router.push({ path: this.redirect || '/' }).catch(() => {});
						})
						.catch(() => {
							this.loading = false;
							if (this.captchaEnabled) {
								this.getCode();
							}
						});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.login-bg {
	width: 100%;
	min-width: 1280px;
	position: relative;
	height: 100%;
	overflow-y: hidden;
	color: #2c3034;
	overflow: hidden;
	.logoNameBox {
		position: absolute;
		top: 44px;
		left: 40px;
		font-size: 26px;
		font-weight: 700;
		color: #1c2a39;
		z-index: 11;
	}
	.backgroundVideo {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		object-fit: cover;
	}
}
.top-logo {
	width: 173px;
	height: 78px;
	position: absolute;
	top: 2%;
	left: 14%;
}
.login-form {
	width: 400px;
	padding: 0px 30px 78px 30px;
	background-color: #fff;
	box-shadow: 0 6px 12px 0 rgba(0, 0, 0, 0.1);
	border-radius: 4px;
	position: absolute;
	top: 20%;
	left: 65%;
	.dicText {
		font-size: 22px;
		line-height: 30px;
		color: #1c79f4;
		text-align: left;
		font-weight: 700;
		margin-top: 25px;
		margin-bottom: 40px;
	}
	.el-form-item__label {
		font-weight: bold;
		font-size: 17px;
		padding-bottom: 2px;
	}
	.el-input__inner {
		height: 40px;
	}
	.login-code {
		width: 33%;
		float: right;
		text-align: right;
		img {
			cursor: pointer;
			vertical-align: middle;
			border-radius: 3px;
		}
	}
	.login-code-img {
		height: 40px;
	}
	button {
		width: 100%;
		height: 48px;
		background-color: #1c79f4;
		border-radius: 4px;
		color: #fff;
	}
  .forget{
    float: right;
    font-size: 14px;
    color: #1c79f4;
    cursor: pointer;
  }
}
.footer {
	position: fixed;
	bottom: 0;
	width: 100%;
	text-align: center;
	height: 68px;
	padding-top: 20px;
	color: #ffffff;
	line-height: 25px;
	padding: 12px;
	font-size: 12px;
}
</style>
