<template>
  <div class="login-page">
    <el-card class="login-card">
      <h2>个人记账系统</h2>
      <el-form :model="form" @keyup.enter.native="login">
        <el-form-item>
          <el-input v-model="form.name" prefix-icon="el-icon-user" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.password" prefix-icon="el-icon-lock" placeholder="密码" show-password></el-input>
        </el-form-item>
        <el-button type="primary" class="login-btn" @click="login">登录</el-button>
        <el-button type="text" @click="$router.push('/registe')">没有账号？去注册</el-button>
      </el-form>
      <p class="hint">测试账号：admin / 123456</p>
    </el-card>
  </div>
</template>

<script>
import Cookie from "js-cookie";

export default {
  name: "login",
  data() {
    return {
      form: {
        name: "admin",
        password: "123456"
      }
    };
  },
  methods: {
    login() {
      this.$axiosJava.post("api/user/login", this.form).then(res => {
        if (res.data && res.data.success) {
          Cookie.set("token", JSON.stringify(res.data.user), { expires: 100 });
          this.$message.success("登录成功");
          this.$router.push("/");
        } else {
          this.$message.error("用户名或密码错误");
        }
      });
    }
  }
};
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #ecf5ff, #f8fbff);
}

.login-card {
  width: 380px;
  border-radius: 8px;
}

.login-card h2 {
  margin: 0 0 24px;
  text-align: center;
  color: #303133;
}

.login-btn {
  width: 100%;
}

.hint {
  margin: 12px 0 0;
  color: #909399;
  font-size: 13px;
  text-align: center;
}
</style>
