<template>
    <div class="login-container">
      <h2>注册</h2>
      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label for="username">用户名</label>
          <input type="text" id="username" v-model="username" required />
        </div>
        <div class="form-group">
          <label for="email">邮箱</label>
          <input type="email" id="email" v-model="email" required />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input type="password" id="password" v-model="password" required />
        </div>
        <div>
          您提交之后，管理员会进行审核。审核通过后，我们会向您发送邮件告知。
        </div>
        <button type="submit">注册</button>
      </form>
    </div>
  </template>
  
  <script lang="ts" setup>
  import { ref } from 'vue';
  import { useRouter } from 'vue-router';
  import axios from 'axios';
  import { useToast } from 'vue-toastification' 
  const toast = useToast()
  const router = useRouter();
  const username = ref('');
  const email = ref('');
  const password = ref('');
  
  const handleSubmit = () => {
    // 这里可以添加注册逻辑，例如调用 API 进行注册
    console.log('用户名:', username.value);
    console.log('邮箱:', email.value);
    console.log('密码:', password.value);
    axios.post('http://localhost:8080/user/registered', {
      username: username.value,
      email: email.value,
      password: password.value
    })
    .then(response => {
      if(response.data.code==1)
      {
        // 处理成功响应
        console.log('Response:', response.data);
        toast.success("注册成功，请等待管理员审核");
      }
      else{
        toast.success("注册失败，此邮箱已被注册");
      }
    })
    .catch(error => {
      // 处理错误
      console.error('Error:', error);
    });


    // 假设注册成功，重定向到登录页面
    router.push('/login');
  };
  </script>
  
  <style scoped>
  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
  
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f0f0f0; /* 可以根据需要调整背景颜色 */
  }
  
  .login-container {
    max-width: 400px;
    margin: 0 auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    background-color: #f9f9f9;
  }
  
  .form-group {
    margin-bottom: 15px;
  }
  
  label {
    display: block;
    margin-bottom: 5px;
  }
  
  input[type="text"],
  input[type="email"],
  input[type="password"] {
    width: 100%;
    padding: 8px;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 4px;
  }
  
  button {
    width: 100%;
    padding: 10px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
  }
  
  button:hover {
    background-color: #45a049;
  }
  </style>