<template>
  <div></div>
</template>

<script>
import { getCodeImg, getlists } from "@/api/login";
import { getInfo } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      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,
      // 验证码开关
      captchaOnOff: false,
      // 注册开关
      register: true,
      redirect: undefined,
      form: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        landUseRights: null,
        type: null,
        unifiedSocialCreditCode: null,
        actualBusinessAddress: null,
        industry: null,
        belongRegion: null,
        fireRating: null,
        acompEvalGrade: null,
        businessLicenseImg: null,
        certificateImg: null,
        ownershipImg: null,
        fireImg: null,
      },
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    // this.getCode();
    this.getCookie();
    this.handleLogin();
  },
  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() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.$store.dispatch("login2", this.loginForm).then((res) => {
        getInfo().then((res) => {
          this.$router
            .replace({
              path: "/profile",
              query: { routerPath: this.redirect },
            })
            .catch(() => {});
          loading.close();
        });
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 38px;
  width: 100%;
}

.registration {
  text-align: right;
}
</style>
