<template>
  <div class="login-container">
    <el-form ref="loginForm"
             :model="loginForm"
             :rules="loginRules"
             class="login-form"
             auto-complete="on"
             label-position="left">
      <div class="title-container">
        <img src="@/assets/images/base/login-logo.png"
             alt="">
      </div>

      <el-form-item prop="username"
                    class='logitem'>
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="username"
                  @keyup.enter.native="handleLogin"
                  v-model.trim="loginForm.username"
                  placeholder="请输入用户名"
                  class='login-input'
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="off" />
      </el-form-item>

      <el-form-item prop="password"
                    class='logitem'>
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input :key="passwordType"
                  ref="password"
                  class='login-input'
                  v-model.trim="loginForm.password"
                  :type="passwordType"
                  placeholder="请输入密码"
                  name="password"
                  tabindex="2"
                  auto-complete="off"
                  @keyup.enter.native="handleLogin" />
        <span class="show-pwd"
              @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <div class="loginBtn"
           :class="!loginForm.username || !loginForm.password?'disabled':''"
           style="width:100%;margin-bottom:30px;"
           @click="handleLogin">登录</div>
      <div class='remberBox'>
        <!-- <div class="dotWrap">
          <div :class="isSaveUserInfo?'active':''"></div>
        </div> -->
        <el-checkbox v-model="isSaveUserInfo">记住密码</el-checkbox>
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import { Searchs } from '@/helpers'
export default {
  name: "Login",
  data () {
    const validateUsername = (rule, value, callback) => {
      // validUsername
      if (!value) {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码必须大于六位"));
      } else {
        callback();
      }
    }
    return {
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      passwordType: "password",
      redirect: undefined,
      isSaveUserInfo: true
    }
  },
  mounted () {
    this.loginForm.username = localStorage.getItem('username');
    this.loginForm.password = localStorage.getItem('password');

  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd () {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin () {
      // 清除所有savekey
      const s = new Searchs();
       s.removeAll()

      // 判断是否点击了记住密码 =>  存储密码
      if (this.isSaveUserInfo) {
        localStorage.setItem('username', this.loginForm.username);
        localStorage.setItem('password', this.loginForm.password);
      } else {
        localStorage.setItem('username', '');
        localStorage.setItem('password', '');
      }
      this.$refs.loginForm.validate(valid => {
        if (valid) {

          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$nextTick(res => {
                //  这里清空 tab
                this.$store
                  .dispatch("delAllViews")
                this.$router.push({ path: "/main/main" });

              })
            })
            .catch(() => {
            });
        } else {
          return false;
        }
      });
    },
    saveUserInfo () {
      this.isSaveUserInfo = !this.isSaveUserInfo;
    }
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
/deep/.el-input__inner{
   background-color:transparent!important;
   border:1px solid transparent!important
}
.login-container .el-input input:-webkit-autofill{
  background-color:transparent!important;
}
.login-input {
  background-color:transparent!important;
}
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
.loginBtn {
  background-color: #00A79D;
  text-align: center;
  width: 100%;
  height: 51px;
  line-height: 51px;
  color: #fff;
  border-radius: 25px;
  cursor: pointer;
  margin-bottom: 10px !important;
}
.loginBtn.disabled {
  background-color: #777;
}
::-webkit-input-placeholder {
  /* WebKit browsers */
  color: #444;
  font-size: 14px;
}

::-moz-placeholder {
  /* Mozilla Firefox 19+ */
  color: #444;
  font-size: 14px;
}

:-ms-input-placeholder {
  /* Internet Explorer 10+ */
  color: #444;
  font-size: 14px;
}
.logitem {
  border-radius: 25px !important;
  border: 1px solid #444 !important;
  background-color: transparent !important;
}
.login-container .el-input input {
  color: #444 !important;
  caret-color: #444 !important;
}
.remberBox {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  margin-bottom: 50px;
  align-items: center;
  cursor: pointer;
  .dotWrap {
    width: 18px;
    height: 18px;
    border: 1px solid #444;
    border-radius: 50%;
    margin-right: 8px;
    position: relative;
    overflow: hidden;
    .active {
      width: 10px;
      height: 10px;
      background-color: #444;
      border-radius: 50%;
      overflow: hidden;
      position: absolute;
      top: 3px;
      left: 3px;
    }
  }
}
$bg: #fff;
$light_gray: #000;
$cursor: #000;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
    &::first-line {
      color: $light_gray;
    }
  }
}

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

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

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

      &:-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: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #fff;
$dark_gray: #000;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url("../../assets/images/base/login-bg.png") no-repeat 100% 100%;
  background-size: cover;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 500px;
    max-width: 90%;
    padding: 0px 35px;
    margin: 12% auto 0;
    overflow: hidden;
    background-color: rgba(255, 255, 255, 0.85);
    // opacity: 0.85;
    box-shadow: 0px 0px 12px 0px rgba(0, 0, 0, 0.66);
    border-radius: 20px;
  }

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

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

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20% 0;
    img {
      width: 48%;
    }
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

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