<template>
  <div class="login">
    <div class="color-model"></div>
    <div class="background-border"></div>
    <img class="login-text" src="../assets/images/login-text.svg" alt="">
    <img class="login-logo" src="../assets/images/login-logo.svg" alt="">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-width="80px">
      <h3 class="mini-title">欢迎登录</h3>
      <h3 class="title">移动安全保卫管理平台项目</h3>
      <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 prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      </el-form-item>

      <div style="width:80%;margin: 0 auto;">
        <el-button
          :loading="loading"
          size="medium"
          style="width:100%;background-color: #00afff;color:#fff;"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </div>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
<!--      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>-->
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { setLocal, getLocal, removeLocal } from '../utils/auth'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "Admin1234",
        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: false,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        //this.redirect = route.query && route.query.redirect;
        console.log(this.redirect,'重定向')
      },
      immediate: true
    }
  },
  created() {
    //this.getCode();
    this.getUserInfo();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getUserInfo() {
      const username = getLocal("username");
      const password = getLocal("password");
      const rememberMe = getLocal('rememberMe')
      this.loginForm = {
        username: !username? this.loginForm.username : username,
        password: !password? this.loginForm.password : decrypt(password),
        rememberMe: !rememberMe ? false : Boolean(rememberMe)
      };
    },
    handleLogin(key, value) {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          //判断是否保存登录信息，没有则登陆时删除
          if (this.loginForm.rememberMe) {
            setLocal("username", this.loginForm.username);
            setLocal("password", encrypt(this.loginForm.password));
            setLocal('rememberMe', this.loginForm.rememberMe);
          } else {
            removeLocal("username");
            removeLocal("password");
            removeLocal('rememberMe');
          }
          //登录请求
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.loading = false;
            console.log(this.redirect,'this.redirect')
            this.$router.push({ path:"/home" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: right;
  align-items: center;
  height: 100%;
  background: rgba(0, 140, 214, 1) url("../assets/images/login-background.png") no-repeat;
  background-size: cover;
  position: relative;
}
.color-model{
  width: 100%;
  height: 100%;
  position: fixed;
  background-color: rgba(0, 140, 214, .5);
}
.background-border{
  position: absolute;
  width: 100%;
  height: 342px;
  background-color: #fff;
  opacity: .2;
  z-index:0;
  display: flex;
}
.login-logo{
  position: absolute;
  left: 118px;
  top: 77px;
  width: 245px;
  height: 69px;
}
.login-text{
  position: absolute;
  left:23%;
  align-items: center;
  width: 510px;
  height: 190px;
}
.mini-title{
  font-size: 16px;
  text-align: center;
  color: #999;
}
.title {
  font-size: 32px;
  margin: 0 auto 50px auto;
  text-align: center;
  color: #333;
}

.login-form {
  margin-right: 15%;
  border-radius: 6px;
  background: #ffffff;
  width: 544px;
  height: 488px;
  padding: 25px 60px 50px 60px;
  z-index:2;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.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>
