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

<script lang="ts" setup name="Login">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import useUserStore from '@/store/modules/user'
import { RSAdecrypt, RSAencrypt } from '@/utils/security'
import { getPrivateKey, removePrivateKey } from '@/utils/auth'
const route = useRoute()
const router = useRouter()

const userStore = useUserStore()

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

const title = import.meta.env.VITE_APP_TITLE

// 表单类型，login 登录，reset 重置密码
const formType = ref('login')
// 加载状态
const loading = ref(false)
// 密码类型：password 密码
const passwordType = ref('password')
// 重定向路径
const redirect = ref(route.query.redirect?.toString() ?? '/')
// 是否需要验证码
const showKaptcha = ref(false)
// 公钥
const publicKey = ref('')
// sid
const sid = ref('')

// 获取系统基础配置： 公钥，是否开启验证码
function getBaseConfig() {
  userStore.getBaseConfig().then((res) => {
    showKaptcha.value = res.kaptcha
    publicKey.value = res.publicKey
    sid.value = res.sid
    ElMessage({
      message: '连接服务器成功',
      type: 'success',
    })
  })
}

// 登录
const loginFormRef = ref<FormInstance>()
const loginPasswordRef = ref<HTMLElement>()
const loginForm = ref({
  username: '',
  password: '',
  kaptcha: '',
  remember: !!localStorage.login_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 handleLogin() {
  loginFormRef.value
    && loginFormRef.value.validate(async (valid) => {
      if (valid) {
        loading.value = true
        // 表单对象
        const finalForm = {
          sid: sid.value,
          username: loginForm.value.username,
          password: '',
          kaptcha: loginForm.value.kaptcha,
        }
        // 加密
        finalForm.password = await RSAencrypt(loginForm.value.password)
        userStore
          .login(finalForm)
          .then(() => {
            ElMessage({
              message: '登录成功',
              type: 'success',
            })
            loading.value = false
            if (loginForm.value.remember) {
              // 保存用户名
              localStorage.setItem('login_username', loginForm.value.username)
              // 保存 加密密码串
              localStorage.setItem('login_password', finalForm.password)
            }
            else {
              // 移除用户名
              localStorage.removeItem('login_username')
              // 移除 加密密码串
              localStorage.removeItem('login_password')
              // 移除私钥
              removePrivateKey()
            }
            router.push(redirect.value)
          })
          .catch(() => {
            loading.value = false
          })
      }
    })
}
// 注册
const registerFormRef = ref<FormInstance>()
const registerPasswordRef = ref<HTMLElement>()
const registerCheckPasswordRef = ref<HTMLElement>()
const registerForm = ref({
  username: '',
  captcha: '',
  password: '',
  checkPassword: '',
})
const registerRules = ref<FormRules>({
  username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
  captcha: [{ required: true, trigger: 'blur', message: '请输入验证码' }],
  password: [
    { required: true, trigger: 'blur', message: '请输入密码' },
    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' },
  ],
  checkPassword: [
    { required: true, trigger: 'blur', message: '请再次输入密码' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.value.password) {
          callback(new Error('两次输入的密码不一致'))
        }
        else {
          callback()
        }
      },
    },
  ],
})
function handleRegister() {
  ElMessage({
    message: '注册模块仅提供界面演示，无实际功能，需开发者自行扩展',
    type: 'warning',
  })
  registerFormRef.value
    && registerFormRef.value.validate((valid) => {
      if (valid) {
        // 这里编写业务代码
      }
    })
}

// 重置密码
const resetFormRef = ref<FormInstance>()
const resetNewPasswordRef = ref<HTMLElement>()
const resetForm = ref({
  username: localStorage.login_username || '',
  captcha: '',
  newPassword: '',
})
const resetRules = ref<FormRules>({
  username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
  captcha: [{ required: true, trigger: 'blur', message: '请输入验证码' }],
  newPassword: [
    { required: true, trigger: 'blur', message: '请输入新密码' },
    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' },
  ],
})
function handleReset() {
  ElMessage({
    message: '重置密码模块仅提供界面演示，无实际功能，需开发者自行扩展',
    type: 'warning',
  })
  resetFormRef.value
    && resetFormRef.value.validate((valid) => {
      if (valid) {
        // 这里编写业务代码
      }
    })
}

