<template>
	<div class="login-wrapper">
		<div class="login-title">{{ sysBaseConfig.SNOWY_SYS_NAME }}</div>
		<div class="login_form">
			<div class="form_head">登 录</div>
			<div class="form_body">
				<a-form ref="loginForm" :model="ruleForm" :rules="rules">
					<!-- 用户名 -->
					<a-form-item name="account">
						<a-input v-model:value="ruleForm.account" placeholder="用户名" size="large" @keyup.enter="login"> </a-input>
					</a-form-item>

					<!-- 密码 -->
					<a-form-item name="password">
						<a-input-password
							v-model:value="ruleForm.password"
							placeholder="密码"
							size="large"
							autocomplete="off"
							@keyup.enter="login"
						>
						</a-input-password>
					</a-form-item>

					<!-- 验证码 -->
					<a-form-item name="validCode" v-if="captchaOpen === 'true'">
						<a-row :gutter="8">
							<a-col :span="17">
								<a-input v-model:value="ruleForm.validCode" placeholder="验证码" size="large" @keyup.enter="login">
								</a-input>
							</a-col>
							<a-col :span="7">
								<img :src="validCodeBase64" class="login-validCode-img" @click="loginCaptcha" />
							</a-col>
						</a-row>
					</a-form-item>

					<!-- 记住登录状态 -->
					<a-form-item>
						<a-checkbox v-model:checked="ruleForm.autologin">记住登录状态</a-checkbox>
					</a-form-item>

					<!-- 登录按钮 -->
					<a-form-item>
						<a-button type="primary" class="w-full" :loading="loading" round size="large" @click="login"
							>{{ $t('login.signIn') }}
						</a-button>
					</a-form-item>
				</a-form>
			</div>
			<!-- <div class="form_foot">没有账号?点击<router-link to="/findpwd" class="nav-link">注册</router-link></div> -->
		</div>
		<div class="version">
			{{ sysBaseConfig.SNOWY_SYS_COPYRIGHT }}
		</div>
	</div>
