<template>
  <div class='login' :loading='dats.loading'>
    <div class='backgroung'>
      <!--      <div class='text'>
              <h1>WHOrigin</h1>
              <h6>Internet technology</h6>
            </div> -->
      <div class='text'>
        <img src='@/assets/images/logoBack.png' alt='' @click='handHref' />
        <h5 @click='handHref'><img src='@/assets/images/backIcon.png' alt='' />返回官网</h5>
      </div>
      <img src='@/assets/images/login-background2.png' alt='' />
    </div>
    <div class='box'>
      <div>
        <h2>登录</h2>
        <h2>智能化工程数字平台</h2>
        <el-form ref='loginForms' :model='dats.loginForm' :rules='dats.loginRules' hide-required-asteriskz>
          <el-form-item prop='username'>
            <label for='username' class='el-form-item__label'>
              <div>账号</div>
            </label>
            <el-input
              v-model='dats.loginForm.username'
              type='text'
              auto-complete='off'
              placeholder='输入账号'
              @keyup.enter.prevent='handleLogin'
            >
              <template #prefix>
                <el-icon>
                  <UserFilled />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop='password'>
            <label for='username' class='el-form-item__label'>
              <div>密码</div>
            </label>
            <el-input
              v-model='dats.loginForm.password'
              type='password'
              auto-complete='off'
              placeholder='输入密码'
              @keyup.enter.prevent='handleLogin'>
              <template #prefix>
                <el-icon>
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-checkbox v-model='dats.loginForm.rememberMe' style='margin:0px 0px 25px 0px'>记住用户</el-checkbox>
          <el-form-item class='el-form-item-button'>
            <el-button
              :loading='dats.loadingBtn'
              size='large'
              type='primary'
              style='width:204px;'
              @click.enter.prevent='handleLogin'>
              <span v-if='!dats.loading'>登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script setup lang='ts'>
import { onBeforeUnmount, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElForm } from 'element-plus'
import { Lock, UserFilled } from '@element-plus/icons-vue'
import Cookies from 'js-cookie'
import type { dasinter, inconfig } from '@/views/login/interface'
import { datas as datess } from '@/views/login/interface'
import { useLoginStore } from '@/store'
import { getSecretConfig } from '@/api/system/login'


const loginForms = ref<any>(null)
const dats = reactive<dasinter>(datess)


watch(() => dats.config, (val: inconfig) => {
    dats.config.dataSecretOnOff = val && val.dataSecretOnOff ? true : false
    dats.config.captchaOnOff = val.captchaOnOff === undefined ? true : val.captchaOnOff
    if (dats.config.captchaOnOff) {
      dats.config.codeUrl = 'data:image/gif;base64,' + val.img
      dats.loginForm.uuid = val.uuid!
    }
  },
  { deep: true, immediate: true })








const handHref = () => {
  window.location.href = 'https://www.whorigin.cn/'
}
const keyDown = () => {
  document.onkeydown = function(event) {
    const e = event || window.event
    if (e.key == 'Enter') {
      handleLogin()
    }
  }
}


const keyDownReview = () => {
  document.onkeydown = function(event) {
    const e = event || window.event
    e.returnValue = true
  }
}

const loadConfig = async () => {
  dats.loading = true
  const { data } = await getSecretConfig()
  dats.config = data
  dats.loading = false
}

