<template>
  <div class="login_box">
    <div class="login-logo"></div>
    <el-form ref="loginForm" :model="loginForm" class="login-form">
      <span class="title">欢迎登录</span>
      <el-form-item prop="username">
        <el-input @input="isMessage = false" v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="account" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input @input="isMessage = false" 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">
        <div class="custom-code">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 65.2%"
            @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>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" class="custom-checkbox-style">记住密码</el-checkbox>
      <el-form-item>
        <el-button :loading="loading" size="medium" type="primary" class="login_btn" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div class="login-message" v-if="isMessage">{{ loginMessage }}</div>
        <!-- <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div> --->
      </el-form-item>

    </el-form>


    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2024 ruoyi.vip 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 {
      isMessage: false,
      loginMessage: '',
      codeUrl: "",
      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,
      // 验证码开关
      captchaEnabled: 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: {
    handleMessage() {
      this.isMessage = !this.isMessage
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled == undefined ? true : res.captchaEnabled;
        console.log(res.captchaEnabled, 'res.captchaEnabledres.captchaEnabledres.captchaEnabled');
        if (this.captchaEnabled) {
          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) {
          // debugger
          this.loading = true;
          if (this.loginForm.username == '') {
            this.loginMessage = '请输入您的账号'
            this.isMessage = true;
            this.loading = false;
            return;
          }
          else if (this.loginForm.password == '') {
            this.loginMessage = '请输入您的密码'
            this.loading = false;
            this.isMessage = true;
            return;
          }
          else if (this.loginForm.code == null && this.captchaEnabled == true) {
            this.loginMessage = '请输入验证码'
            this.loading = false;
            this.isMessage = true;
            return;
          }
          else {
            this.$store.dispatch("Login", this.loginForm).then(() => {
              this.isMessage = false;
              this.$router.push({ path: this.redirect || "/index" }).catch(() => { });
            }).catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
          }
          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');
          }


        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login_box {
  width: 100%;
  height: 100%;
  background: url("../assets/images/bjtp.png") no-repeat center;
  background-size: cover;
  position: relative;

}

.custom-checkbox-style {
  margin-left: 14px;
}

.login-logo {
  width: 361px;
  height: 70px;
  min-width: 361px;
  min-height: 70px;
  background: url("../assets/images/zhlogo.png") no-repeat center;
  background-size: cover;
  position: absolute;
  top: 6%;
  left: 7.9%;
}

.login_btn {
  width: 101%;
  height: 40px;
  margin-top: 8.5%;
  border-radius: 33px;
  font-size: 12px;
}

.custom-code {
  display: flex;
  justify-content: space-between;
}

.title {
  display: inline-block;
  font-size: 20px;
  font-weight: 950;
  letter-spacing: 0;
  color: #333333;
  font-family: 'Times New Roman', Times, serif;
  margin: 5.1% 0 13.1% 0;
  color: #707070;
}

.login-form {
  position: absolute;
  border-radius: 6px;
  background: #ffffff;
  width: 27.2%;
  height: auto;
  top: 22%;
  left: 58%;
  padding: 3.5% 4.5% 0.9% 4.5%;

  .el-input {
    margin-bottom: 3%;

    input {
      height: 36px;
    }
  }

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

.login-tip {
  font-size: 13px;
  // text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 31%;
  height: auto;
  border-radius: 6px;

  img {
    width: 100%;
    cursor: pointer;
    vertical-align: middle;
    // margin-left: 16.5%;
    border-radius: 6px;
    margin-top: -1px;
  }
}

.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: 36px;
}

.login-message {
  font-size: 14px;
  font-weight: 400;
  color: #F1403C;
  letter-spacing: 0;
}
</style>
