<template>
	<div class="login">
		<h1>ByteSailing</h1>
		<div class="content">
			<div class="warp-bg">
				<div class="left-bg">
					<img src="../assets/images/login-img.png" alt="" />
				</div>
				<div class="right-bg">
					<el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
						<div class="form" style="margin-top:300px">
							<div class="">欢迎登录~</div>
							<h2>ByteSailing 管理系统</h2>
							<el-form-item prop="tenantId" style="margin-top:30px">
								<el-input
									placeholder="请输入租户号"
									prefix-icon="user"
									@keyup.enter="handleLogin"
									v-model="loginForm.tenantId">
									
								</el-input>
							</el-form-item>
							<el-form-item prop="username" style="margin-top:30px">
								<el-input
									placeholder="请输入账号"
									prefix-icon="UserFilled"
									@keyup.enter="handleLogin"
									v-model="loginForm.username">
								</el-input>
							</el-form-item>
							<el-form-item prop="password" style="margin-top:30px">
								<el-input
									placeholder="请输入密码"
									prefix-icon="View"
									type="password"
									v-model="loginForm.password"
									@keyup.enter="handleLogin">
								</el-input>
							</el-form-item>
							<!-- <el-form-item prop="code" class="code" style="margin-top:30px">
								<el-input
									placeholder="请输入验证码"
									prefix-icon="ChatSquare"
									v-model="loginForm.code"
									@keyup.enter="handleLogin">
								</el-input>
								<img :src="codeUrl" @click="getCode" class="login-code-img"/>
							</el-form-item> -->
							<div  style="margin-top:30px">
								<el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
							</div>
							<el-button type="primary"  @click="handleLogin" style="width:100%;margin-top:30px">登录</el-button>
						</div>
					
					</el-form>
					
				</div>
			</div>
		</div>
	</div>
</template>

<script setup>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
const router = useRouter()
const { proxy } = getCurrentInstance()

const loginForm = ref({
	username: '',
	password: '',
	rememberMe: false,
	tenantId:'',
	code: '',
	uuid: '',
})

const loginRules = {
	username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
	password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
	code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
}

const codeUrl = ref('')
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(true)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)

function handleLogin() {
	proxy.$refs.loginRef.validate((valid) => {
		if (valid) {
			loading.value = true
			// 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
			if (loginForm.value.rememberMe) {
				Cookies.set('username', loginForm.value.username, {
					expires: 30,
				})
				Cookies.set('tenantId', loginForm.value.tenantId, {
					expires: 30,
				})
				Cookies.set('password', encrypt(loginForm.value.password), {
					expires: 30,
				})
				Cookies.set('rememberMe', loginForm.value.rememberMe, {
					expires: 30,
				})
			} else {
				// 否则移除
				Cookies.remove('tenantId')
				Cookies.remove('username')
				Cookies.remove('password')
				Cookies.remove('rememberMe')
			}
			// 调用action的登录方法
			userStore
				.login(loginForm.value)
				.then(() => {
					router.push({ path: redirect.value || '/' })
				})
				.catch(() => {
					loading.value = false
					// 重新获取验证码
					if (captchaEnabled.value) {
						getCode()
					}
				})
		}
	})
}

function getCode() {
	getCodeImg().then((res) => {
		captchaEnabled.value =
			res.captchaEnabled === undefined ? true : res.captchaEnabled
		if (captchaEnabled.value) {
			codeUrl.value = 'data:image/gif;base64,' + res.img
			loginForm.value.uuid = res.uuid
		}
	})
}

function getCookie() {
	const username = Cookies.get('username')
	const password = Cookies.get('password')
	const rememberMe = Cookies.get('rememberMe')
	const tenantId = Cookies.get('tenantId')
	loginForm.value = {
		username: username === undefined ? loginForm.value.username : username,
		password:
			password === undefined
				? loginForm.value.password
				: decrypt(password),
		rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
		tenantId: tenantId === undefined ? loginForm.value.tenantId : tenantId,
	}
}

getCode()
getCookie()
</script>

<style lang="scss">
.login{
	.warp-bg{
		background: #fff;
		height: 100vh;
		width: 100vw;
		overflow: hidden;
		.left-bg{
			width: calc(50vw + 200px);
			background: #0084FF;
			height: 100vh;
			float: left;
			overflow: hidden;
			img{
				float: right;
				margin-right: 75px;
				margin-top: 120px;
			}
		}
		.right-bg{
			width: calc(50vw - 200px);
			background: url(../assets/images/login-right-bg.png) no-repeat;
			background-size: cover;
			height: 100vh;
			float: right;
			overflow: hidden;
			.form{
				width: 500px;
				padding:0 75px;
				box-sizing: border-box;
				.el-input{
					input{
						border:none;
						border-bottom: 1px solid #DDDDDD;
					}
				}
				.el-input__wrapper{
					box-shadow:0 0 0 0!important;
				}
				.code{
					position: relative;
					img{
						position: absolute;
						right: 0;
						bottom: 0;
					}
				}
			}
		}
	}
	h1{
		position: fixed;
		left: 40px;
		top: 0;
		height: 96px;
		line-height: 96px;
		font-size: 26px;
		color: #fff;
	}
	
}
</style>
