<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
      label-width="0px"
    >
      <div class="title-container">
        <h3 class="title">用户登录</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <span class="el-icon-s-custom"></span>
        </span>
        <el-input class="inps" placeholder="账号" v-model="loginForm.account">
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <span class="el-icon-lock"></span>
        </span>
        <el-input
          class="inps"
          type="password"
          placeholder="密码"
          v-model="loginForm.passWord"
          show-password
        ></el-input>
      </el-form-item>

      <div style="text-align: center">
        <el-button
          type="primary"
          style="width: 40%; margin-top: 20px"
          round
          class="submitBtn"
          @click="submitForm"
          >登录
        </el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api/user";
export default {
  name: "Login",
  data: function () {
    return {
      loginForm: {
        account: "",
        passWord: "",
      },
      loginRules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        passWord: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      const userAccount = this.loginForm.account;
      const userPassword = this.loginForm.passWord;
      if (!userAccount) {
        return this.$message({
          type: "error",
          message: "账号不能为空！",
        });
      }
      if (!userPassword) {
        return this.$message({
          type: "error",
          message: "密码不能为空！",
        });
      }
      login({ userName: userAccount, userPwd: userPassword }).then((res) => {
        if (res) {
          localStorage.setItem("userName", res.userName);
          localStorage.setItem("userId", Number(res.id));
          localStorage.setItem("userType", res.userClass);
          switch (res.userClass) {
            case "1":
              this.$router.push("/home");
              break;
            case "2":
              this.$router.push("/daichuli");
              break;
            case "3":
              this.$router.push("/daishenhe");
              break;
          }
        }
      });
    },
  },
};
</script>

<style lang="less">
@supports (-webkit-mask: none) and (not (cater-color: #fff)) {
  .login-container .el-input input {
    color: #fff;
  }
}

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

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #fff;
      height: 47px;
      caret-color: #fff;

      :-webkit-autofill {
        box-shadow: 0 0 0px 1000px #283443 inset !important;
        -webkit-text-fill-color: #fff !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="less" scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 360px;
    max-width: 100%;
    padding: 200px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

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

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #eee;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }
}
</style>
