<template>
  <div class="login" v-loading="loading"  element-loading-text="登录加载中..."
    ></div>
</template>

<script>
// import {SsoLogin} from "@/api/login";
import Cookies from "js-cookie";
import {encrypt, decrypt} from '@/utils/jsencrypt'
import { getToken, setToken, setExpiresIn, removeToken } from '@/utils/auth'
export default {
  name: "Login",
  data() {
    return {
      code:undefined,
      loading: true,
      redirect: undefined, 
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    console.log(this.$route)
    let path =  this.$route.query.redirect;
    this.code = path.substr(path.indexOf('code=')+5);
    console.log(this.code)
    this.SsoLogin(this.code)

  },
  methods: {
    SsoLogin({ commit },code){
      // https://test-cc.zhgkxt.com/
      let param = {
        code:this.code,
        redirectUrl:'https://test-cc.zhgkxt.com',
        type:'authorization_code'
      }
      this.$store.dispatch("ssoLogin", param).then(() => {
        //成功后会跳转页面入首页
        this.$router.push({path: this.redirect || "/"}).catch(() => {
        });
      }).catch(() => {
        //失败返回南网登录页面
        window.location.replace('https://test-sso.csgmall.com.cn/?client_id=549OWptc&theme=zhgc_storage&response_type=code&redirect_uri=https%3A%2F%2Ftest-cc.zhgkxt.com')
      });
    },
    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(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  //display: flex;
  //justify-content: center;
  //align-items: center;
  position: relative;
  height: 100%;
  // background-image: url("../assets/images/login.png");
  // background-size: 100% 100%;
  //background: #1891FF;
}

.title {
  margin: 0px auto 30px auto;
  font-size: 32px;
  //text-align: center;
  color: #707070;
}

.login-bar {
  position: absolute;
  height: auto;
  top: 20%;
  left: 14%;
  width: auto;
  // height: 500px;
  border-radius: 6px;
  display: flex;
  background: #fff;
  text-align: center;
  padding-top: 40px;
}

.form-bar {
  //height: 500px;
  background: #fff;
}

.login-form {
  //border-radius: 6px;
  //height: 100%;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 45px;

    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-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