const getCookie = () => {
  const username = Cookies.get('username')
  const rememberMe = Cookies.get('rememberMe')
  if (username && username != undefined) {
    dats.loginForm.username = username
  }
  if (rememberMe && rememberMe != undefined) {
    dats.loginForm.rememberMe = Boolean(rememberMe)
  }
}
const setCookie = () => {
  if (dats.loginForm.rememberMe) {
    Cookies.set('username', dats.loginForm.username, { expires: 30 })
    Cookies.set('rememberMe', ('' + dats.loginForm.rememberMe + ''), { expires: 30 })
  } else {
    Cookies.remove('username')
    Cookies.remove('rememberMe')
  }
}
const handleLogin = () => {
  loginForms.value.validate(async (valid: any) => {
    if (valid) {
      if (!dats.loadingBtn) {
        dats.loadingBtn = true
        //  如果数据加解密打开
        // if (dats.config.dataSecretOnOff) {
        //   const secret = getRandomString(16)
        //   const pubKey = await SecretAPI.getPublicKey()
        //   dats.loginForm.publicQKey = pubKey
        //   dats.loginForm.secret = SMUtil.sm2Encode(pubKey, secret)
        //   dats.loginForm.password = SMUtil.sm4Encode(secret, this.loginForm.password)
        //   this.$store.dispatch('Getdatsecret', secret)
        // }

        setCookie()
        useLoginStore().LoginWithPwd(dats.loginForm).then(() => {
          console.log('登录成功，但是不能跳转了')
          // 登录成功跳转
          useRouter().push({ path: dats.redirect+'' || '/' })
        }).catch(() => {
          dats.loadingBtn = false
          if (dats.config.captchaOnOff) {
            // dats.getCode()
          }
          dats.loginForm.password = ''
        })
      }

    }
  })
}

keyDown()
getCookie()
loadConfig()
onBeforeUnmount(() => {
  keyDownReview()
})

</script>




<style lang='less'>
.login {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;

  .backgroung {
    height: 100%;
    padding-left: 3vw;

    .text {
      user-select: none;
      position: absolute;
      left: 5vw;
      top: 8vh;
      color: rgba(147, 146, 149, 1);

      h1 {
        margin: 0;
        font-size: 60px;
      }

      h6 {
        border-top: 1px solid rgba(147, 146, 149, 1);
        line-height: 50px;
        margin: 5px 0 0 0;
        font-size: 24px;
        width: 112%;
      }

      h5 {
        margin-block-start: 1.67em;
        margin-block-end: 1.67em;
        margin-inline-start: 0px;
        margin-inline-end: 0px;
        width: 200px;
        height: 46px;
        display: flex;
        align-items: center;
        border-radius: 4px;
        -webkit-text-stroke: 0.6px #999999;
        color: #999999;
        font-size: 16px;
        box-shadow: 0 0 1px 1px #999999;
        -webkit-box-shadow: 0 0 1px 1px #999999;
        cursor: pointer;

        img {
          object-fit: none;
          margin-left: 24px;
        }
      }

      img {
        cursor: pointer;
      }
    }

    img {
      object-fit: contain;
      height: 100%;
    }
  }

  .box {
    > div {
      margin-top: -100px;
      background: white;
      padding: 50px;
      border-radius: 10px;
      overflow: auto;
      text-align: left;
    }

    position: absolute;
    min-width: 600px !important;
    top: 0;
    right: 0;
    width: 1262px;
    max-width: 70%;
    overflow-y: auto;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    h2 {
      font-size: 28px;
      font-weight: bold;
      margin: 10px 0;
      line-height: 40px;
    }

    .el-form {
      margin-top: 30px;

      .el-input-root {

      }

      input {
        width: 400px;
        height: 48px;
        line-height: 48px;
        text-indent: 5px;
        font-size: 18px;
        box-sizing: border-box;
      }

      input:focus {
        border: 1px solid rgba(85, 168, 253, 0.3);
        box-shadow: 0 0 8px 0 #55A8FD;
        background: white;
        background: #F5F7FA;
        border: 1px solid white;
      }

      .el-input__prefix {
        position: absolute;
        height: 100%;
        left: 10px;
        top: 0;
        text-align: center;
        color: #C0C4CC;
        -webkit-transition: all 0.3s;
        transition: all 0.3s;
      }

      .el-input__wrapper {
        padding: 0px;
      }

      .el-input__inner {
        padding-left: 25px;
        background: #F5F7FA;
        border: 1px solid white;
      }

      .el-form-item-button .el-form-item__content {
        justify-content: end;
      }
    }
  }
}


</style>
