<template>
  <div class="register">
    <h2>Registrieren</h2>
    <form @submit.prevent="register">
      <label for="name">Name:</label>
      <input type="text" id="name" v-model="name" required>
      
      <label for="email">Email-Adresse:</label>
      <input type="email" id="email" v-model="email" required>
      
      <label for="password">Passwort:</label>
      <input type="password" id="password" v-model="password" required>
      
      <button type="submit">Registrieren</button>
    </form>
    <p>
      <router-link to="/login">Login</router-link>
    </p>
    <p>
      <router-link to="/forgot-password">Passwort vergessen?</router-link>
    </p>

    <div v-if="dialogVisible" class="dialog">
      <div class="dialog-content">
        <h3>{{ dialogTitle }}</h3>
        <p>{{ dialogMessage }}</p>
        <button type="button" @click="closeDialog">Schließen</button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '../../axios';

export default {
  name: 'RegisterComponent',
  components: {
  },
  data() {
    return {
      name: '',
      email: '',
      password: '',
      dialogTitle: '',
      dialogMessage: '',
      dialogVisible: false
    };
  },
  methods: {
    async register() {
      try {
        const response = await axios.post('/auth/register', {
          name: this.name,
          email: this.email,
          password: this.password
        });
        this.showDialog('Registrierung erfolgreich', response.data?.message || 'Ihr Konto wurde erfolgreich erstellt.');
        this.name = '';
        this.email = '';
        this.password = '';
      } catch (err) {
        const message = err?.response?.data?.message || err?.message || 'Ein unbekannter Fehler ist aufgetreten';
        this.showDialog('Fehler', message);
      }
    },
    showDialog(title, message) {
      this.dialogTitle = title;
      this.dialogMessage = message;
      this.dialogVisible = true;
    },
    closeDialog() {
      this.dialogVisible = false;
    }
  }
};
</script>

<style scoped>
.register {
  max-width: 400px;
  margin: auto;
}
form {
  display: flex;
  flex-direction: column;
}
label {
  margin-top: 10px;
}
button {
  margin-top: 20px;
}
.dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
}
.dialog-content {
  background: #fff;
  padding: 16px;
  border-radius: 4px;
  max-width: 420px;
  width: 90%;
}
</style>
