{% extends 'base.html' %}
{% load static %}

{% block title %}Alle Dokumente - Stiftungsverwaltung{% endblock %}

{% block content %}

    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3 mb-0">
                    <i class="fas fa-file-alt text-primary me-2"></i>
                    Alle Dokumente
                </h1>
                <div>
                    <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-info me-2">
                        <i class="fas fa-external-link-alt me-1"></i>Dokumentenverwaltung
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Verknüpfte Dokumente -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-success">
                        <i class="fas fa-link me-2"></i>Verknüpfte Dokumente ({{ dokumente|length }})
                    </h6>
                </div>
                <div class="card-body">
                    {% if dokumente %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Dokument</th>
                                        <th>Kontext</th>
                                        <th>Verknüpft mit</th>
                                        <th>Aktionen</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for dokument in dokumente %}
                                    <tr>
                                        <td>
                                            <strong>{{ dokument.titel }}</strong>
                                            <br>
                                            <small class="text-muted">ID: {{ dokument.paperless_document_id }}</small>
                                        </td>
                                        <td>
                                            <span class="badge bg-secondary">{{ dokument.get_kontext_display }}</span>
                                        </td>
                                        <td>
                                            {% if dokument.verpachtung_id %}
                                                <span class="badge bg-info">Verpachtung</span>
                                            {% elif dokument.land_id %}
                                                <span class="badge bg-success">Länderei</span>
                                            {% elif dokument.paechter_id %}
                                                <span class="badge bg-primary">Pächter</span>
                                            {% elif dokument.destinataer_id %}
                                                <span class="badge bg-warning">Destinatär</span>
                                            {% elif dokument.foerderung_id %}
                                                <span class="badge bg-secondary">Förderung</span>
                                            {% else %}
                                                <span class="text-muted">Keine Verknüpfung</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <a href="{{ dokument.get_paperless_url }}" target="_blank" class="btn btn-sm btn-outline-primary" title="In Paperless öffnen">
                                                    <i class="fas fa-external-link-alt"></i>
                                                </a>
                                                <a href="{% url 'stiftung:dokument_detail' dokument.pk %}" class="btn btn-sm btn-outline-info" title="Details">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{% url 'stiftung:dokument_update' dokument.pk %}" class="btn btn-sm btn-outline-warning" title="Bearbeiten">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <div class="text-center py-4">
                            <i class="fas fa-link fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">Keine Dokumente verknüpft</h5>
                            <p class="text-muted">Verknüpfen Sie Dokumente aus Paperless mit Ihren Entitäten.</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Verfügbare Paperless-Dokumente -->
    {% if available_dokumente %}
    <div class="row mb-4">
        <div class="col-12">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-info">
                        <i class="fas fa-plus-circle me-2"></i>Verfügbare Paperless-Dokumente ({{ available_dokumente|length }})
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        {% for doc in available_dokumente %}
                        <div class="col-md-6 col-lg-4 mb-3">
                            <div class="card h-100 border-info">
                                <div class="card-body">
                                    <h6 class="card-title">{{ doc.title }}</h6>
                                    <div class="mb-2">
                                        {% for tag in doc.tags %}
                                            {% if tag == 'Stiftung_Destinatäre' or tag == 'Stiftung_Land_und_Pächter' or tag == 'Stiftung_Administration' %}
                                                <span class="badge bg-primary me-1">{{ tag }}</span>
                                            {% else %}
                                                <span class="badge bg-light text-dark me-1">{{ tag }}</span>
                                            {% endif %}
                                        {% endfor %}
                                    </div>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <a href="{{ doc.document_url }}" target="_blank" class="btn btn-sm btn-outline-info">
                                            <i class="fas fa-external-link-alt me-1"></i>In Paperless öffnen
                                        </a>
                                        <a href="{% url 'stiftung:dokument_management' %}" class="btn btn-sm btn-success">
                                            <i class="fas fa-link me-1"></i>Verknüpfen
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

{% endblock %} 
