<template>
  <div class="login-container">
    <div class="content">
      <div class="left">
        <img :src="loginBackground" alt="">
      </div>
      <div class="right">
        <div class="logo-container">
          <!--      <h1>灵雀</h1>-->
          <img :src="logo" alt="">
        </div>

        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          autocomplete="on"
          label-position="left"
        >

          <div class="title-container">
            <div class="title">账号登录</div>
          </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="用户名"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-tooltip
            v-model="capsTooltip"
            content="大写锁定"
            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="密码"
                name="password"
                tabindex="2"
                autocomplete="on"
                @blur="capsTooltip = false"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>

          <el-form-item prop="captcha" class="captcha">
            <span class="svg-container">
              <svg-icon icon-class="captcha" />
            </span>
            <el-input
              v-model="loginForm.captcha"
              placeholder="验证码"
              @keyup.native="checkCapsLock"
              @keyup.enter.native="handleLogin"
            />
            <div class="captcha-container" @click="getCaptcha">
              <img :src="captchaUrl" alt="">
            </div>
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
      </div>
    </div>

    <div class="bottom-container">
      copyright© 2019 苏州英思唯智能科技有限公司.
    </div>
  </div>
</template>

<script>
import md5 from 'md5'
import loginBackground from '@/assets/login-background.png'
import logo from '@/assets/logo-login.png'
import backgroundImg from '@/assets/background-img.svg'
import { getCaptcha } from '@/api/components/basic-services'

export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        captcha: '',
        captchaId: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        captcha: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      logo: logo,
      backgroundImg: backgroundImg,
      loginBackground: loginBackground,
      captchaUrl: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    getCaptcha() {
      getCaptcha()
        .then(res => {
          const { data } = res
          this.captchaUrl = data.captcha
          this.loginForm.captchaId = data.id
        })
    },
    checkCapsLock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        this.capsTooltip = shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    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) {
          this.loading = true
          const params = {
            username: this.loginForm.username,
            password: md5(this.loginForm.password),
            id: this.loginForm.captchaId,
            captcha: this.loginForm.captcha
          }
          this.$store.dispatch('user/login', params)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(err => {
              this.loading = false
              this.$message.error(err.message)
              console.error(err.message)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

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

$bg:#f0f2f5;
$light_gray:rgba(0,0,0,.65);
$cursor: rgba(0,0,0,.65);

@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: 0;
      -webkit-appearance: none;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

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

  .el-form-item {
    border: 1px solid #d9d9d9;
    background: #fff;
    border-radius: 5px;
    color: #454545;
    transition: all .3s;
  }
  .el-form-item:hover{
    border-color: #40a9ff;
  }
}
</style>

<style lang="scss" scoped>
$bg:#29569a;
$dark_gray:#889aa4;
$light_gray:#eee;
$cursor: rgba(0,0,0,.65);
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  /*background-image: ;*/
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  .content{
    display: flex;
    max-width: 1200px;
    margin: 0 auto;
    justify-content: space-around;
    align-items: center;
    height: 100vh;
    .left{
      width: 560px;
      margin-right: 80px;
      img{
        width: 100%;
      }
    }
    .right{
      flex-shrink: 0;
      width: 520px;
      margin-left: 20px;
      margin-right: 20px;
      .logo-container{
        max-width: 300px;
        margin: 0 auto 20px auto;
        text-align: center;
        padding-left: 20px;
        padding-right: 20px;
        img{
          margin: 0 auto;
          width: 100%;
        }
      }
      .login-form {
        background-color: #fff;
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 35px 35px 0;
        margin: 0 auto;
        overflow: hidden;
        .captcha{
          .el-input{
            width: 70%;
          }
          .captcha-container{
            width: 80px;
            height: 30px;
            position: absolute;
            right: 5px;
            top: 12px;
            img{
              width: 100%;
            }
          }
        }
      }
    }
  }

  .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: relative;

    .title {
      font-size: 26px;
      color: $cursor;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: normal;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .bottom-container{
    color: rgba(255,255,255, 0.75);
    font-size: 14px;
    position: absolute;
    bottom: 20px;
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    width: 300px;
    left: 0;
    right: 0;
  }

  @media (max-width: 1200px) {
    .content{
      .left{
        display: none;
      }
    }
  }
}
</style>
