<template>
  <div v-if="forums" v-loading="loading" class="register">
    <div class="lg-bg">
      <div class="login">
        <div class="bg">
          <div class="logo">
            <img src="../../assets/BLT.png" alt="" />
          </div>
        </div>
        <div class="form">
          <div class="welcome">
            <p>欢迎登录</p>
            <p>广州百伦供应链SAAS系统</p>
          </div>
          <div id="qr_login">
            <div id="wx_login"></div>
          </div>
        </div>
      </div>
      <div class="div_footer">
        <a href="http://www.beian.miit.gov.cn/" rel="nofollow" target="_blank">
          <p>粤ICP备16072482号</p>
        </a>
        <p>广州百伦供应链科技有限公司版权所有</p>
      </div>
    </div>
  </div>
</template>

<script>
import head from "@/mixin/head";
import handleError from "@/mixin/handleError";
import loginAbout from "@/mixin/loginAbout";
import service from "../../api/request";
import axios from "@/plugins/axios.js"
import "@/plugins/qrcode.js";

export default {
  name: "Login",
  mixins: [head, handleError, loginAbout],
  data() {
    return {
      title: this.$t("user.login"),
      userName: "",
      passWord: "",
      checked: true,
      activeName: "0", // 默认激活tab
      site_mode: "", // 站点模式
      isPaid: false, // 是否付费
      code: "", // 注册邀请码
      loading: false,
      canReg: false,
      ischeck: true,
      preurl: "/",
    };
  },
  computed: {
    forums() {
      return this.$store.state.site.info.attributes || {};
    },
  },

  mounted() {
    const { code, preurl } = this.$route.query;
    this.createCode();
    this.code = this.getQueryVariable("code");
    if (preurl) {
      this.preurl = preurl;
    }
    if (this.code) {
      this.getStaffInfo(this.code);
    }
    if (this.forums && this.forums.set_site && this.forums.set_site.site_mode) {
      this.site_mode = this.forums.set_site.site_mode;
    }
    if (
      this.forums &&
      this.forums.set_reg &&
      this.forums.set_reg.register_close
    ) {
      this.canReg = true;
    }
    this.createCode();
  },
  methods: {
    check(value) {
      this.ischeck = value;
    },
    // 用户名登录
    UserLogin() {
      this.loading = true;
      if (this.userName === "") {
        this.$message.error("用户名不能为空");
        this.loading = false;
      } else if (this.passWord === "") {
        this.$message.error("密码不能为空");
        this.loading = false;
      } else {
        const params = {
          data: {
            attributes: {
              username: this.userName,
              password: this.passWord,
            },
          },
        };
        this.$store
          .dispatch("session/h5Login", params)
          .then((res) => {
            this.loading = false;
            if (res && res.data && res.data.data && res.data.data.id) {
              this.logind(res);
              this.userName = "";
              this.passWord = "";
            }
            if (
              res &&
              res.data &&
              res.data.errors &&
              res.data.errors[0].code === "register_validate"
            ) {
              this.$router.push(`/user/warning?username=${this.userName}`);
            }
            if (res && res.data && res.data.errors && res.data.errors[0]) {
              const error = res.data.errors[0].detail
                ? res.data.errors[0].detail[0]
                : res.data.errors[0].code;
              const errorText = res.data.errors[0].detail
                ? res.data.errors[0].detail[0]
                : this.$t(`core.${error}`);
              this.$message.error(errorText);
            }
          })
          .catch((err) => {
            this.loading = false;
            console.log(err);
          });
      }
    },
    toRegister() {
      this.$router.push(
        `/user/register?code=${this.code}&preurl=${this.preurl}`
      );
    },
    iscanReg() {
      return [
        this.canReg &&
        this.forums &&
        this.forums.set_reg &&
        this.forums.set_reg.register_type === 0
          ? ""
          : "noreg",
      ];
    },
    toWechat() {
      this.$router.push(`/user/wechat?code=${this.code}&preurl=${this.preurl}`);
    },
    toPhonelogin() {
      if (
        this.forums &&
        this.forums.set_reg &&
        this.forums.set_reg.register_type === 1
      ) {
        this.$router.push(
          `/user/phone-login-register?code=${this.code}&preurl=${this.preurl}`
        );
      } else {
        this.$router.push(
          `/user/phone-login?code=${this.code}&preurl=${this.preurl}`
        );
      }
    },
    createCode() {
      window.WwLogin({
        id: "wx_login",
        appid: "ww833808f6b8dc0745",
        agentid: "1000026",
        state: "discuz",
        redirect_uri: "https://discuz.bailuntec.com/user/login",
      });
    },
    getStaffInfo(code) {
      axios
        .get(`https://discuz.bailuntec.com/api/login?code=${code}`)
        .then((res) => {
          if (res.data.data.relationships.users.data.id) {
            localStorage.setItem(
              "user_id",
              res.data.data.relationships.users.data.id
            );
            localStorage.setItem(
              "access_token",
              res.data.data.attributes.access_token
            );
            window.location = "https://discuz.bailuntec.com";
          }
        });
    },
    getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/variable/color.scss";
::v-deep input::-ms-reveal {
  display: none;
}
::v-deep.disable {
  pointer-events: none;
}
.register {
  display: flex;
  width: 415px;
  flex-direction: column;
  .register-title {
    height: 35px;
    font-size: 26px;
  }
  .register-select {
    display: flex;
    flex-direction: column;
    margin-top: 62px;
    border: none;
    background: transparent;
    box-shadow: none;
    .title {
      width: 66px;
      text-align: center;
      display: inline-block;
      color: #606266;
    }
    .title2 {
      margin-right: 10px;
      color: #606266;
    }
    .title:first-child {
      margin-right: 0px;
    }
    .reg-input {
      width: 299px;
      margin-bottom: 20px;
    }
  }
  .agreement {
    margin-left: 70px;
    font-size: 12px;
    .agree {
      color: #6d6d6d;
      font-size: 12px;
    }
  }
  .logorreg {
    width: 300px;
    margin-top: 10px;
    margin-left: 70px;
    display: flex;
    justify-content: space-between;
    .agreement_text {
      color: $color-blue-base;
      cursor: pointer;
      margin-left: -3px;
    }
    .findpass {
      margin-left: 105px;
      color: $color-blue-base;
    }
    .noreg {
      margin-left: 240px;
    }
  }
  .otherlogin {
    font-size: 50px;
    margin-top: 40px;
    margin-left: 70px;
    .wechat-icon {
      cursor: pointer;
    }
    .phone-icon {
      cursor: pointer;
    }
  }
  .r-button {
    width: 300px;
    margin-left: 70px;
    margin-top: 29px;
    background: $color-blue-base;
    transition: all 0.2s ease-out;
    &:hover {
      border: 1px solid $color-blue-deep;
      background: $color-blue-deep;
    }
  }
}
::v-deep.el-tabs {
  .el-tabs__header {
    background: transparent;
  }
  .el-tabs__header .el-tabs__item {
    border: none;
    color: #b5b5b5;
    padding: 0 45px 0 10px;
    font-size: 16px;
    transition: none;
  }
  .el-tabs__header .el-tabs__item:last-child {
    padding-right: 0px;
    padding-left: 25px;
  }
  .el-tabs__header .el-tabs__item.is-active {
    color: black;
    background: transparent;
    border: none;
    font-weight: bold;
    font-size: 26px;
    margin-bottom: 19px;
  }
  .el-tabs__header .el-tabs__item:hover {
    color: $color-blue-deep;
  }
  .el-tabs__nav-wrap {
    margin-bottom: 0px;
  }
  .el-input__inner {
    border-radius: 2px;
  }
  .el-button {
    border-radius: 2px;
  }
}
::v-deep .el-tabs--border-card > .el-tabs__content {
  margin-top: 7px;
}
::v-deep .el-checkbox__input.is-checked .el-checkbox__inner,
.el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: $color-blue-base;
  border-color: $color-blue-base;
}
#wx_login {
  width: 500px;
  height: 500px;
  margin-left: 20%;
}

