<template>
  <div class="bg">
    <div class="login">
      <div class="container">
        <div class="logo">
          <img src="../../assets/images/login/logo.png">
        </div>
        <div class="content">
          <div class="form">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" hide-required-asterisk>
              <el-form-item prop="username">
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  size="medium"
                  placeholder="请输入账号"
                  tabindex="1"
                  autocomplete="on">
                  <img slot="prefix" src="../../assets/images/login/userico.png" alt="" width="20" height="20">
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  size="medium"
                  placeholder="请输入密码"
                  tabindex="2"
                  autocomplete="on"
                  style="margin-top: 18px;">
                  <img slot="prefix" src="../../assets/images/login/password.png" alt="" width="20" height="20">
                </el-input>
              </el-form-item>
              <el-form-item>
                <validation ref="validation" style="margin-top: 18px;"/>
              </el-form-item>
              <el-form-item>
                <el-button :loading="loading" :disabled="flag" type="primary" size="medium" @click="handleLogin">登录
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <div class="login-foot">
        河北秦安安全科技股份有限公司 版权所有 Copy right 2013-2020-V{{ version }}
      </div>
    </div>
  </div>
</template>

<script>
import { requestFN } from '@/utils/request'
import validation from './components/validation'
import vueQr from 'vue-qr'

export default {
  name: 'Login',
  components: { vueQr, validation },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    return {
      dialogFormQrcode: false,
      qrcodeStr: '',
      version: '',
      imgUrl: require('@/assets/images/logo-tit2.png'),
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      rangeStatus: false,
      flag: false
    }
  },
  watch: {
    $route: {
      // eslint-disable-next-line
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    sessionStorage.clear()
    this.getVersion()
    // window.addEventListener('storage', this.afterQRScan)
  },
  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: {
    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.flag = true
      // 设置定时器，1秒后，按钮可用
      setTimeout(() => {
        this.flag = false
      }, 5000)
      this.$refs.loginForm.validate(valid => {
        /*        if (this.rangeStatus) {
          console.info(this.rangeStatus)
        } else {
          console.info(this.rangeStatus)
        }*/

        if (this.$refs.validation.rangeStatus) {
          if (valid) {
            this.loading = true
            requestFN(
              '/admin/check',
              {
                KEYDATA: 'qdkjchina' + this.loginForm.username + ',qd,' + this.loginForm.password,
                SOURCE: '1',
                PERSONNEL_TYPE: '1'
              }
            ).then((data) => {
              sessionStorage.setItem('user', JSON.stringify(data))
              this.$router.push({ path: '/index' })
              // this.loading = false
            })
              .catch((e) => {
                this.flag = false
                this.loading = false
              })
          } else {
            this.flag = false
            return false
          }
        } else {
          this.flag = false
          this.$message({
            message: '请进行登录验证',
            type: 'error'
          })
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    getVersion() {
      this.listLoading = true
      requestFN(
        '/versionlog/getVersion',
      ).then((data) => {
        this.listLoading = false
        this.version = data.pd.VERSION
      }).catch((e) => {
        this.listLoading = false
      })
    },
    goqrcode() {
      this.qrcodeStr = 'https://wwag.qhdsafety.com/file/uploadFiles/wwag/app.apk'
      this.dialogFormQrcode = true
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style lang="scss" scoped>
.bg{
  width: 100vw;
  height: 100vh;
  background-color: #158ae0;
  position: relative;
}
.login {
  width: 1920px;
  height: 937px;
  background-image: url("../../assets/images/login/bg.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;

  .container {
    width: 1400px;
    height: 728px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;

    .logo {
      position: absolute;
      right: 55px;
      top: 85px;
      img {
        width: 424px;
        height: 146px;
      }
    }

    .content {
      width: 404px;
      height: 470px;
      position: absolute;
      right: 55px;
      .form {
        margin-top: 162px;
        .el-button {
          width: 100%;
          margin-top: 26px;
          height: 50px;
          font-size: 16px;
          background-color: #2150d5;
          border: none;
        }
      }
    }
  }

  .login-foot {
    font-size: 14px;
    color: #fff;
    position: absolute;
    left: 50%;
    bottom: 45px;
    transform: translateX(-50%);
  }
}

</style>
<style scoped>
.form >>> .el-input__prefix {
  top: 6px;
}
</style>
