<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="formData"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <div class="title">用户登录</div>
        <div class="second">User login</div>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="zhanghu" />
        </span>
        <el-input
          ref="username"
          v-model="formData.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          auto-complete="on"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="icon-password" />
        </span>
        <el-input
          ref="password"
          v-model="formData.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;background:rgba(91,140,255,1);box-shadow:0px 6px 10px 0px rgba(59,119,227,0.2);"
        @click.native.prevent="handleLogin"
      >
        立即登录
      </el-button>
    </el-form>
    <div class="footer">技术支持：广州方阵科技有限公司</div>
  </div>
</template>

<script>
import { login } from "@/api/login/index";
import Cookies from "js-cookie";
import { setToken,setSessionId } from "@/utils/auth";
import store from "@/store";

export default {
  name: "Login",
  data() {
    return {
      formData: {
        // username: 'enterprise_admin',
        // password: '123456'
        // username: "admin",
        // password: "cementadmin123"        
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [{ required: true, trigger: "blur", message: "密码不能为空" }]
      },
      passwordType: "password",
      loading: false,
      redirect: ""
    };
  },
  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();
      });
    },
    async handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http(
            login(this.formData),
            res => {
              // 登录成功，跳转到主页
              this.loading = false;
              if (res.code === 0) {
                this.$store.dispatch("setUser", res.data).then(() => {
                  location.reload(); // 为了重新实例化vue-router对象 避免bug
                  // // 重新实例化再进入
                  setTimeout(() => {
                    this.$router.push({ path: "/init" });
                  }, 100); // 加上定时器效果更好，
                });
                localStorage.setItem('sessionId',res.data)
                setToken(res.data);
                this.loading = false;
                this.$message({
                  message: res.message,
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.message || "用户名或密码错误",
                  type: "warning"
                });
              }
            },
            res => {
              // 登录失败
              this.loading = false;
            }
          );
        } else {
          this.loading = false;
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #5b8cff;
$light_gray: #eee;
$cursor: #fff;

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

/* reset element-ui css */
.login-container {
  width: 100%;
  height: 100%;
  background: url("../../assets/common/bg.png") no-repeat;
  background-size: contain;

  .footer {
    width: 100%;
    position: fixed;
    bottom: 59px;
    font-size: 16px;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
    opacity: 0.6;
    text-align: center;
  }

  .el-form-item__content {
    background: rgba(255, 255, 255, 1);
    border: 1px solid rgba(153, 153, 153, 0.5);
    border-radius: 5px;
    box-sizing: border-box;
  }
  .el-input {
    display: inline-block;
    height: 40px;
    width: 85% !important;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 8px 5px 8px 0px;
      color: #666666;
      height: 40px;
      /*caret-color: #000;*/
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #000 !important;
      }
    }
  }

  .el-form-item.is-success .el-input__inner,
  .el-form-item.is-success .el-input__inner:focus,
  .el-form-item.is-success .el-textarea__inner,
  .el-form-item.is-success .el-textarea__inner:focus {
    border: none;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #515151;
    margin-bottom: 30px;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #5b8cff;
$dark_gray: #889aa4;
$light_gray: #4876e9;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: absolute;
    top: 45%;
    left: 30%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 3px 30px 0px rgba(13, 44, 115, 0.28);
    border-radius: 10px;
    position: relative;
    width: 450px;
    max-width: 100%;
    padding: 80px 50px;
    margin: 0 auto;
    overflow: hidden;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    margin: 0px 8px;
    color: #999999;
    vertical-align: middle;
    width: 26px;
    display: inline-block;
    text-align: center;
    margin-top: 10px;
    .svg-icon {
      width: 26px;
      height: 26px;
    }
  }
  .title-container {
    position: relative;
    margin-bottom: 30px;
    .title {
      font-size: 24px;
      font-weight: 400;
      color: $light_gray;
      text-align: left;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }

    .second {
      margin-top: 9px;
      font-size: 14px;
      font-weight: 400;
      color: rgba(149, 149, 149, 1);
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
    .svg-icon {
      width: 20px;
      height: 20px;
    }
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
}
</style>
