<template>
  <div class="box">
    <div class="loginBox">
      <h2>用户登录</h2>
      <form @submit="login">
        <div class="item">
          <input type="text" v-model="username" required />
          <label for="">用户名</label>
        </div>
        <div class="item">
          <input type="password" v-model="password" required />
          <label for="">密码</label>
        </div>
      </form>
      <button class="btn" @click="login">
        登录
        <span></span>
        <span></span>
        <span></span>
        <span></span>
      </button>
    </div>
  </div>
</template>

<script>
export default {
  components: {},

  data() {
    return {
      username: localStorage.getItem("usernameEn") || "",
      password: "",
    };
  },

  created() {
    const that = this;
    document.onkeydown = function (event) {
      const e = event ? event : window.event ? window.event : null;
      if (e.keyCode == 13) {
        that.login();
      }
    };
  },

  mounted() {},

  methods: {
    login() {
      const that = this;
      if (!that.username || !that.password) {
        that.BASE.showMsg({
          msg: "账号密码不可为空",
        });
      } else {
        that.API.requestData({
          isMust: false,
          method: "POST",
          baseURL: "http://192.168.10.5:9002/",
          subUrl: "sys/login",
          data: {
            username: that.username,
            password: that.password,
          },
          success(res) {
            localStorage.setItem("authToken", res.data.token);
            localStorage.setItem("usernameEn", res.data.user.code);
            localStorage.setItem("username", res.data.user.name);
            location.reload();
          },
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgb(0, 11, 32);
  color: #03e9f4;
}
.loginBox {
  width: 500px;
  height: 364px;
  border-radius: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  box-shadow: 0px 15px 25px 0px rgba(0, 0, 0, 0.6);
  padding: 40px;
  box-sizing: border-box;
}

h2 {
  text-align: center;
  color: #fff;
  margin-bottom: 30px;
}

.item {
  height: 45px;
  border-bottom: 1px solid #fff;
  margin-bottom: 40px;
  position: relative;
}

.item input {
  width: 100%;
  height: 100%;
  color: #fff;
  /* 输入框输入光标为白色 */
  padding-top: 20px;
  box-sizing: border-box;
}

/* valid 合法状态下  focus 获取焦点时*/

.item input:focus + label,
.item input:valid + label {
  top: 0;
  font-size: 12px;
}

.item label {
  position: absolute;
  left: 0;
  top: 12px;
  transition: all 0.2s linear;
  /* linear 匀速 */
}

/* 重置输入框和按钮的样式 */

input,
button {
  background: transparent;
  border: none;
  outline: none;
}

.btn {
  padding: 10px 20px;
  margin-top: 30px;
  color: #000;
  position: absolute;
  left: calc(50% - 44px);
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: 0.2s;
  background: #03e9f4;
  font-size: 16px;
  border-radius: 8px;
}

.btn:hover {
  background-color: transparent;
  color: #03e9f4;
  transition: 0.2s;
  /* text-transform: uppercase; */
  /* 文字大写 */
  /* letter-spacing: 2px; */
  /* 字母间隔 */
}

.btn > span {
  position: absolute;
}

.btn > span:nth-child(1) {
  width: 100%;
  height: 2px;
  background-image: linear-gradient(to right, transparent, #03e9f4);
  left: -100%;
  top: 0;
  animation: span-line-one 1s linear infinite;
}

.btn > span:nth-child(2) {
  width: 2px;
  height: 100%;
  background-image: linear-gradient(to bottom, transparent, #03e9f4);
  right: 0;
  top: -100%;
  /* 延迟 0.25s */
  animation: span-line-two 1s 0.25s linear infinite;
}

.btn > span:nth-child(3) {
  width: 100%;
  height: 2px;
  background-image: linear-gradient(to left, transparent, #03e9f4);
  left: 100%;
  bottom: 0;
  /* 延迟 0.5s */
  animation: span-line-three 1s 0.5s linear infinite;
}

.btn > span:nth-child(4) {
  width: 2px;
  height: 100%;
  background-image: linear-gradient(to top, transparent, #03e9f4);
  left: 0;
  bottom: -100%;
  /* 延迟 0.75s */
  animation: span-line-four 1s 0.75s linear infinite;
}

@keyframes span-line-one {
  50%,
  100% {
    left: 100%;
  }
}

@keyframes span-line-two {
  50%,
  100% {
    top: 100%;
  }
}

@keyframes span-line-three {
  50%,
  100% {
    left: -100%;
  }
}

@keyframes span-line-four {
  50%,
  100% {
    bottom: 100%;
  }
}
</style>