<template>
  <div class="login">


    <img src="@/assets/huijuntong.png" class="model">

    <div class="titletext">长春市惠军通双拥服务平台(二道区)</div>

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-content">

      <div class="title"> </div>

      <div class="please">
        请登录
      </div>

      <div class="contenttexte" >
        <img src="../assets/username.png" class="icontetx">
        <input type="text"  class="input" v-model="loginForm.username" placeholder="请输入用户名"/>
      </div>

      <div class="contenttexte" >
        <img src="../assets/password.png" class="icontetx">
        <input  v-model="loginForm.password" type="password"  class="input"      placeholder="请输入密码"   @keyup.enter.native="handleLogin"/>
      </div>




      <el-checkbox v-model="loginForm.rememberMe" style="margin-left: 28px;">记住密码</el-checkbox>


      <div>
          <el-button  class="btn"
            :loading="loading"
            style="width:90%;"
            @click.native.prevent="handleLogin" >
            登 录
          </el-button>
      </div>



    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2021-2022 春城惠军通后台管理系统 All Rights Reserved.</span>
    </div>
  </div>
</template>

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

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        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,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    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');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">


  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/background.png");
    background-size: 100% 100%;
  }

  .title {

    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;


  }


  .model {
    width: 150px;
    height: 150px;
    position: absolute;
    top: 2%;
    right: 44%;
  }


  .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;
    }
  }
  .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;
  }



  .titletext {



    position: absolute;
    top: 25%;
    right: 29%;
    font-size: 36px;
    font-weight: 800;
    color: #ffffff;
    /* box-shadow: #000000; */
    font-family: PingFangSC-Medium, PingFang SC;


  }


  .login-content{
    width: 400px;
    border-radius: 20px;
    position: absolute;
    top: 34%;
    right: 36%;
    background: #ffffff;
    opacity: 0.7;


  }
  .input{
    border: none;
    outline: none;
  }
  .please{
    width: 100px;
    padding: 10px;
    margin: 20px 0 10px 28px;
    font-weight: 700;
    border-bottom: 1px solid #0000EE;
  }
  .contenttexte{
    display: flex;
    align-items: center;
    border: 1px solid #CD0303;
    border-radius: 5px;
    padding: 10px;
    margin: 25px 28px;


  }
  .icontetx{
    width: 20px;
    height: 20px;
    margin-bottom: 4px;
    padding-right: 5px;

  }
  .btn{
    /* width: 300px;
    height: 30px; */
    background: #CD0303;
    text-align: center;
    color: #FFFFFF;
    border-radius: 5px;
    padding: 10px 0;
    margin: 20px 25px;
  }

</style>
