<template>
  <div class="login-page">
    <el-card class="login-card">
      <h2>注册账号</h2>
      <el-form :model="form" @keyup.enter.native="registe">
        <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="registe">注册</el-button>
        <el-button type="text" @click="$router.push('/login')">已有账号？去登录</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "registe",
  data() {
    return {
      form: {
        name: "",
        password: ""
      }
    };
  },
  methods: {
    registe() {
      if (!this.form.name || !this.form.password) {
        this.$message.warning("请输入用户名和密码");
        return;
      }
      this.$axiosJava.post("api/user/add", this.form).then(res => {
        if (res.data) {
          this.$message.success("注册成功");
          this.$router.push("/login");
        } 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%;
}
</style>
