{% load static %}
{% if help_obj %}
<div class="card border-info mb-3" style="max-width: 350px;">
    <div class="card-header bg-info text-white d-flex justify-content-between align-items-center">
        <h6 class="mb-0">
            <i class="fas fa-info-circle me-2"></i>{{ help_obj.title }}
        </h6>
        {% if can_edit %}
            <button class="btn btn-sm btn-outline-light" onclick="editHelpBox('{{ page_key }}')" title="Bearbeiten">
                <i class="fas fa-edit"></i>
            </button>
        {% endif %}
    </div>
    <div class="card-body">
        <div class="help-content">
            {{ content_html }}
        </div>
        {% if can_edit %}
            <div class="mt-2">
                <small class="text-muted">
                    <i class="fas fa-user me-1"></i>{{ help_obj.updated_by|default:"System" }}
                    <i class="fas fa-clock ms-2 me-1"></i>{{ help_obj.updated_at|date:"d.m.Y H:i" }}
                </small>
            </div>
        {% endif %}
    </div>
</div>

{% if can_edit %}
<!-- Modal für Bearbeitung -->
<div class="modal fade" id="helpBoxModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Hilfs-Infobox bearbeiten</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="helpBoxForm" method="post" action="{% url 'stiftung:edit_help_box' %}">
                <div class="modal-body">
                    {% csrf_token %}
                    <input type="hidden" name="page_key" value="{{ page_key }}">
                    
                    <div class="mb-3">
                        <label for="title" class="form-label">Titel</label>
                        <input type="text" class="form-control" id="title" name="title" 
                               value="{{ help_obj.title }}" required>
                    </div>
                    
                    <div class="mb-3">
                        <label for="content" class="form-label">Inhalt</label>
                        <textarea class="form-control" id="content" name="content" rows="10" required>{{ help_obj.content }}</textarea>
                        <div class="form-text">
                            <strong>Markdown Syntax:</strong><br>
                            **fett** | *kursiv* | `code` | [Link](url)<br>
                            - Liste | 1. Nummerierte Liste | &gt; Zitat
                        </div>
                    </div>
                    
                    <div class="mb-3 form-check">
                        <input type="checkbox" class="form-check-input" id="is_active" name="is_active" 
                               {% if help_obj.is_active %}checked{% endif %}>
                        <label class="form-check-label" for="is_active">
                            Aktiv (anzeigen)
                        </label>
                    </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">Speichern</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function editHelpBox(pageKey) {
    var modal = new bootstrap.Modal(document.getElementById('helpBoxModal'));
    modal.show();
}
</script>
{% endif %}

{% elif can_edit %}
<!-- Keine Hilfsbox vorhanden - Button zum Erstellen -->
<div class="card border-secondary mb-3" style="max-width: 350px;">
    <div class="card-body text-center">
        <p class="text-muted mb-2">Keine Hilfe verfügbar</p>
        <button class="btn btn-outline-primary btn-sm" onclick="createHelpBox('{{ page_key }}')">
            <i class="fas fa-plus me-1"></i>Hilfe erstellen
        </button>
    </div>
</div>

<!-- Modal für neue Hilfsbox -->
<div class="modal fade" id="createHelpBoxModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Neue Hilfs-Infobox erstellen</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="createHelpBoxForm" method="post" action="{% url 'stiftung:edit_help_box' %}">
                <div class="modal-body">
                    {% csrf_token %}
                    <input type="hidden" name="page_key" value="{{ page_key }}">
                    
                    <div class="mb-3">
                        <label for="new_title" class="form-label">Titel</label>
                        <input type="text" class="form-control" id="new_title" name="title" 
                               placeholder="z.B. Hilfe beim Erstellen" required>
                    </div>
                    
                    <div class="mb-3">
                        <label for="new_content" class="form-label">Inhalt</label>
                        <textarea class="form-control" id="new_content" name="content" rows="10" 
                                  placeholder="Geben Sie hier die Hilfsinformationen ein..." required></textarea>
                        <div class="form-text">
                            <strong>Markdown Syntax:</strong><br>
                            **fett** | *kursiv* | `code` | [Link](url)<br>
                            - Liste | 1. Nummerierte Liste | &gt; Zitat
                        </div>
                    </div>
                    
                    <div class="mb-3 form-check">
                        <input type="checkbox" class="form-check-input" id="new_is_active" name="is_active" checked>
                        <label class="form-check-label" for="new_is_active">
                            Aktiv (anzeigen)
                        </label>
                    </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">Erstellen</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function createHelpBox(pageKey) {
    var modal = new bootstrap.Modal(document.getElementById('createHelpBoxModal'));
    modal.show();
}
</script>
{% endif %}
