<template>
  <div class="login">
    <div class="login-form">
      <div class="input-box account-input">
        <div class="icon-box">
          <img src="@/assets/img/login/icon_user.png" class="icon" />
        </div>
        <input type="text" placeholder="请输入账号" v-model="account" @keyup.enter="login" />
      </div>
      <div class="input-box password-input">
        <div class="icon-box">
          <img src="@/assets/img/login/icon_password.png" class="icon" />
        </div>
        <input type="password" placeholder="请输入密码" v-model="password" @keyup.enter="login" />
      </div>
      <div class="login-btn" @click="login">登录</div>

      <!-- <div class="ding-login-btn">
        <a :href="dingLoginUrl">钉钉登陆</a>
      </div> -->
    </div>
  </div>
</template>

<script>
  import { defineComponent } from "vue";
  import { login, getUserInfo } from "@/http/request_user";
  import { message } from "ant-design-vue";
  export default defineComponent({
    name: "login",
    data() {
      return {
        account: "",
        password: "",
        dingLoginUrl: "",
      };
    },
    created() {},
    methods: {
      login() {
        if (this.account === "") {
          message.error("请输入用户名");
        } else if (this.account !== "" && this.password === "") {
          message.error("请输入密码");
        } else {
          login({
            username: this.account,
            password: this.password,
          })
            .then((res) => {
              if (res.data.success) {
                message.success("登录成功");
                localStorage.setItem("LUWANG_TOKEN", "Bearer " + res.data.data.token);
                this.getUserInfo();
                this.$router.push("/");
              }
            })
            .catch((err) => {
              if (err.status === 401) {
                message.error("用户名或密码错误");
              }
            });
        }
      },
      async getUserInfo() {
        try {
          const res = await getUserInfo();
          const userInformation = res.data.data;
          userInformation && localStorage.setItem("USER_INFO", JSON.stringify(userInformation));
        } catch (e) {
          console.error(e);
        }
      },
    },
  });
</script>

<style scoped lang="less">
  .login {
    width: 100%;
    height: 100%;
    text-align: center;
    padding-top: 8%;
    box-sizing: border-box;
    font-size: 16px;
    .logo-icon {
      width: 200px;
    }
    input {
      font-size: 16px;
      vertical-align: middle;
    }
  }
  .title {
    color: #333;
    font-size: 34px;
  }
  .sub-title {
    color: #999;
    font-size: 14px;
  }
  .login-form {
    margin: 3% auto 0;
    padding: 20px 20px 60px;
    width: 375px;
    background: #fff;
    border-radius: 8px;
    font-size: 14px;
    box-shadow: 0px 0px 20px 0px #ddd;
  }
  .input-box {
    margin: 20px auto;
    padding: 15px;
    width: 80%;
    border-bottom: 1px solid rgba(152, 152, 152, 0.3);
    text-align: left;
    box-sizing: border-box;
  }
  .icon-box {
    display: inline-block;
    width: 20px;
    text-align: center;
    margin-right: 10px;
    vertical-align: middle;
  }
  .icon {
    height: 20px;
  }
  input {
    border: none;
    outline: none;
    width: 80%;
    height: 25px;
  }
  .login-btn {
    margin: 40px auto 0;
    background: #00acff;
    color: #fff;
    border-radius: 5px;
    width: 80%;
    height: 36px;
    line-height: 36px;
    cursor: pointer;
  }
  .ding-login-btn {
    margin-top: 20px;
    a {
      color: #00acff;
    }
  }
</style>
<style>
  .error-message {
    font-size: 16px;
  }
</style>
