<route lang="yaml">
meta:
  enabled: false
whiteList: true
title: 登录
constant: true
layout: false
</route>

<script lang="ts" setup name="Login">
import type { FormInstance, FormRules } from 'element-plus'

import { useI18n } from 'vue-i18n'
import useUserStore from '@/store/modules/user'
import storage from '@/utils/storage'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const { t } = useI18n()

const banner = new URL('../../assets/images/login-banner.png', import.meta.url).href
const title = import.meta.env.VITE_APP_TITLE

// 表单类型，login 登录，register 注册，reset 重置密码
const formType = ref('login')
const loading = ref(false)
const redirect = ref(route.query.redirect?.toString() ?? '/')

// 登录
const loginFormRef = ref<FormInstance>()
const loginForm = ref({
  username: storage.local.get('username') || '',
  password: '',
  remember: storage.local.has('username'),
})

const loginRules = ref<FormRules>({
  username: [
    { required: true, trigger: 'blur', message: '请输入用户名' },
  ],
  password: [
    { required: true, trigger: 'blur', message: '请输入密码' },
    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' },
  ],
})

function onLogin() {
  loginFormRef.value && loginFormRef.value.validate(async (valid) => {
    if (valid) {
      await userStore.login(loginForm.value.username, loginForm.value.password)
      if (loginForm.value.remember) {
        storage.local.set('username', loginForm.value.username)
      }
      else {
        storage.local.remove('username')
      }
      await router.push(redirect.value)
    }
  })
}
</script>

<template>
  <div>
    <i18n-selector class="i18n-selector">
      <el-icon>
        <svg-icon name="i-ri:translate" />
      </el-icon>
    </i18n-selector>
    <div id="login-box">
      <div class="login-banner">
        <div class="logo" />
        <img :src="banner" class="banner" alt="">
      </div>

      <el-form v-show="formType === 'login'" ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
        <div class="title-container">
          <h3 class="title">
            欢迎来到 {{ title }} ! 👋🏻
          </h3>
        </div>

        <div>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" :placeholder="t('app.username')" type="text" tabindex="1">
              <template #prefix>
                <el-icon>
                  <svg-icon name="i-ri:user-3-fill" />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" :placeholder="t('app.password')" tabindex="2" autocomplete="new-password" show-password @keyup.enter="onLogin">
              <template #prefix>
                <el-icon>
                  <svg-icon name="i-ri:lock-2-fill" />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
        </div>

        <div class="flex-bar">
          <el-checkbox v-model="loginForm.remember">
            记住我
          </el-checkbox>
        </div>

        <el-button :loading="loading" type="primary" size="large" style="width: 100%;" @click.prevent="onLogin">
          {{ t('app.login') }}
        </el-button>
      </el-form>
    </div>
    <Copyright />
  </div>
</template>

<style lang="scss" scoped>
[data-mode="mobile"] {
  #login-box {
    position: relative;
    width: 100%;
    height: 100%;
    top: inherit;
    left: inherit;
    transform: translateX(0) translateY(0);
    flex-direction: column;
    justify-content: start;
    border-radius: 0;
    box-shadow: none;

    .login-banner {
      width: 100%;
      padding: 20px 0;

      .banner {
        position: relative;
        left: inherit;
        right: inherit;
        width: 100%;
        height: 100%;
        max-width: 375px;
        margin: 0 auto;
        display: inherit;
        top: inherit;
        transform: translateY(0);
      }
    }

    .login-form {
      width: 100%;
      min-height: auto;
      padding: 30px;
    }
  }

  .copyright {
    position: relative;
  }

  .login-switcher {
    display: none;
  }
}

:deep(input[type="password"]::-ms-reveal) {
  display: none;
}

:deep(.i18n-selector) {
  position: absolute;
  z-index: 1;
  top: 20px;
  right: 20px;
  cursor: pointer;
  font-size: 18px;
  color: var(--el-text-color-primary);
}

#login-box {
  display: flex;
  justify-content: space-between;
  position: absolute;
  width: 100%;
  height: 100%;

  .login-banner {
    flex: 1;
    position: relative;
    background: radial-gradient(circle at center, var(--g-app-bg), var(--g-main-bg));
    overflow: hidden;

    .banner {
      width: 50%;
      height: 50%;
      object-fit: contain;

      @include position-center(xy);
    }

    .logo {
      position: absolute;
      top: 20px;
      left: 20px;
      width: 30px;
      height: 30px;
      background: url("../../assets/images/logo.png") no-repeat;
      background-size: contain;
      box-shadow: var(--el-box-shadow-light);
    }
  }

  .login-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-height: 500px;
    width: 500px;
    padding: 50px;
    overflow: hidden;

    .title-container {
      position: relative;

      .title {
        font-size: 1.3em;
        color: var(--el-text-color-primary);
        margin: 0 auto 30px;
        font-weight: bold;
      }
    }
  }

  .el-form-item {
    margin-bottom: 24px;

    :deep(.el-input) {
      height: 48px;
      line-height: inherit;
      width: 100%;

      input {
        height: 48px;
      }

      .el-input__prefix,
      .el-input__suffix {
        display: flex;
        align-items: center;
      }

      .el-input__prefix {
        left: 10px;
      }

      .el-input__suffix {
        right: 10px;
      }
    }
  }

  :deep(.el-divider__text) {
    background-color: var(--g-app-bg);
  }

  .flex-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .sub-link {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 20px;
    font-size: 14px;
    color: var(--el-text-color-secondary);

    .text {
      margin-right: 10px;
    }
  }
}

.copyright {
  position: absolute;
  bottom: 0;
  padding: 20px;
  margin: 0;
  width: 100%;
}
</style>
