<template>
  <div id="login-container">
    <el-card class="login-card" shadow="always">
      <div slot="header" class="clearfix">
        <span>登录</span>
      </div>
      <div class="login-form">
        <el-form :model="form" ref="form" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="form.username" autocomplete="off" @keydown.enter="handleLogin"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" type="password" autocomplete="off"
              @keyup.enter.native="handleLogin"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import api from "@/api/settings/userSetting";

export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      conf: {}
    };
  },
  created() {
    this.getUserConfig();
  },
  methods: {
    getUserConfig() {
      api.getUserConfig().then(res => {
        this.conf = res.conf;
      })
    },

    handleLogin() {
      const { username, password } = this.form;
      const conf = this.conf;
      if (username === conf.username && password === conf.password) {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        setTimeout(() => {
          loading.close();
          sessionStorage.setItem("isFromLoginPage", "true");
          this.$router.push("/settings");

        }, 1000);
      } else {
        this.$message.error('用户名或密码错误');
      }
    }
  }
};
</script>

<style scoped>
#login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.login-card {
  width: 400px;
}

.login-form {
  margin-top: 20px;
}
</style>
