<template>
  <div class="login-container">
    <div class="login-weaper animated bounceInDown">
      <div class="login-logo">
        <img src="/img/logo.jpg" alt="" />
      </div>
      <p class="login-tip">巨雾商家管理后台</p>
      <div class="login-border">
        <div class="login-main">
          <userLogin v-if="activeName === 'user'" />
        </div>
      </div>
    </div>
    <div class="login-copyright">All Rights Reserved</div>
    <top-color v-show="false" />
  </div>
</template>
<script>
import { fetchList } from "@/api/admin/tenant";
import userLogin from "./userlogin";
import codeLogin from "./codelogin";
import thirdLogin from "./thirdlogin";
import { mapGetters } from "vuex";
import { getStore, setStore } from "@/util/store";
import topColor from "@/page/index/top/top-color";

export default {
  name: "Login",
  components: {
    userLogin,
    codeLogin,
    thirdLogin,
    topColor,
  },
  data() {
    return {
      active: "",
      tenantList: [],
      activeName: "user",
      socialForm: {},
    };
  },
  watch: {
    $route: {
      handler() {
        const params = this.$route.query;
        if (this.validatenull(params.state) && this.validatenull(params.code))
          return;

        this.socialForm.state = params.state;
        this.socialForm.code = params.code;

        const loading = this.$loading({
          lock: true,
          text: `登录中,请稍后。。。`,
          spinner: "el-icon-loading",
        });

        this.$store
          .dispatch("LoginBySocial", this.socialForm)
          .then(() => {
            loading.close();
            this.$router.push({ path: this.tagWel.value });
          })
          .catch(() => {
            loading.close();
          });
      },
      immediate: true,
    },
  },
  created() {
    this.getTenantList();
    this.active = getStore({ name: "tenantId" });
  },
  computed: {
    ...mapGetters(["website", "tagWel"]),
  },
  methods: {
    handleCommand(command) {
      // 写入到 sessionStore 方便 axios 每次请求获取 tab 范围内的租户ID
      setStore({ name: "tenantId", content: command, type: "session" });
      // 写入到 localStore 方便 下次登录回显上次选择的租户ID
      setStore({ name: "tenantId", content: command });
    },
    getTenantList() {
      fetchList().then((response) => {
        this.tenantList = response.data.data;
      });
    },
  },
};
</script>

<style lang="scss">
@import "@/styles/login.scss";
</style>
