<template>
  <div class="login-container">
    <!-- <div class="title-container">
      <h3 class="title">重卡换电智能站控系统</h3>
    </div> -->
    <div class="login-bg">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <div class="login-logo"><img src="./assets/logo.png" /></div>
        <div class="login-title">登录</div>
        <div class="login-list">
          <!-- <p>账号：</p> -->
          <el-form-item tabindex="1" prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              name="Username"
              type="text"
              autocomplete="on"
            />
          </el-form-item>
        </div>
        <div class="login-list">
          <!-- <p>密码：</p> -->
          <el-form-item tabindex="2" prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              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' : 'eye-open'"
              />
            </span>
          </el-form-item>
        </div>
        <div class="login-list">
          <el-form-item prop="code" class="yzm">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              class="yzm-input"
              @keyup.enter.native="handleLogin"
            >
            <svg-icon slot="prefix" icon-class="secured" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item>
        </div>
        <el-checkbox
          v-model="loginForm.rememberMe"
          >记住密码</el-checkbox>
        <div class="login-list">
          <el-button :loading="loading" @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </div>
      </el-form>
    </div>
    <div class="copyright">
      Copyright©2022-2023 苏州睿锂物联科技有限公司版权所有
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error("用户名错误"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不少于6位"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "88888888",
        rememberMe: false,
        code: "1111",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      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,
    },
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    getCode() {
      this.codeUrl = "data:image/gif;base64,";
    },
    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();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: "/power-change-monitoring",
              });
              this.loading = false;
            })
            .catch((err) => {
              this.$message({
                type: "error",
                message: "登录失败!",
              });
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #1d2129;
$cursor: #86909c;

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

/* reset element-ui css */
.login-container {
  .login-code {
    width: 33%;
    height: 38px;
    float: right;
    background-color: #FFFFFF;
    border-radius: 3px;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .login-img {
    height: 80px;
    width: 300px;
    margin: 0 50px 20px 50px;
  }
  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 8px 5px 8px 15px;
      color: $light_gray;
      caret-color: $cursor;
      font-size: 16px;

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

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 3px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url("./assets/login-bg.png");
  background-size: 100% 100%;
  overflow: hidden;

  .login-bg {
    position: relative;
    width: 500px;
    height: 600px;
    background: rgba(60, 66, 84, 0.2);
    border-radius: 8px;
    border: 1px solid #3c4254;
    margin-left: auto;
    margin-right: 211px;
    top: 19.5vh;
    padding: 0px 50px;
    box-sizing: border-box;

    
    .login-list {
      display: flex;
      flex-direction: row;
      width: 100%;
      margin: 0 auto;
      .yzm{
        background: none;
        border: none;
       .el-input{
        background-color: #FFFFFF;
        border-radius: 3px;
       }
      }
      .input-icon {
        height: 18px;
        width: 18px;
        margin-left: 8px;
        vertical-align: middle;
      }
      .yzm-input{
        width: 63%;
        padding-left: 30px;
      }
      
      p {
        font-size: 16px;
        color: white;
        width: 48px;
        margin: 0;
        padding: 0;
        line-height: 46px;
      }
    }
    .login-logo {
      width: 286px;
      height: 65px;
      margin-top: 55px;
    }
    .login-title {
      font-size: 20px;
      font-weight: 400;
      color: #bfc1cb;
      margin-top: 60px;
      margin-bottom: 32px;
    }
  }
  .copyright {
    width: 100%;
    text-align: center;
    position: absolute;
    bottom: 40px;
    font-size: 15px;
    font-weight: 400;
    color: #757b8c;
  }
  .title-container {
    position: relative;
    top: 26vh;

    .title {
      font-size: 32px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      letter-spacing: 1px;
    }
  }

  .el-button {
    border: none !important;
    height: 40px;
    font-size: 15px;
    color: #ffffff;
    letter-spacing: 1px;
    margin-top: 20px;
    flex: 1;
    background: #246cf9;
    border-radius: 3px;
    font-weight: 400;
  }

  .login-form {
    position: relative;
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
    ::v-deep .el-checkbox__label{
        font-size: 14px;
        font-weight: 400;
        color: #BFC1CB !important;
    }
  }

  .el-form-item {
    margin-bottom: 25px;
    flex: 1;
    background-color: #ffffff;
    border: 1px solid #246cf9;
  }

  .el-form-item:focus-within {
    // background-color: rgb(17, 31, 32, 0.9);
    border: 1px solid rgb(110, 236, 251, 0.9);
  }

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

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

  .svg-container {
    padding: 0px 5px 0px 12px;
    color: #6eecfb;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    .svg-icon {
      color: #959ea8;
    }
  }

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

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

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