<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">川仪READOPC后台管理系统</h3>
      </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"
          auto-complete="off"
        />
      </el-form-item>
      <el-tooltip
        v-model="capsTooltip"
        content="已开启大写字母"
        placement="bottom-start"
        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"
            auto-complete="off"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span
            class="show-pwd"
            @click="showPwd"
          >
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <el-form-item
        v-if="captchaEnabled"
        prop="code"
        class="code-div"
      >
        <div
          class="el-form-item"
          style="width: 63%;float: left;"
        >
          <span class="svg-container">
            <svg-icon icon-class="validCode" />
          </span>
          <el-input
            ref="code"
            v-model="loginForm.code"
            placeholder="验证码"
            name="code"
            type="text"
            tabindex="3"
            auto-complete="off"
          />
        </div>
        <div class="login-code">
          <img
            :src="codeUrl"
            class="login-code-img"
            @click="getCode"
          >
        </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>
</template>

<script>
import { getCode } from '@/api/user'
import { Message } from 'element-ui'
import errorCode from '@/utils/errorCode'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        publicKey: '',
        userName: '',
        password: '',
        code: '',
        uid: ''
      },
      loginRules: {
        userName: [{ required: true, trigger: ['blur', 'change'], message: '请输入您的用户名', pattern: '[^ \x22]+' }],
        password: [{ required: true, trigger: ['blur', 'change'], message: '请输入您的密码', pattern: '[^ \x22]+' }],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      passwordType: 'password',
      // 大写字母开启提示
      capsTooltip: false,
      // 加载状态
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
  },
  mounted() {
  },
  destroyed() {
  },
  methods: {
    getCode() {
      getCode(true).then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          const data = res.data
          this.loginForm.uid = data.uid
          this.loginForm.publicKey = data.publicKey
          this.codeUrl = 'data:image/gif;base64,' + data.verifyCode
        }
      })
    },
    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() {
      if (!this.loginForm.publicKey) {
        const msg = errorCode[900]
        Message.error(msg)
        return
      }
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          console.log(this.loginForm)
          this.loading = true
          this.$store.dispatch('user/Login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
            this.loading = false
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
$imgBg: url("~@/assets/images/loginbg.png");
$dark_gray: #889aa4;

.login-container {
  .el-input {
    display: inline-block;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #2c3e50;
  //background-image: $imgBg;
  //background-size: cover;
  overflow: hidden;

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

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

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

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

  .login-code {
    width: 33%;
    height: 38px;
    float: right;
    display: flex;
    justify-content: flex-end;

    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .code-div {
    border: 0;
    position: relative;
  }

  .login-code-img {
    height: 38px;
  }
}
</style>
