<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :model="loginForm"
      :rules="loginRules"
      ref="loginFormRef"
    >
      <div class="title-container">
        <h3 class="title">ithoth管理后台</h3>
        <!-- <lang-select class="lang-select"></lang-select> -->
      </div>
      <!-- username -->
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon="user"></svg-icon>
        </span>
        <el-input
          placeholder="请输入账户"
          name="username"
          type="text"
          v-model="loginForm.username"
        ></el-input>
      </el-form-item>
      <!-- password -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon="password"></svg-icon>
        </span>
        <el-input
          placeholder="请输入密码"
          name="username"
          :type="passwordType"
          v-model="loginForm.password"
        ></el-input>
        <span class="show-pwd">
          <span class="svg-container" @click="onChangePwdType">
            <svg-icon
              :icon="passwordType == 'password' ? 'eye' : 'eye-open'"
            ></svg-icon>
          </span>
        </span>
      </el-form-item>

      <!-- 登录按钮 -->
      <el-button
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        :loading="loading"
        @click="handleLogin"
        >{{ $t('msg.login.title') }}</el-button
      >
    </el-form>

    <!-- <div class="tips" v-html="$t('msg.login.desc')"></div> -->
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { validatePassword } from './rule'
import { useStore } from 'vuex'
//组件中使用，非组建中通过i18n/index.js下面创建的实例引入
import { useI18n } from 'vue-i18n'
// import { ElMessage } from 'element-plus'
// 数据源
const loginForm = ref({
  username: '',
  password: ''
})

const loginFormRef = ref(null)

const i18n = useI18n()
// 验证规则
const loginRules = ref({
  username: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.login.usernameRule')
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      validator: validatePassword()
    }
  ]
})

// 处理密码框文本显示
const passwordType = ref('password')

// template中绑定方法 直接声明
const onChangePwdType = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}

// 处理登录 composition api没有this!
const loading = ref(false)
const store = useStore()

//处理enter事件
onMounted(() => {
  document.addEventListener('keydown', (e) => {
    const key = window.event.keyCode
    if (key === 13) {
      handleLogin()
    }
  })
})

const handleLogin = () => {
  // 1.进行表单校验
  if (!loginFormRef.value) return
  loginFormRef.value.validate((valid) => {
    if (!valid) return
    // 2.触发登录动作
    loading.value = true
    store
      .dispatch('user/login', loginForm.value)
      .then(() => {
        loading.value = false
        // 3.登录后动作
      })
      .catch((err) => {
        console.log(err)
        loading.value = false
      })
  })
}
</script>
<style lang="scss" scoped>
$bg: #235a88;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 520px;
    padding: 160px 50px 0;
    margin: 0 auto;
    overflow: hidden;
    // ::v-deep可以选择子组件
    :deep(.el-form-item) {
      border: 1px solid rgba($color: #fff, $alpha: 0.1);
      background-color: rgba($color: #000000, $alpha: 0.1);
      border-radius: 5px;
      color: #454545;
    }
    :deep(.el-input) {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        height: 47px;
        background: transparent;
        border: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        caret-color: $cursor;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }
  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    :deep(.lang-select) {
      position: absolute;
      top: 4px;
      right: 0px;
      background-color: #fff;
      padding: 4px;
      border-radius: 4px;
      cursor: pointer;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .tips {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #fff;
    line-height: 24px;
  }
}
</style>
