<template>
  <div class="login-container">
    <div style="width: 100%;text-align: center">
      <img style="margin: 0 auto;margin-top: 8%" :src="title" alt="">
    </div>
    <el-row class="window-login">
      <el-col :span="15" class="picColor">
        <img style="width: 100%;height: 100%" :src="pic" alt="">
      </el-col>
      <el-col :span="9" style="background-color: #fff">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          auto-complete="on"
          label-position="top"
        >
          <div class="title-container" style="margin-top:35px;margin-bottom: 20px;">
            系统账户登录
          </div>

          <el-form-item label="账号名称" prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
              size="small"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>

          <el-form-item label="账号密码" prop="password" style="position: relative">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              size="small"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" style="position: absolute;right: 12px;" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-form-item label="验证码">
            <el-input
              ref="code"
              v-model="loginForm.code"
              style="width: 60%;vertical-align:middle"
              placeholder="请输入验证码"
              tabindex="2"
              auto-complete="on"
              size="mini"
              @keyup.enter.native="handleLogin"
            />
            <img :src="imgSrc" alt="" style="margin-left:5%;width: 35%;height: 40px;vertical-align:middle;" @click="captchaImage">
          </el-form-item>

          <el-form-item>
            <el-tooltip class="item" effect="dark" content="请联系管理员重置密码" placement="top">
              <el-button
                type="text"
                style="float: right;padding:0;
                color: #406AE1;
                font-family: Source Han Sans CN;
                font-weight: regular;
                font-size: 12px;
                line-height: 22px;
                letter-spacing: 0px;"
              >忘记密码？</el-button>
            </el-tooltip>
          </el-form-item>
          <el-button
            type="primary"
            style="width:100%;
            margin:10px 0 30px;
            border-radius: 40px;
            background: #406AE1;
            box-shadow: 0px 6px 4px 0px #547BD53F;"
            @keyup.enter.native="handleLogin"
            @click.native.prevent="handleLogin"
          >
            <h3 style="margin: 0;color: white">登  录</h3>
          </el-button>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { captchaImage } from '@/api/user'
import pic from '@/assets/login/pic.png'
import title from '@/assets/login/title@1.png'

export default {
  name: 'Login',
  data() {
    return {
      pic: pic,
      title: title,
      loginForm: {
        username: '',
        password: '',
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      imgSrc: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.captchaImage()
  },
  methods: {
    captchaImage() {
      captchaImage({ type: 'math' }).then(res => {
        const url = 'data:image/jpg;base64,' + res.img
        this.imgSrc = url
        this.loginForm.uuid = res.uuid
      })
    },
    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
          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">
  .login-container {
    background-image: url('../../assets/login/bg.png');
    background-size: 100% 100%;
    .el-form {
      width: 70%;
      margin: 0 auto;
      .el-input__inner{
        height: 40px;
      }
    }

  }
</style>

<style lang="css" scoped>
  .title-container{
    color: #547BD4;
    font-family: Noto Sans Malayalam UI;
    font-size: 28px;
    line-height: 26px;
    letter-spacing: 0px;
  }
  /deep/ .el-input__inner {
    border: 0;
    border-bottom: 1px solid #DCDFE6;
    border-radius: 0;
    padding: 0;
  }
  /deep/ .el-form-item__label{
    padding:0;
  }
  .login-container {
    min-height: 100%;
    width: 100%;
    overflow: hidden;
  }

  /deep/ .el-form-item{
    margin-bottom: 4px;
  }

  .window-login {
    overflow: hidden;
    border-radius: 15px;
    margin: 0 auto;
    margin-top: 3%;
    width: 800px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
  }
  .picColor{
    background: linear-gradient(166.06deg, #2782DF 0%, #003DAE 100%);
    box-shadow: 0px 4px 8px 0px #46BBFD5B;
  }
</style>
