<template>
  <div class="login">
    <el-form
      label-position="top"
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <el-row>
        <el-col :span="13" class="_left">
          <img src="@/assets/images/login-left.png" alt="" />
        </el-col>
        <el-col :span="11" class="_right">
          <h3 class="title">后台登录</h3>
          <el-form-item label="账户名称" prop="account">
            <el-input
              v-model="loginForm.account"
              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 label="密码" prop="password">
            <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="captchaOnOff">
            <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-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
        >记住密码</el-checkbox
      > -->
          <el-form-item style="width: 100%; margin-top: 69px">
            <el-button
              :round="false"
              :loading="loading"
              size="medium"
              type="primary"
              style="
                width: 100%;
                height: 50px;
                background-color: #0054ff;
                border-radius: 0;
                font-size: 18px;
                font-weight: 400;
                color: #ffffff;
              "
              @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>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2021 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 {
      codeUrl: "",
      mode: 1,
      loginForm: {
        account: "13888888888",
        password: "123456",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        account: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: false,
      // 注册开关
      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 account = Cookies.get("account");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        account: account === undefined ? this.loginForm.account : account,
        password: password === undefined ? this.loginForm.password : password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("account", this.loginForm.account, { expires: 30 });
            Cookies.set("password", this.loginForm.password, {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("account");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          let loginForm = Object.assign({}, this.loginForm);
          loginForm["mode"] = this.mode; // 登录方式  预留
          loginForm.password = this.md5(loginForm.password);
          this.$store
            .dispatch("Login", 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/images/login-background.jpg");
  background-color: #b1ccff;
  background-size: cover;
}
.title {
  height: 34px;
  font-size: 36px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #000000;
  margin: 0px auto 73px auto;
}
.login-form {
  background: #ffffff;
  width: 1200px;
  ._left {
    display: flex;
    justify-content: center;
    align-content: center;
    height: 660px;
    background: linear-gradient(0deg, #0054ff, #516af5);
    img {
      width: 495px;
      height: 441px;
      margin: auto;
    }
  }
  ._right {
    height: 100%;
    padding: 120px 104px 5px 104px;
  }
  .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>
<style lang="scss" scoped>
::v-deep .el-form-item__label {
  font-size: 18px;
  font-weight: 400;
  color: #666666;
  padding-bottom: 0;
  &::before {
    display: none;
  }
}
::v-deep .el-input {
  input {
    font-size: 18px;
    font-weight: 400;
    color: #333333;
    border-radius: 0;
    border: 0;
    border-bottom: 1px solid #dfdfdd;
    padding: 0;
    &:focus {
      border-bottom: 1px solid #547bd5;
    }
  }
}
</style>
