<template>
  <div class="login-box hidden-xs-only">
    <div class="left">
      <div class="logo"></div>
    </div>
    <div class="login-container right">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="off"
        label-position="left"
        size="small"
      >
        <div class="logo">政研室重要文档起草系统</div>
        <div class="title-container">
          <div class="title">登录</div>
        </div>

        <el-form-item prop="username">
          <el-input
            suffix-icon="el-icon-user"
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="off"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            ref="passwordRef"
            v-model="loginForm.password"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="off"
            show-password
          />
        </el-form-item>

        <el-form-item prop="code" v-if="captchaEnabled">
          <div class="captcha-container">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon
                slot="suffix"
                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"
          style="margin: 0px 0px 25px 0px"
          >记住密码</el-checkbox
        >
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.prevent="handleLogin"
          >登录</el-button
        >
      </el-form>
      <!-- <div class="footer">©贵阳市档案馆（市地方志办）版权所有</div> -->
    </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: "",
      loginForm: {
        username: "admin",
        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: 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.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.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) {
          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((err) => {
              console.log("登录失败", err);
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-form-item {
    border: 2px solid #e0e7ff;
    background: #fff;
    border-radius: 35px;
    color: #454545;
    box-shadow: 0px 8px 10px 0px rgba($color: #131242, $alpha: 0.07);

    .el-input {
      display: inline-block;
      height: 47px;

      input {
        background: transparent !important;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: #333;
        height: 47px;
        caret-color: #333;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px transparent inset !important;
        }
      }
    }
  }
}

input:-webkit-autofill {
  box-shadow: 0 0 0px 999px rgba(255, 255, 255, 1) inset !important;
  -webkit-transition-delay: 99999s;
  -webkit-transition: color 99999s ease-out, background-color 99999s ease-out;
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$deep_gray: "#313c64";
$light_gray: #eee;

.login-box {
  display: flex;
  height: 100vh;
  width: 100%;
  overflow: hidden;
  background-image: url("~@/assets/images/login/bg.png");
  background-size: 100% 100%;
  box-sizing: border-box;
  position: relative;
}

.left,
.right {
  // width: 50%;
  height: 100%;
}

.left {
  width: 60%;
  padding: 80px;
  background-image: url("~@/assets/images/login/img2.png");
  background-size: 60%;
  background-repeat: no-repeat;
  background-position: center;
}

.right {
  background-color: #e8eff9;
  position: relative;
  width: 40%;
}

.login-container {
  min-height: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 120px;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $deep_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    font-size: 16px;
  }

  .logo {
    margin-bottom: 60px;
    text-align: center;
    font-size: 26px;
    font-weight: bold;
  }

  .title-container {
    position: relative;
    margin: 0px auto 40px auto;
    text-align: center;

    .title {
      font-size: 26px;
      color: #170f49;
      margin-bottom: 8px;
    }

    .subtitle {
      letter-spacing: 2px;
    }
  }

  .captcha {
    // width: 100%;
    height: 100%;

    img {
      display: block;
      width: 100%;
      height: 100%;
    }
  }
}

.el-button--primary {
  background-color: #1a5eff;
  border-radius: 35px;
  color: #fff;
  border-color: transparent;
  box-shadow: 0px 8px 22px 0px rgba($color: #1a5eff, $alpha: 0.7);

  &:hover {
    background-color: rgba($color: #1a5eff, $alpha: 0.8);
  }
}

.footer {
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
}

:deep(.el-input__wrapper) {
  box-shadow: none !important;
  background-color: transparent !important;
  width: 100%;
}

.captcha-container {
  display: flex;
  .login-code {
    height: 46px;
    display: flex;
    align-items: center;
    img {
      height: 90%;
      width: 80%;
      object-fit: contain;
    }
  }
}
</style>
