<template>
  <div class="login">
    <div class="container">
      <div class="header">
        <div class="logo"></div>
        <div class="propaganda">澎湃科技力量 牧业腾飞伙伴</div>
      </div>
      <div class="content">
        <div class="login-c-r">
          <div class="title">
            肉牛精准饲喂管理系统
          </div>
          <div class="form">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
              <el-form-item prop="username">
                <el-input ref="username" v-model="loginForm.username" placeholder="用户名" name="username" type="text" tabindex="1" auto-complete="on" />
              </el-form-item>

              <el-form-item prop="password" class="password">
                <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="密码" 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-item prop="password" class="rememberPassword">
                <input id="remember-password-checkbox" v-model="rememberPassword" type="checkbox" value="remember-me" @click="doRememberPassword($event)">
                <span for="remember-password-checkbox" style="position: absolute;top: -1px; margin-left: 5px;">
                  记住密码
                </span>
                </input>
              </el-form-item>
            </el-form>
          </div>
          <div class="btn">
            <el-button type="text" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
          </div>
        </div>
      </div>
      <div class="footer">
        <p>版权所有 <a href="http://www.dairyinfo.com.cn" target="_blank">上海科湃腾信息科技有限公司</a>
        <a style="margin-left: 20px;" href="https://beian.miit.gov.cn/" target="_blank">沪ICP备11008303号-3 </a></p>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import Cookies from 'js-cookie'
import { getToken } from '@/utils/auth'
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: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      rememberPassword: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    console.log(window.screen.width)
    console.log(window.screen.height)
    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')-登录按钮的点击事件）
      }
    }
  },
  mounted: function() {
    // 读取cookie中的账号信息，如果有accountInfo的话，则说明该用户之前勾选了记住密码的功能，则需要自动填上账号密码
    this.loadAccountInfo()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      var rememberStatus = this.rememberPassword
      var accountInfo = this.loginForm.username + '&' + this.loginForm.password
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm).then(() => {
              if (rememberStatus) {
                console.log('勾选了记住密码，现在开始写入cookie')
                Cookies.set('accountInfo', accountInfo, 1440 * 3)
              } else {
                console.log('没有勾选记住密码，现在开始删除账号cookie')
                Cookies.remove('accountInfo')
              }

              // 若为本地环境 则手写cookie
              if (window.location.href.indexOf('localhost') != -1) {
                Cookies.set('token', getToken(), 1440)
              }
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    doRememberPassword() {
      const rememberStatus = this.rememberPassword
      this.rememberPassword = !rememberStatus
    },
    loadAccountInfo: function() {
      const _this = this
      // zhaopeng&A15hOsu8YeGnCsjb
      const accountInfo = Cookies.get('accountInfo')
      // 如果cookie里没有账号信息
      if (Boolean(accountInfo) == false) {
        console.log('cookie中没有检测到账号信息！')
        return false
      } else {
        // 如果cookie里有账号信息
        console.log('cookie中检测到账号信息！现在开始预填写！')
        let userName = ''
        let passWord = ''
        const index = accountInfo.indexOf('&')

        userName = accountInfo.substring(0, index)
        passWord = accountInfo.substring(index + 1)
        // this.loginForm.username + '&' + this.loginForm.password
        _this.loginForm.username = userName
        _this.loginForm.password = passWord
        _this.rememberPassword = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .login{
    // background: url(../../assets/images/loginImages/login_bg1.png) center center no-repeat;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
  }
  .container{
    width: 55%;
    height: 100%;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
  }
  .header {
    width: 100%;
    color: #fff;
    position: absolute;
    top: 20%;
    display: flex;
    justify-content: center;
    align-items: center;
    .propaganda{
      background-image: linear-gradient(to right, #BE8A2F, #E59379);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }
  .content{
    position: absolute;
    top: 30%;
  }
  .login-c-r{
    position: absolute;
    left: 50%;
    top: 14%;
    width: 30%;
  }
  .footer {
    width: 100%;
    text-align: center;
    position: absolute;
    font-size: 14px;
    bottom: 10%;
    color:#666666;
  }

  @media (min-width: 500px) {
    .login{
      height: 100%;
      width: 100%;
      background: url(../../assets/images/loginImages/login_bg1.png) center center no-repeat;
      background-size:cover;
    }
    .container {
      // background: red;
      .header {
        height: 35px;
        line-height:35px;
        .logo{
          width: 110px;
          height: 100%;
          margin-right: 26px;
          background: url(../../assets/images/loginImages/logo1.png) center center no-repeat;
        }
        .propaganda{
          font-size: 21px;
        }
      }
      .content{
        width: 538px;
        height: 266px;
        background: url(../../assets/images/loginImages/center_bg1.png) center center no-repeat;
      }
    }
    .title{
      font-size: 16px;
      background-image: linear-gradient(to right, #BE8A2F, #E59379);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
    /deep/ .el-input__inner{
      height: 25px;
      line-height: 25px;
    }
    /deep/ .el-form-item{
      margin-bottom: 0;
    }
    /deep/ .el-form-item__content{
      font-size: 12px;
    }
    .show-pwd {
      position: absolute;
      right: 15px;
      top: 3px;
      font-size: 16px;
      color: #999;
      cursor: pointer;
      user-select: none;
    }
    .btn{
      width: 170px;
      height: 40px;
      color: #fff !important;
      background: url(../../assets/images/loginImages/btn1.png) center center no-repeat;
      position: absolute;
      bottom: -20%;
      left: 0;
      line-height: 32px;
      font-size: 14px;
      text-align: center;
      .el-button--text{
        color: #fff !important;
      }
    }
  }
  @media (min-width: 1920px) {
    .login{
      height: 100%;
      width: 100%;
      background: url(../../assets/images/loginImages/login_bg2.png) center center no-repeat;
      background-size:cover;
    }
    .container {
      // background:blue;
      .header {
        height: 70px;
        line-height:70px;
        .logo{
          width: 220px;
          height: 100%;
          margin-right: 52px;
          background: url(../../assets/images/loginImages/logo2.png) center center no-repeat;
        }
        .propaganda{
          font-size: 42px;
        }
      }
      .content{
        width: 1075px;
        height: 531px;
        background: url(../../assets/images/loginImages/center_bg2.png) center center no-repeat;
      }
      .title{
        font-size: 32px;
        background-image: linear-gradient(to right, #BE8A2F, #E59379);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
      /deep/ .el-input__inner{
        height: 50px;
        line-height: 50px;
      }
      /deep/ .el-form-item{
        margin-bottom: 30px;
      }
      /deep/ .el-form-item__content{
        font-size: 16px;
      }
      .show-pwd {
        position: absolute;
        right: 15px;
        top: 6px;
        font-size: 16px;
        color: #999;
        cursor: pointer;
        user-select: none;
      }
      .btn{
        width: 340px;
        height: 80px;
        color: #fff !important;
        background: url(../../assets/images/loginImages/btn2.png) center center no-repeat;
        position: absolute;
        bottom: -20%;
        left: 0;
        // line-height: 80px;
        font-size: 28px;
        text-align: center;
        .el-button--text{
          color: #fff !important;
          line-height: 60px;
        }
      }
    }
  }
</style>
