<template>
  <div class="login-container">
    <img src="@/assets/images/logo.png" alt class="logo">
    <!-- <img src="@/assets/images/login-bujian.png" alt="" class="bujian"> -->
    <div class="login">
      <div class="logo" />
      <div class="title-container">
        <img src="@/assets/images/login-top.png">
        <!-- <h3 class="title">设备管理系统</h3>
          <b>Equipment Management System</b> -->
        <!-- <hr> -->
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="username" v-model="loginForm.username" placeholder="Username" name="username" type="text" tabindex="1" auto-complete="on" />
        </el-form-item>

        <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="Password" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

      </el-form>
      <el-button type="text" :loading="loading" class="btn" @click.native.prevent="handleLogin">登录</el-button>

      <!-- <span class="kpt">Copyright上海科湃腾信息科技有限公司. <br>All Rights Reserved</span> -->
    </div>
    <div style="position:absolute;bottom:0px;  font-size: 14px;height:40px;width:100%;color: #000;text-align:center;padding-top:15px;">

      <a href="http://www.dairyinfo.com.cn" target="_blank">上海科湃腾信息科技有限公司</a>
      <a style="margin-left:10px;" href="https://beian.miit.gov.cn/" target="_blank">沪ICP备11008303号-3 </a>
    </div>

  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6个字符'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '', //10020
        password: ''  //123456
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    var that = this
    document.onkeydown = function(e) {
      e = window.event || e
      // eslint-disable-next-line eqeqeq
      if (that.$route.path == '/login' && (e.code == 'Enter' || e.code == 'Num Enter')) { // 验证在登录界面和按得键是回车键enter
        that.handleLogin('ruleForm2') // 登录函数 （handleSubmit2('ruleForm2')-登录按钮的点击事件）
      }
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      console.log(12)
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

// $bg: #283443;
// $light_gray: #fff;
// $cursor: #fff;
// 更改
$bg: #f3f3f3;
$light_gray: #f3f3f3;
$cursor: #000;
@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: 38px;
    width: 80%;

    input {
      background: #f3f3f3;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 5px 5px 5px 15px;
      color: #000;
      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 {
    background: #f3f3f3;
    border-radius: 20px;
    color: #454545;
    width: 100%;
    margin: 30px auto;
    .el-form-item__content {
      line-height: 30px;
    }
  }
  // .el-form{background-color: red;}
  .btn {
    position: relative;
    width: 520px;
    max-width: 100%;
    overflow: hidden;
  }
  .btn span {
    width: 100%;
    background: #4c64f7;
    border: #4c64f7;
    border-radius: 40px;
    line-height: 50px;
    text-align: center;
    color: #fff;
    margin: -10px auto 0;
    display: block;
  }
}
</style>

<style>
.login-container .el-input input:-webkit-autofill,
textarea:-webkit-autofill,
select:-webkit-autofill {
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #000;

.login-container {
  min-height: 100%;
  width: 100%;
  height: 100%;
  position: relative;
  background: url("../../assets/images/login-bg.png") no-repeat;
  background-size: 100%;
  overflow: hidden;
  .logo {
    padding: 5px;
  }
  .bujian {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 200px;
    margin: auto;
  }
  .login {
    border-radius: 5%;
    padding: 30px;

    width: 380px;
    height: 340px;
    position: relative;
    top: 59%;
    left: 73%;
    margin-left: -190px;
    margin-top: -270px;

    .login-form {
      margin-top: 60px;
      position: relative;
      width: 520px;
      max-width: 100%;
      overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

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

    .title-container {
      position: absolute;

      height: 80px;
      width: 100%;
      top: -12px;
      left: 24px;
      border-radius: 12px 12px 0 0;
      opacity: 0.7;

      // img{height: 20px;}

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 0 auto;
        text-align: center;
        font-weight: bold;
      }
      b {
        text-align: center;
        font: 14px/2 "";
      }
    }

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

    .kpt {
      text-align: center;
      color: #fff;
      display: inline-block;
      width: 100%;
      font: 12px/18px "";
    }
    // p{text-align: center;color: #fff;display: inline-block;width: 100%;font-size:12px;margin-top: 30px;}
  }
}
</style>
