<template>
  <div id="particles" class="login-container">
    <div class="title-container">
      <img src="/pic/login/logo.png" alt="" />
    </div>
    
      <div class="login-center">
        <el-form
          ref="loginForm"
          :model="loginForm"
          class="login-form"
          autocomplete="on"
          label-position="left"
        >
          <el-form-item prop="phone">
            <svg-icon icon-class="mobile" class="svg-container" />

            <el-input
              ref="phone"
              v-model="loginForm.phone"
              placeholder="请输入手机号码"
              name="phone"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-form-item v-if="loginByPassword" prop="password">
            <svg-icon icon-class="lock-1" class="svg-container" />
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye-1' : 'eye-2'" />
            </span>
          </el-form-item>

          <el-form-item v-if="!loginByPassword || needSendCode" prop="code">
            <svg-icon icon-class="secrity" class="svg-container" />
            <el-input
              key="code"
              ref="code"
              v-model="loginForm.code"
              type="text"
              placeholder="请输入验证码"
              name="code"
              tabindex="3"
              autocomplete="on"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd button-code">
              <a style="color: #1890ff; font-size: 14px" @click="handleSendCode">{{
                reSend
              }}</a>
            </span>
          </el-form-item>

          <div class="no-bg">
            <div class="more-select" style="float: right">
              <div style="margin-bottom: 20px; margin-top: 10px">
                <a
                  v-if="loginByPassword"
                  style="color: #1890ff; font-size: 14px"
                  @click="loginByPassword = false"
                  >验证码登录</a
                >
                <a
                  v-else
                  style="color: #1890ff; font-size: 14px"
                  @click="loginByPassword = true"
                  >账号密码登录</a
                >
              </div>
            </div>
          </div>
          <el-button
            :loading="loading"
            type="primary"
            @click.native.prevent="handleLogin"
          >
            {{ $t("login.logIn") }}
          </el-button>
        </el-form>
      </div>
 

    <div class="footer-box">
      <div class="inner">
        <div class="text">
          <p>Copyright © 2013-2020 广东智莺科技有限公司 版权所有 粤ICP备13020798号</p>
        </div>
      </div>
    </div>

    <el-dialog :title="$t('login.thirdparty')" :visible.sync="showDialog">
      {{ $t("login.thirdpartyTips") }}
      <social-sign />
    </el-dialog>
  </div>
</template>

<script>
import SocialSign from "./components/SocialSignin";
import particles from "particles.js";

export default {
  name: "Login",
  components: { SocialSign },
  data() {
    return {
      loginByPassword: true,
      needSendCode: false,
      isDisabled: false,
      reSend: "发送验证码",
      count: "",
      timer: null,
      loginForm: {
        phone: "",
        password: "",
        code: "",
        checked: true,
      },
      // loginRules: {
      //   phone: [{ required: true, trigger: 'blur', validator: validatePhone }],
      //   password: [
      //     { required: true, trigger: 'blur', validator: validatePassword }
      //   ]
      // },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
    loginByPassword: function (newQuestion, oldQuestion) {
      this.loginForm.password = "";
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    particlesJS.load("particles", "/particles.json", function () {
      console.log("callback - particles.js config loaded");
    });
    if (this.loginForm.phone === "") {
      this.$refs.phone.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleSendCode() {
      console.log(this.loginForm);
      if (this.isDisabled) {
        return;
      }
      this.$store
        .dispatch("user/sendCode", this.loginForm)
        .then(() => {
          this.countDown();
          this.$message.success("验证码已发送");
        })
        .catch(() => {});
    },
    handleLogin() {
      this.loginForm.type = this.loginByPassword ? "pwd" : "code";
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then((code) => {
              if (code === 401) {
                this.needSendCode = true;
                this.$message.warning("为了您的账户安全，请输入手机验证码校验");
              } else {
                this.$router.push({
                  path: this.redirect || "/",
                  query: this.otherQuery,
                });
              }

              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },

    countDown() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.isDisabled = true;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
            this.reSend = "剩余" + this.count + "秒";
          } else {
            this.isDisabled = false;
            clearInterval(this.timer);
            this.timer = null;
            this.reSend = "重新发送";
          }
        }, 1000);
      }
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #fff;
$light_gray: #333;
$cursor: #3a3a3a;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  // width: 100vw;
  // height: 100vh;
  // overflow: hidden;
  .el-input {
    display: inline-block;
    height: 44px;
    width: 80%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 44px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-checkbox__input.is-checked + .el-checkbox__label {
    color: #fff;
  }
  .el-form-item {
    border: 1px solid #dcdcdc;
    border-radius: 5px;
    color: #454545;
    margin-bottom: 32px;
  }

  .el-form-item:hover {
    border: 1px solid #1890ff;
    border-radius: 5px;
    color: #454545;
  }

  .el-form-item__error {
    margin-top: 8px;
  }

  .no-bg {
    background-color: transparent;
    border: none;
    padding: 0 10px;
    .el-checkbox {
      color: #fff;
    }
    .el-button {
      color: #fff;
    }
  }

  .el-button {
    width: 100%;
    margin-bottom: 30px;
    height: 44px;
    span {
      font-size: 20px;
    }
  }
}
</style>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;

  height: 100%;
  width: 100%;
  background: url("/pic/login/login_bg.png") no-repeat top center;
  background-size: cover;
  overflow: hidden;

  .main-container {
 
    z-index: 99;
  }

  .login-center {
    padding: 0;
    width: 60%;
    min-width: 400px;
    max-width: 500px;
    margin: 5% auto;
    position: fixed;
    z-index: 999;
    left: 0;
    right: 0;
  }

  .login-form {
    padding: 10% 16% 6% 16%;
    background-color: #fff;

    box-shadow: 0px 2px 20px rgba(0, 0, 0, 0.16);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #333;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 6px 6px 6px;
    vertical-align: middle;
    text-align: center;
    width: 36px;
    height: 36px;
    display: inline-block;
  }

  .title-container {
    // position: relative;
    height: 20%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 auto;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: -50px auto 100px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: $white;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
  .more-select {
    display: flex;
    justify-content: space-between;
  }

  .footer-box {
    .inner {
      display: flex;
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
      padding: 20px;
      align-items: flex-end;
      justify-content: center;
      .text {
        color: #fff;
        display: flex;
        font-size: 12px;
      }
    }
  }
  @media only screen and (max-height: 500px) {
    .footer-box {
      display: none;
    }
  }
  @media only screen and (max-width: 450px) {
    .footer-box {
      .inner {
        text-align: center;
        color: #fff;
        height: 60px;
        background-color: transparent;
        .text {
          display: block;
        }
      }
    }
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}

 
</style>