</template>
<script setup>
	import loginApi from '@/api/auth/loginApi'
	const PhoneLoginForm = defineAsyncComponent(() => import('./phoneLoginForm.vue'))
	import ThreeLogin from './threeLogin.vue'
	import smCrypto from '@/utils/smCrypto'

	import { required } from '@/utils/formRules'
	import { afterLogin } from './util'
	import configData from '@/config'
	import configApi from '@/api/dev/configApi'
	import tool from '@/utils/tool'
	import { globalStore, iframeStore, keepAliveStore, viewTagsStore } from '@/store'

	const { proxy } = getCurrentInstance()

	const activeKey = ref('userAccount')
	const captchaOpen = ref(configData.SYS_BASE_CONFIG.SNOWY_SYS_DEFAULT_CAPTCHA_OPEN)

	const validCodeBase64 = ref('')
	const loading = ref(false)

	const ruleForm = reactive({
		account: 'superAdmin',
		password: '123456',
		validCode: '',
		validCodeReqNo: '',
		autologin: false
	})

	const rules = reactive({
		account: [required('请输入用户名', 'blur')],
		password: [required(proxy.$t('login.PWError'), 'blur')]
	})
	const lang = ref([
		{
			name: '简体中文',
			value: 'zh-cn'
		},
		{
			name: 'English',
			value: 'en'
		}
	])
	const config = ref({
		lang: tool.data.get('APP_LANG') || proxy.$CONFIG.LANG,
		theme: tool.data.get('APP_THEME') || 'default'
	})

	const store = globalStore()
	const kStore = keepAliveStore()
	const iStore = iframeStore()
	const vStore = viewTagsStore()

	const setSysBaseConfig = store.setSysBaseConfig
	const clearKeepLive = kStore.clearKeepLive
	const clearIframeList = iStore.clearIframeList
	const clearViewTags = vStore.clearViewTags

	const sysBaseConfig = computed(() => {
		return store.sysBaseConfig
	})

	onMounted(() => {
		let formData = ref(configData.SYS_BASE_CONFIG)
		getAutologin() //记住登录状态

		configApi
			.configSysBaseList()
			.then((data) => {
				if (data) {
					data.forEach((item) => {
						formData.value[item.configKey] = item.configValue
					})

					captchaOpen.value = formData.value.SNOWY_SYS_DEFAULT_CAPTCHA_OPEN
					tool.data.set('SNOWY_SYS_BASE_CONFIG', formData.value)
					setSysBaseConfig(formData.value)
					refreshSwitch()
				}
			})
			.catch(() => {})
	})

	onBeforeMount(() => {
		clearViewTags()
		clearKeepLive()
		clearIframeList()
	})

	// 监听语言
	watch(
		() => config.value.lang,
		(newValue) => {
			proxy.$i18n.locale = newValue
			tool.data.set('APP_LANG', newValue)
		}
	)
	// 主题
	watch(
		() => config.value.theme,
		(newValue) => {
			document.body.setAttribute('data-theme', newValue)
		}
	)

	//记住状态
	const getAutologin = () => {
		let account = tool.data.get('account')
		let password = tool.data.get('password')
		let autologin = tool.data.get('autologin')

		ruleForm.account = account === undefined ? ruleForm.account : account
		ruleForm.password = password === undefined ? ruleForm.password : password
		ruleForm.autologin = autologin === undefined ? ruleForm.account : autologin
	}
	// 通过开关加载内容
	const refreshSwitch = () => {
		// 判断是否开启验证码
		if (captchaOpen.value === 'true') {
			// 加载验证码
			loginCaptcha()
			// 加入校验
			rules.validCode = [required(proxy.$t('login.validError'), 'blur')]
		}
	}
	// 获取验证码
	const loginCaptcha = () => {
		loginApi.getPicCaptcha().then((data) => {
			validCodeBase64.value = data.validCodeBase64
			ruleForm.validCodeReqNo = data.validCodeReqNo
		})
	}
	//登陆
	const loginForm = ref()
	const login = async () => {
		loginForm.value
			.validate()
			.then(async () => {
				loading.value = true

				const loginData = {
					account: ruleForm.account,
					// 密码进行SM2加密，传输过程中看到的只有密文，后端存储使用hash
					password: smCrypto.doSm2Encrypt(ruleForm.password),
					validCode: ruleForm.validCode,
					validCodeReqNo: ruleForm.validCodeReqNo
				}

				//是否记住登录状态
				if (ruleForm.autologin) {
					tool.data.set('loginForm', ruleForm.account)
					tool.data.set('password', ruleForm.password)
					tool.data.set('autologin', ruleForm.autologin)
				} else {
					tool.data.remove('account')
					tool.data.remove('password')
					tool.data.remove('autologin')
				}

				// 获取token
				try {
					const loginToken = await loginApi.login(loginData)
					await afterLogin(loginToken)
				} catch (err) {
					loading.value = false
					if (captchaOpen.value === 'true') {
						loginCaptcha()
					}
				}
			})
			.catch(() => {})
	}
	const configLang = (key) => {
		config.value.lang = key
	}
	// logo链接
	const handleLink = (e) => {
		if (!sysBaseConfig.value.SNOWY_SYS_COPYRIGHT_URL) {
			e?.stopPropagation()
			e?.preventDefault()
		}
	}
</script>
<style lang="less" scoped>
	.login-wrapper {
		width: 100%;
		min-height: 800px;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-start;
		padding-top: 10%;
		background-image: url('/src/assets/images/login/logo_background.png');
		background-size: cover;
		background-position: center;
		position: relative;
		.login-title {
			font-size: 30px;
			margin-bottom: 30px;
			letter-spacing: 5px;
			font-weight: bold;

			color: #1f3253;
		}

		// 登录
		.login_form {
			width: 400px;
			padding: 0 0 30px;
			display: flex;
			flex-direction: column;
			align-items: center;
			border-radius: 5px;
			background-color: #fbfcfd;

			.form_head {
				font-size: 20px;
				height: 70px;
				line-height: 70px;
			}

			// 表单
			.form_body {
				width: 100%;
				padding: 0 30px;

				.w-full {
					background-color: #0052cc;
				}
			}

			// 注册
			.form_foot {
				font-size: 14px;
				.nav-link {
					text-decoration: none;
					color: #0052cc;
				}
				.nav-link:hover {
					text-decoration: underline;
				}
			}
		}

		.version {
			position: absolute;
			bottom: 10px;
			font-size: 13px;
		}
	}
</style>