.login .bg {
  position: fixed;
  z-index: -10;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-image: url(../../assets/login_bg.png);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.login .bg .logo {
  position: absolute;
  left: 10%;
  top: 23%;
}

.login .bg .logo img {
  width: 187px;
  height: 130px;
  vertical-align: middle;
  margin-left: 24%;
}

.login .bg .logo #cn_name {
  letter-spacing: 1px;
  color: #333333;
  font-size: 18px;
  padding-top: 20px;
  text-align: center;
}

.login .bg .logo #en_name {
  letter-spacing: 1px;
  color: #333333;
  font-size: 21px;
  padding-top: 0px;
  text-align: center;
}

.login .form {
  overflow: hidden;
  position: relative;
  float: left;
  padding: 38px 33px 90px 33px;
  margin-left: 50%;
  margin-top: 84px;
  background: #ffffff;
  box-shadow: 0px 4px 20px 0px rgba(0, 0, 0, 0.16);
  border-radius: 20px;
}

.login .form .welcome {
  margin-bottom: 100px;
}

.login .form .welcome p {
  text-align: left;
  color: #18a9d5;
}

.login .form .welcome p:first-child {
  font-size: 24px;
  letter-spacing: 2px;
  margin-bottom: 8px;
}

.login .form .welcome p:last-child {
  font-size: 14px;
  letter-spacing: 1px;
}

.div_footer {
  position: absolute;
  height: 50px;
  text-align: center;
  line-height: 50px;
  width: 100%;
  color: #000000;
  margin-top: 45%;
}

.div_footer a {
  text-decoration: none;
  color: black;
}

.div_footer a p {
  height: 20px;
  line-height: 20px;
  margin: 0px 0px 0px 5px;
  color: #939393;
}
</style>
