{% extends "base.html" %}
{% block title %}Onboarding-Einladungen{% endblock %}

{% block content %}
<div class="container-fluid py-4">
  <div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Onboarding-Einladungen</h1>
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#einladungModal">
      + Neue Einladung
    </button>
  </div>

  <!-- Neue Einladung Modal -->
  <div class="modal fade" id="einladungModal" tabindex="-1">
    <div class="modal-dialog">
      <div class="modal-content">
        <form method="post" action="{% url 'stiftung:onboarding_einladung_senden' %}">
          {% csrf_token %}
          <div class="modal-header">
            <h5 class="modal-title">Neue Onboarding-Einladung senden</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
          </div>
          <div class="modal-body">
            <div class="mb-3">
              <label for="email" class="form-label">E-Mail-Adresse <span class="text-danger">*</span></label>
              <input type="email" class="form-control" id="email" name="email" required>
            </div>
            <div class="row g-2">
              <div class="col-6">
                <label for="vorname" class="form-label">Vorname (optional)</label>
                <input type="text" class="form-control" id="vorname" name="vorname">
              </div>
              <div class="col-6">
                <label for="nachname" class="form-label">Nachname (optional)</label>
                <input type="text" class="form-control" id="nachname" name="nachname">
              </div>
            </div>
            <div class="alert alert-info mt-3 mb-0 small">
              Der eingeladene Kandidat erhält einen Einmal-Link per E-Mail (gültig 30 Tage), über den er das mehrstufige Onboarding-Formular ausfüllen kann.
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
            <button type="submit" class="btn btn-primary">Einladung senden</button>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Tabelle -->
  <div class="card">
    <div class="card-body p-0">
      <table class="table table-hover mb-0">
        <thead class="table-light">
          <tr>
            <th>E-Mail</th>
            <th>Name</th>
            <th>Status</th>
            <th>Gültig bis</th>
            <th>Eingeladen von</th>
            <th>Abgeschlossen</th>
            <th>Destinatär</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          {% for e in einladungen %}
          <tr>
            <td>{{ e.email }}</td>
            <td>{{ e.vorname }} {{ e.nachname }}</td>
            <td>
              {% if e.status == "offen" %}
                <span class="badge bg-success">Offen</span>
              {% elif e.status == "abgeschlossen" %}
                <span class="badge bg-primary">Abgeschlossen</span>
              {% elif e.status == "abgelaufen" %}
                <span class="badge bg-secondary">Abgelaufen</span>
              {% elif e.status == "widerrufen" %}
                <span class="badge bg-danger">Widerrufen</span>
              {% endif %}
            </td>
            <td>{{ e.gueltig_bis|date:"d.m.Y" }}</td>
            <td>{{ e.eingeladen_von.get_full_name|default:e.eingeladen_von.username|default:"–" }}</td>
            <td>{{ e.abgeschlossen_am|date:"d.m.Y H:i"|default:"–" }}</td>
            <td>
              {% if e.destinataer %}
                <a href="{% url 'stiftung:destinataer_detail' pk=e.destinataer.id %}">
                  {{ e.destinataer.vorname }} {{ e.destinataer.nachname }}
                  {% if not e.destinataer.unterstuetzung_bestaetigt %}
                    <span class="badge bg-warning text-dark ms-1">Freigabe ausstehend</span>
                  {% endif %}
                </a>
              {% else %}–{% endif %}
            </td>
            <td>
              {% if e.status == "offen" %}
              <form method="post" action="{% url 'stiftung:onboarding_einladung_widerrufen' pk=e.id %}"
                    onsubmit="return confirm('Einladung für {{ e.email }} widerrufen?');">
                {% csrf_token %}
                <button type="submit" class="btn btn-sm btn-outline-danger">Widerrufen</button>
              </form>
              {% endif %}
            </td>
          </tr>
          {% empty %}
          <tr><td colspan="8" class="text-center text-muted py-4">Keine Onboarding-Einladungen vorhanden.</td></tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
</div>
{% endblock %}