function showPassword(passwordEl: HTMLElement | undefined) {
  passwordType.value = passwordType.value === 'password' ? '' : 'password'
  nextTick(() => {
    passwordEl?.focus()
  })
}

function testusername(username: string) {
  loginForm.value.username = username
  loginForm.value.password = '111111'
  handleLogin()
}
onBeforeMount(async () => {
  loginForm.value.username = localStorage.login_username || ''
  if (localStorage.login_username) {
    const passwoed = await RSAdecrypt(localStorage.login_password)
    loginForm.value.password = passwoed
  }
  getBaseConfig()
})
</script>

<template>
  <div>
    <div class="bg-top-banner">
      {{ title }}
    </div>
    <div class="bg-banner" />
    <div id="login-box">
      <el-form
        v-show="formType === 'login'"
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
      >
        <div class="title-container">
          <h3 class="title">
            用户登录
          </h3>
        </div>
        <div>
          <el-form-item prop="username">
            <el-input
              ref="name"
              v-model="loginForm.username"
              placeholder="用户名"
              text
              tabindex="1"
              autocomplete="on"
            >
              <template #prefix>
                <el-icon>
                  <svg-icon name="ep:user" />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              ref="loginPasswordRef"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              tabindex="2"
              autocomplete="on"
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon>
                  <svg-icon name="ep:lock" />
                </el-icon>
              </template>
              <template #suffix>
                <el-icon>
                  <svg-icon
                    :name="passwordType === 'password' ? 'eye' : 'eye-open'"
                    @click="showPassword(loginPasswordRef)"
                  />
                </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="handleLogin"
        >
          登录
        </el-button>
        <div
          v-if="showKaptcha"
          style="margin-top: 20px; margin-bottom: -20px; text-align: center;"
        >
          <el-divider>演示账号一键登录</el-divider>
          <el-button
            type="primary"
            size="small"
            plain
            @click="testusername('admin')"
          >
            admin
          </el-button>
          <el-button size="small" plain @click="testusername('test')">
            test
          </el-button>
        </div>
      </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;
        right: inherit;
        width: 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;
    bottom: 0;
    padding-bottom: 10px;
  }
}

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

.bg-top-banner {
  position: fixed;
  top: 0;
  width: 100%;
  height: 17vh;
  z-index: 999;
  line-height: 10vh;
  text-align: center;
  font-size: 30px;
  color: #fff;
  background:
    url("../assets/images/login-image/banner.png") no-repeat
    center / cover;
}

.bg-banner {
  position: fixed;
  z-index: 0;
  width: 100%;
  height: 100%;
  // background: radial-gradient(circle at center, var(--el-fill-color-lighter), var(--el-bg-color-page));
  background:
    url("../assets/images/login-image/bg.png") no-repeat center /
    cover;
}

#login-box {
  display: flex;
  justify-content: space-between;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  // background-color: var(--el-bg-color);

  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--el-box-shadow);

  .login-banner {
    position: relative;
    width: 450px;
    background-color: var(--el-fill-color-light);
    overflow: hidden;

    .banner {
      width: 100%;

      @include position-center(y);
    }

    .logo {
      position: absolute;
      top: 20px;
      left: 20px;
      width: 30px;
      height: 30px;
      border-radius: 4px;
      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;
    background:
      url("../assets/images/login-image/frame.png") no-repeat
      center / cover;
    overflow: hidden;

    .title-container {
      position: relative;

      .title {
        font-size: 1.3em;
        color: #fff;
        margin: 0 auto 30px;
        text-align: center;
        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;
      }
    }
  }

  .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: 30px;
  width: 100%;
  margin: 0;
}

:deep .el-input__wrapper {
  background-color: transparent !important;
}

:deep .el-input__inner {
  color: #fff !important;
}
</style>
