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

{% block title %}{{ dok.titel }} bearbeiten – DMS – Stiftungsverwaltung{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-lg-7">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1 class="h3">
                <i class="fas fa-edit text-primary me-2"></i>
                Metadaten bearbeiten
            </h1>
            <a href="{% url 'stiftung:dms_detail' pk=dok.pk %}" class="btn btn-outline-secondary">
                <i class="fas fa-arrow-left me-2"></i>Zurück
            </a>
        </div>

        <div class="card shadow">
            <div class="card-body">
                <form method="post">
                    {% csrf_token %}

                    <div class="mb-3">
                        <label class="form-label fw-semibold">Titel</label>
                        <input type="text" name="titel" class="form-control"
                               value="{{ dok.titel }}" required maxlength="255">
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold">Typ / Kontext</label>
                        <select name="kontext" class="form-select">
                            {% for code, label in kontext_choices %}
                            <option value="{{ code }}" {% if code == dok.kontext %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-semibold">Beschreibung</label>
                        <textarea name="beschreibung" class="form-control" rows="3">{{ dok.beschreibung }}</textarea>
                    </div>

                    <!-- Zuordnung -->
                    <hr>
                    <h6 class="fw-semibold mb-3"><i class="fas fa-link me-2"></i>Zuordnung</h6>
                    <div class="row g-3 mb-4">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Destinatär</label>
                            <select name="destinataer_id" class="form-select form-select-sm">
                                <option value="">– keine –</option>
                                {% for d in destinataere %}
                                <option value="{{ d.pk }}" {% if dok.destinataer_id == d.pk %}selected{% endif %}>
                                    {{ d.get_full_name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Länderei</label>
                            <select name="land_id" class="form-select form-select-sm">
                                <option value="">– keine –</option>
                                {% for l in laendereien %}
                                <option value="{{ l.pk }}" {% if dok.land_id == l.pk %}selected{% endif %}>
                                    {{ l.lfd_nr }}{% if l.gemeinde %} – {{ l.gemeinde }}{% endif %}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Pächter</label>
                            <select name="paechter_id" class="form-select form-select-sm">
                                <option value="">– keine –</option>
                                {% for p in paechter_qs %}
                                <option value="{{ p.pk }}" {% if dok.paechter_id == p.pk %}selected{% endif %}>
                                    {{ p.get_full_name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Verpachtung</label>
                            <select name="verpachtung_id" class="form-select form-select-sm">
                                <option value="">– keine –</option>
                                {% for v in verpachtungen %}
                                <option value="{{ v.pk }}" {% if dok.verpachtung_id == v.pk %}selected{% endif %}>
                                    {{ v.land.lfd_nr }} – {{ v.paechter.get_full_name }} ({{ v.vertragsbeginn|date:"Y" }})
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="d-flex gap-2 justify-content-end">
                        <a href="{% url 'stiftung:dms_detail' pk=dok.pk %}" class="btn btn-outline-secondary">Abbrechen</a>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save me-2"></i>Speichern
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Datei-Info (read-only) -->
        <div class="card shadow mt-4">
            <div class="card-header bg-light py-2">
                <span class="small text-muted"><i class="fas fa-file me-2"></i>Datei (nicht änderbar)</span>
            </div>
            <div class="card-body py-2">
                <span class="font-monospace small text-muted">{{ dok.dateiname_original|default:dok.datei.name }}</span>
                <span class="text-muted small ms-3">{{ dok.get_human_size }}</span>
            </div>
        </div>
    </div>
</div>
{% endblock %}
