<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title">慧研学惠生活后台管理</div>
        <div class="sub-title">您好，欢迎登录</div>

      <el-form-item prop="account" >
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="请输入账号"
          name="account"
          :maxlength="11"
          type="text"
          tabindex="1"
          auto-complete="off"
        />
      </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="请输入登录密码"
          name="password"
          tabindex="2"
          auto-complete="off"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

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

    </el-form>
  </div>
</template>

<script>
import {superadminList} from '@/api/auth'
import { getToken, setToken, removeToken,setREToken } from '@/utils/auth'
import {setAes} from '@/utils/index'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名称'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      let reg = /(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}/
      if (reg.test(value)) {
          callback()
        } else {
          callback(new Error('密码长度应在8-16，并且需要包含字母、数字、符号'))
      }
    }
    return {
      loginForm: {
        account: '13245678904',
        password: 'admin123456.',
        
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        // googleCode: [{ required: true, trigger: 'blur', message:'请输入谷歌验证码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      failNum:0
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },

  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          let time = localStorage.getItem('loginTime')
          if(new Date().getTime()< time){
            return this.$message({
                duration:0,
                message: '密码输入错误3次，请十分钟后重试',
                type: 'error'
              });
          }
          this.loading = true
          let obj = JSON.parse(JSON.stringify(this.loginForm))
          obj.timestamp = new Date().getTime()
          obj.password = setAes(obj.password,obj.timestamp+'hyx')
          this.$store.dispatch('user/login', obj).then((content) => {
             superadminList().then(r=>{
                if (r.state == 'Success'&&r.content.length) {
                  localStorage.setItem('epid',r.content[0].id)
                  localStorage.setItem('shopName',r.content[0].name)
                  // this.$router.push({ path: this.redirect || '/' }) //登录成功进入之前访问的页面，没有权限会报错
                   if(content.update){
                    this.$router.push('/updatePassword')
                  }else{
                        this.$router.push({ path: '/' })
                  }
                  this.loading = false
                }else{
                  this.loading = false
                  removeToken()
                  localStorage.clear()
                }
              })
           
          }).catch(() => {
            this.loading = false
            // this.failNum ++
            // if(this.failNum == 3){
            //   let time = new Date().getTime() + 1000*60*10
            //   localStorage.setItem('loginTime',time)
            //   this.$message({
            //     duration:0,
            //     message: '密码输入错误3次，请十分钟后重试',
            //     type: 'error'
            //   });
            // }
          })
        } 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;

/* reset element-ui css */
.login-container {
  min-width: 900px;
  .el-form-item__content{
    background: #F6F6F6;
    border-radius: 46px 46px 46px 46px;
    width: 70%;
    margin: 0 auto 10px;
    input{
      border: none;
      background: #F6F6F6;
      color: #181818;
    }
    input:-webkit-autofill { box-shadow: 0 0 0px 1000px #F6F6F6 inset !important;}
    .el-input__inner{
      background: #F6F6F6!important;
    }
    
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }

  .el-form-item {
   
    color: #454545;
  }
}
</style>

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

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('../../assets/bg.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  position: relative;
  .login-form {
    position: absolute;
    left: 50%;
    top: 18%;
    width: 34%;
    height: 64vh;
    padding:  35px;
    border-radius: 10px;
    left: 52.5%;
    .login-btn{
      width: 70%;
      height: 52px;
      border-radius: 26px;
      text-align: center;
      display: block;
      margin: 40px auto 0;
    }
  }
  .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 {
      font-size: 30px;
      font-weight: 800;
      color: #2836fe;
      margin: 0px auto 20px;
      margin-top: 80px;
      padding-left: 15%;

    }

    .sub-title{
      font-size: 20px;
      font-weight: 800;
      color: #2836FE;
      margin-top: 20px;
      margin-bottom: 50px;
      padding-left: 15%;
    }

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