<template>
  <div class="login-page">
    <div class="login-card">
      <h2>欢迎登录</h2>
      <el-form :model="form" label-position="top">
        <el-form-item label="账号">
          <el-input v-model="form.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-button type="primary" style="width: 100%;" @click="handleLogin">登录</el-button>
      </el-form>
      <div class="login-footer">
        <span>还没有账号？</span>
        <el-button type="text" @click="$router.push('/register')">注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "../api";

export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      }
    };
  },
  methods: {
    async handleLogin() {
      try {
        const res = await login(this.form);
        const data = res.data.data;
        localStorage.setItem("token", data.token);
        localStorage.setItem("role", data.role);
        if (data.role === "ADMIN") {
          this.$router.push("/admin");
        } else if (data.role === "NURSE") {
          this.$router.push("/nurse");
        } else {
          this.$router.push("/family");
        }
      } catch (e) {
        this.$message.error(e.message || "登录失败");
      }
    }
  }
};
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 20% 20%, #dff3e7, #f3f8f3);
}
.login-card {
  width: 360px;
  padding: 28px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.12);
}
.login-footer {
  margin-top: 12px;
  text-align: center;
}
</style>

