<template>
  <div class="login-page" ref="login" @keyup.enter.native="handleLogin">
    <div class="panel-container">
      <section class="login-intro">
        <h1 class="intro-title">{{ website.title }}</h1>
        <p class="intro-subtitle">欢迎登录！</p>
        <img :src="loginImg" alt="login"/>
      </section>
      <section class="login-panel">
        <userLogin v-if="activeName==='user'"></userLogin>
        <renlian v-else-if="activeName==='renlian'"></renlian>
      </section>
    </div>
  </div>
  <!--    <div class="login-container"-->
  <!--         ref="login"-->
  <!--         @keyup.enter.native="handleLogin">-->
  <!--        <div class="login-weaper animated bounceInDown">-->
  <!--            <div class="login-left">-->
  <!--                <div class="login-time">-->
  <!--                    {{time}}-->
  <!--                </div>-->
  <!--            </div>-->
  <!--            <div class="login-border">-->
  <!--                <div class="login-main">-->
  <!--                    <h4 class="login-title">-->
  <!--                    </h4>-->
  <!--                    <userLogin v-if="activeName==='user'"></userLogin>-->
  <!--                    <renlian v-else-if="activeName==='renlian'"></renlian>-->
  <!--                    <div class="login-menu" style="margin-top: 105px">-->
  <!--                        <a href="#" style="color: red;font-weight: bold">提示：如果未录入人脸请联系管理员进行录入</a>-->
  <!--                    </div>-->
  <!--                </div>-->

  <!--            </div>-->
  <!--        </div>-->
  <!--    </div>-->
</template>
<script>
import userLogin from "./userlogin.vue";
import renlian from "./renlian.vue";
import {mapGetters} from "vuex";
import {dateFormat} from "@/util/date";
import {validatenull} from "@/util/validate";
import {getQueryString, getTopUrl} from "@/util/util";
import loginSvg from "@/assets/images/login.svg";
import website from "@/config/website";

export default {
  name: "login",
  components: {
    userLogin,
    renlian,
  },
  data() {
    return {
      time: "",
      activeName: "user",
      loginImg: loginSvg,
      website,
      socialForm: {
        tenantId: "000000",
        source: "",
        code: "",
        state: "",
      }
    };
  },
  watch: {
    $route() {
      this.handleLogin();
    }
  },
  created() {
    this.handleLogin();
    this.getTime();
    //配置中心的接口完成后，放开以下代码
    //this.loadData();
  },
  mounted() {
  },
  computed: {
    ...mapGetters(["website", "tagWel"])
  },
  props: [],
  methods: {
    loadData() {
      config.getConfig().then(res => {
        this.website.title = res.data.data.systemName;
      })
    },
    getTime() {
      setInterval(() => {
        this.time = dateFormat(new Date());
      }, 1000);
    },
    handleLogin() {
      const topUrl = getTopUrl();
      const redirectUrl = "/oauth/redirect/";
      this.socialForm.source = getQueryString("source");
      this.socialForm.code = getQueryString("code");
      this.socialForm.state = getQueryString("state");
      if (validatenull(this.socialForm.source) && topUrl.includes(redirectUrl)) {
        let source = topUrl.split("?")[0];
        source = source.split(redirectUrl)[1];
        this.socialForm.source = source;
      }
      if (!validatenull(this.socialForm.source) && !validatenull(this.socialForm.code) && !validatenull(this.socialForm.state)) {
        const loading = this.$loading({
          lock: true,
          text: '第三方系统登录中,请稍后。。。',
          spinner: "el-icon-loading"
        });
        this.$store.dispatch("LoginBySocial", this.socialForm).then(() => {
          window.location.href = topUrl.split(redirectUrl)[0];
          this.$router.push({path: this.tagWel.value});
          loading.close();
        }).catch(() => {
          loading.close();
        });
      }
    }
  }
};
</script>

<style lang="scss">
.login-page {
  width: 100%;
  height: 100vh;
  background: #f6f7f9;
  @include flex-row-center-center;

  .panel-container {
    position: relative;
    @include flex-row-center-center;
    width: auto;

    .login-intro {
      width: 500px;
      height: 500px;
      flex-shrink: 0;
      background-color: $primaryColor;
      padding: 50px;
      color: #fff !important;
      box-sizing: border-box;
      padding-right: 60px;
      /* border-right: 1px dashed rgba(0, 0, 0, 0.1); */
      border-top-left-radius: 6px;
      border-bottom-left-radius: 6px;

      @media screen and (max-width: 750px) {
        display: none;
      }

      > img {
        width: 100%;
        height: 100%;
      }

      .intro-title,
      .intro-subtitle {
        text-align: left;
      }

      .intro-title {
        font-weight: bold;
        font-size: 22px;
      }

      .intro-subtitle {
        font-size: 14px;
        margin-top: 10px;
      }
    }

    .login-panel {
      width: 400px;
      min-height: 500px;
      padding: 50px;
      background-color: #fff;
      box-sizing: border-box;
      transition: all 0.5s;
      flex-shrink: 0;
      border-top-right-radius: 6px;
      border-bottom-right-radius: 6px;

      @media screen and (max-width: 750px) {
        width: 90vw;
        padding: 50px 30px;
      }

      h1 {
        text-align: center;
        font-size: 24px;
        width: 100%;
        margin: 0 auto;
      }

      .login-form {
        margin-top: 40px;

        .login-btn {
          width: 100%;
          height: 40px;
          margin-top: 30px;
        }

        .form-bottom {
          margin-top: 10px;
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
      }
    }

    .code-by {
      position: absolute;
      bottom: -50px;
      left: 50%;
      transform: translateX(-50%);
      color: #ccc;
    }
  }
}
</style>
