<template>
  <div class="bigbox">
    <div class="login-container">
      <!-- style="background-image: url(img/login.png); background-size: 100% 100 -->

      <div class="img">
        <!-- <div class="login"> -->
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="off"
          label-position="left">
          <div class="title-container">
            <h4 class="title">
              {{ $t("login.title") }}
            </h4>
            <hr />
            <br>
          </div>
          <div><lang-select class="set-language" /></div>

          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input ref="username" v-model="loginForm.username" :placeholder="$t('login.username')" name="username"
              type="text" tabindex="1" autocomplete="on" />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
                :placeholder="$t('login.password')" name="password" tabindex="2" autocomplete="on"
                @keyup.native="checkCapslock" @blur="capsTooltip = false" @keyup.enter.native="handleLogin" />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>
          <span v-if="this.count > 0 && this.count <= 5" class="chance">您的还有{{5-this.count}}次机会</span>
          <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px "
            @click.native.prevent="handleLogin">
            {{ $t("login.logIn") }}
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import LangSelect from '@/components/LangSelect'
import SocialSign from './components/SocialSignin'
import CryptoJS from 'crypto-js';
export default {
  name: 'Login',
  components: { LangSelect, SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    return {
      pass: true,
      loginForm: {
        username: 'admin',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: this.validatePassword }
        ]
      },
      count: 0,
      secretKey: 'loginCount',
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $router: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    },
    delStorge(newCount) {
      if (newCount === 5) {
        setTimeout(() => {
          localStorage.removeItem('count')
          this.count = 1
        }, 60000 * 5)
      }
    }
  },
  computed: {},
  created() { },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    validatePassword(rule, value, callback) {
      if (value.length < 6) {
        callback(new Error('密码不能少于六位'))
      } else if(this.count > 5){
        callback(new Error('您的错误次数已超过五次，请五分钟后再尝试'))
      } else {
        callback()
      }
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.count++
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          let loginTime = CryptoJS.AES.encrypt(this.count.toString(), this.secretKey);
          localStorage.setItem('count', loginTime)
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              alert('密码错误')
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      })
    }
  }
}
</script>

<style lang="scss">
.chance{
  font-size: 2px;
  margin-left: 10px;
}

$bg: #283443;
$light_gray: #283443;
$cursor: #3f3939;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 30rem;
    height: 30rem;
    right: 0rem;
    top: 7rem;
    left: 2rem;
  }

  .img {
    height: 40rem;
    width: 77rem;
    background-repeat: no-repeat;
    position: absolute;
    top: 55%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: url("../../assets/2.png");
    background-size: contain;
  }

  .bigbox {
    background: rgb(255, 255, 255);
    width: 90%;
    left: 50%;
    top: 4rem;
    translate: -50% 0;
    margin-left: 48rem;
    margin-top: 2rem;
    padding: 2rem;
  }


  .login {
    position: relative;
    width: 40rem;
    height: 35rem;
    right: 0rem;
    top: 7rem;
    left: 40rem;
  }

  .svg-container {
    padding: 6px 5px 6px 10px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: rgb(49, 47, 62);
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: #b4c0e6;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
