<template>
  <div class="page flex-col">
    <div class="section_login flex-col">
      <div class="section_2 flex-col"><div class="section_3 flex-col"></div></div>
      <div class="section_4 flex-col"><div class="block_1 flex-col"></div></div>
      <div class="section_5 flex-col">
        <el-form ref="loginForm" class="login-form" :model="loginForm" :rules="loginRules" label-position="top">
          <div class="title">欢迎使用企研通系统</div>
          <span class="sub-title">请使用您的账号密码进行登录</span>
          <el-form-item prop="username" label="账号">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号/手机号"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password" label="密码">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="请输入密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item style="width:100%;">
            <div style="float: right">
              <router-link class="forgot-password" :to="'/forgotPassword'">忘记密码？</router-link>
            </div>
          </el-form-item>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              class="login-button"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
              &nbsp;&nbsp;&nbsp;&nbsp;
              <i class="el-icon-right"></i>
            </el-button>
            <div class="login-tip">
              <span>还没注册账号？</span>
              <router-link class="link-type" :to="'/register'">创建账号</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { md5 } from "@/plugins/md5.js";

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        username: '15888888888',
        password: 'admin123',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
  },
  methods: {
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          let data = JSON.parse(JSON.stringify(this.loginForm))
          // data.password = md5(data.password);
          this.$store.dispatch('Login', data).then(() => {
            this.$router.push({ path: this.redirect || '/' }).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.page {
  background-color: rgba(255, 255, 255, 1);
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.section_login {
  height: 100%;
  overflow: hidden;
  width: 100%;
  position: relative;
}

.section_2 {
  height: 777px;
  background: url(../assets/images/SketchPng541344af0021516af613e5d33011551dae67fd61346a3ffb30c3271a33d10d72.png)
  100% no-repeat;
  background-size: 100% 100%;
  width: 400px;
  position: relative;
}

.section_3 {
  width: 197px;
  height: 67px;
  background: url(../assets/images/SketchPng0315281a6c7ea53b0fbfab28fb440a1f6bb340f0da7336829473d65404ebe1b8.png)
  100% no-repeat;
  background-size: 100% 100%;
  margin: 21px 0 0 41px;
}

.section_4 {
  height: 682px;
  background: url(../assets/images/SketchPng553e00d0c9924665be872604947787caf55a86713ff8106c71ebc7d3aad6adbd.png)
  100% no-repeat;
  background-size: 100% 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 398px;
}

.block_1 {
  width: 685px;
  height: 591px;
  background: url(../assets/images/SketchPng2910edad7ca640260c33409149998e429df952fe097ffc722931c16dad352033.png)
  100% no-repeat;
  background-size: 100% 100%;
  margin: -267px 0 0 248px;
}

.section_5 {
  box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 1);
  border-radius: 6px;
  position: absolute;
  left: 60%;
  top: 152px;
}

.title {
  text-align: left;
  font-size: 24px;
  font-weight: bold;
  color: rgb(82, 77, 104);
  margin: 10px 0;
}

.sub-title {
  margin: 10px 0;
  display: inline-block;
  font-size: 14px;
  color: rgb(132, 148, 169);
  font-weight: 400;
}

.login-form {
  background: #ffffff;
  padding: 50px 50px 20px;
  width: 448px;
  border-width: 1px;
  border-style: solid;
  border-color: rgba(255, 255, 255, 1);
  border-radius: 5px;
  box-shadow: 0px 5px 20px rgb(110 0 255 / 10%);
  position: fixed;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

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

  .forgot-password {
    font-size: 12px;
    color: #8494A9;
  }

  .login-button {
    height: 40px;
    padding: 0 20px;
    background-color: rgba(110, 0, 255, 1);
    border: 1px solid rgba(110, 0, 255, 1);
    border-radius: 35px;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
    font-weight: 650;
    font-size: 14px;
    color: #FFFFFF;
    text-align: center;
  }

  .login-tip {
    font-weight: 400;
    color: #8494A9;
    font-size: 12px;
    margin-top: 20px;
  }
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

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

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

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