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

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

{% block content %}

    <div class="row">
        <div class="col-12">
            <!-- Header -->
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3">
                    <i class="fas fa-map text-primary me-2"></i>
                    {{ title }}
                </h1>
                <div>
                    <a href="{% url 'stiftung:land_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-2"></i>Zurück zur Liste
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Form -->
    <div class="row">
        <div class="col-lg-8">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-edit me-2"></i>Länderei-Daten eingeben
                    </h6>
                </div>
                <div class="card-body">
                    <form method="post" novalidate>
                        {% csrf_token %}
                        
                        <!-- Error Messages -->
                        {% if form.non_field_errors %}
                        <div class="alert alert-danger">
                            <ul class="mb-0">
                                {% for error in form.non_field_errors %}
                                <li>{{ error }}</li>
                                {% endfor %}
                            </ul>
                        </div>
                        {% endif %}

                        <!-- Identifikation -->
                        <div class="row mb-4">
                            <div class="col-12">
                                <h5 class="text-primary mb-3">
                                    <i class="fas fa-fingerprint me-2"></i>Identifikation
                                </h5>
                            </div>
                            <div class="col-12">
                                <div class="mb-3">
                                    <label for="{{ form.lfd_nr.id_for_label }}" class="form-label">
                                        {{ form.lfd_nr.label }} *
                                    </label>
                                    {{ form.lfd_nr }}
                                    {% if form.lfd_nr.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.lfd_nr.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                    {% if form.lfd_nr.help_text %}
                                    <div class="form-text">{{ form.lfd_nr.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="mb-3">
                                    <label for="{{ form.ew_nummer.id_for_label }}" class="form-label">
                                        {{ form.ew_nummer.label }}
                                    </label>
                                    {{ form.ew_nummer }}
                                    {% if form.ew_nummer.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.ew_nummer.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <!-- Gerichtliche Zuständigkeit -->
                        <div class="row mb-4">
                            <div class="col-12">
                                <h5 class="text-primary mb-3">
                                    <i class="fas fa-balance-scale me-2"></i>Gerichtliche Zuständigkeit
                                </h5>
                            </div>
                            <div class="col-md-12">
                                <div class="mb-3">
                                    <label for="{{ form.amtsgericht.id_for_label }}" class="form-label">
                                        {{ form.amtsgericht.label }} *
                                    </label>
                                    {{ form.amtsgericht }}
                                    {% if form.amtsgericht.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.amtsgericht.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <!-- Verwaltungsstruktur -->
                        <div class="row mb-4">
                            <div class="col-12">
                                <h5 class="text-primary mb-3">
                                    <i class="fas fa-sitemap me-2"></i>Verwaltungsstruktur
                                </h5>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-3">
                                    <label for="{{ form.gemeinde.id_for_label }}" class="form-label">
                                        {{ form.gemeinde.label }} *
                                    </label>
                                    {{ form.gemeinde }}
                                    {% if form.gemeinde.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.gemeinde.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-3">
                                    <label for="{{ form.gemarkung.id_for_label }}" class="form-label">
                                        {{ form.gemarkung.label }} *
                                    </label>
                                    {{ form.gemarkung }}
                                    {% if form.gemarkung.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.gemarkung.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-3">
                                    <label for="{{ form.flur.id_for_label }}" class="form-label">
                                        {{ form.flur.label }} *
                                    </label>
                                    {{ form.flur }}
                                    {% if form.flur.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.flur.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-3">
                                    <label for="{{ form.flurstueck.id_for_label }}" class="form-label">
                                        {{ form.flurstueck.label }} *
                                    </label>
                                    {{ form.flurstueck }}
                                    {% if form.flurstueck.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.flurstueck.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <!-- ALKIS Kennzeichen -->
                        <div class="row mb-4">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="{{ form.alkis_kennzeichen.id_for_label }}" class="form-label">
                                        {{ form.alkis_kennzeichen.label }}
                                    </label>
                                    {{ form.alkis_kennzeichen }}
                                    <small class="form-text text-muted">{{ form.alkis_kennzeichen.help_text }}</small>
                                    {% if form.alkis_kennzeichen.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.alkis_kennzeichen.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <!-- Flächenangaben -->
                        <div class="row mb-4">
                            <div class="col-12">
                                <h5 class="text-primary mb-3">
                                    <i class="fas fa-ruler-combined me-2"></i>Flächenangaben
                                </h5>
                            </div>
                            <div class="col-12">
                                <div class="mb-3">
                                    <label for="{{ form.groesse_qm.id_for_label }}" class="form-label">
                                        {{ form.groesse_qm.label }} *
                                    </label>
                                    {{ form.groesse_qm }}
                                    {% if form.groesse_qm.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.groesse_qm.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="mb-3">
                                    <label for="{{ form.verpachtete_gesamtflaeche.id_for_label }}" class="form-label">
                                        {{ form.verpachtete_gesamtflaeche.label }} *
                                    </label>
                                    {{ form.verpachtete_gesamtflaeche }}
                                    {% if form.verpachtete_gesamtflaeche.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.verpachtete_gesamtflaeche.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <!-- Landnutzung -->
                        <div class="row mb-4">
                            <div class="col-12">
                                <h5 class="text-primary mb-3">
                                    <i class="fas fa-leaf me-2"></i>Landnutzung (Aufteilung der Gesamtfläche)
                                </h5>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-3">
                                    <label for="{{ form.gruenland_qm.id_for_label }}" class="form-label">
                                        {{ form.gruenland_qm.label }}
                                    </label>
                                    {{ form.gruenland_qm }}
                                    {% if form.gruenland_qm.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.gruenland_qm.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-3">
                                    <label for="{{ form.acker_qm.id_for_label }}" class="form-label">
                                        {{ form.acker_qm.label }}
                                    </label>
                                    {{ form.acker_qm }}
                                    {% if form.acker_qm.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.acker_qm.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-3">
                                    <label for="{{ form.wald_qm.id_for_label }}" class="form-label">
                                        {{ form.wald_qm.label }}
                                    </label>
                                    {{ form.wald_qm }}
                                    {% if form.wald_qm.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.wald_qm.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="mb-3">
                                    <label for="{{ form.sonstiges_qm.id_for_label }}" class="form-label">
                                        {{ form.sonstiges_qm.label }}
                                    </label>
                                    {{ form.sonstiges_qm }}
                                    {% if form.sonstiges_qm.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.sonstiges_qm.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <!-- Verpachtung -->
                        <div class="row mb-4">
                            <div class="col-12">
                                <h5 class="text-primary mb-3">
                                    <i class="fas fa-handshake me-2"></i>Verpachtung
                                </h5>
                            </div>
                            <div class="col-12">
                                <div class="mb-3">
                                    <label for="{{ form.flaeche_alte_liste.id_for_label }}" class="form-label">
                                        {{ form.flaeche_alte_liste.label }}
                                    </label>
                                    {{ form.flaeche_alte_liste }}
                                    {% if form.flaeche_alte_liste.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.flaeche_alte_liste.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="mb-3">
                                    <label for="{{ form.verp_flaeche_aktuell.id_for_label }}" class="form-label">
                                        {{ form.verp_flaeche_aktuell.label }} *
                                    </label>
                                    {{ form.verp_flaeche_aktuell }}
                                    {% if form.verp_flaeche_aktuell.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.verp_flaeche_aktuell.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <!-- Steuern und Abgaben -->
                        <div class="row mb-4">
                            <div class="col-12">
                                <h5 class="text-primary mb-3">
                                    <i class="fas fa-percentage me-2"></i>Steuern und Abgaben
                                </h5>
                            </div>
                            <div class="col-12">
                                <div class="mb-3">
                                    <label for="{{ form.anteil_grundsteuer.id_for_label }}" class="form-label">
                                        {{ form.anteil_grundsteuer.label }}
                                    </label>
                                    {{ form.anteil_grundsteuer }}
                                    {% if form.anteil_grundsteuer.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.anteil_grundsteuer.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-12">
                                <div class="mb-3">
                                    <label for="{{ form.anteil_lwk.id_for_label }}" class="form-label">
                                        {{ form.anteil_lwk.label }}
                                    </label>
                                    {{ form.anteil_lwk }}
                                    {% if form.anteil_lwk.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.anteil_lwk.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <!-- Status und Notizen -->
                        <div class="row mb-4">
                            <div class="col-12">
                                <h5 class="text-primary mb-3">
                                    <i class="fas fa-info-circle me-2"></i>Status und Notizen
                                </h5>
                            </div>
                            <div class="col-md-12">
                                <div class="mb-3">
                                    <label for="{{ form.notizen.id_for_label }}" class="form-label">
                                        {{ form.notizen.label }}
                                    </label>
                                    {{ form.notizen }}
                                    {% if form.notizen.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.notizen.errors %}
                                        {{ error }}
                                        {% endfor %}
                                    </div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-check">
                                    {{ form.aktiv }}
                                    <label class="form-check-label" for="{{ form.aktiv.id_for_label }}">
                                        {{ form.aktiv.label }}
                                    </label>
                                </div>
                            </div>
                        </div>

                        <!-- Submit Buttons -->
                        <div class="row">
                            <div class="col-12">
                                <hr class="my-4">
                                <div class="d-flex justify-content-between">
                                    <a href="{% url 'stiftung:land_list' %}" class="btn btn-secondary">
                                        <i class="fas fa-times me-2"></i>Abbrechen
                                    </a>
                                    <button type="submit" class="btn btn-success">
                                        <i class="fas fa-save me-2"></i>
                                        {% if land %}Aktualisieren{% else %}Erstellen{% endif %}
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
                
        <!-- Sidebar mit Hilfe -->
        <div class="col-lg-4">
            {% help_box 'laenderei_new' user %}
        </div>
    </div>

{% endblock %}

{% block extra_js %}
<script>
    // Auto-calculate total area from individual areas
    function calculateTotalArea() {
        const gruenland = parseFloat(document.getElementById('{{ form.gruenland_qm.id_for_label }}').value) || 0;
        const acker = parseFloat(document.getElementById('{{ form.acker_qm.id_for_label }}').value) || 0;
        const wald = parseFloat(document.getElementById('{{ form.wald_qm.id_for_label }}').value) || 0;
        const sonstiges = parseFloat(document.getElementById('{{ form.sonstiges_qm.id_for_label }}').value) || 0;
        
        const total = gruenland + acker + wald + sonstiges;
        
        if (total > 0) {
            document.getElementById('{{ form.groesse_qm.id_for_label }}').value = total.toFixed(2);
        }
    }

    // Add event listeners to area fields
    document.getElementById('{{ form.gruenland_qm.id_for_label }}').addEventListener('input', calculateTotalArea);
    document.getElementById('{{ form.acker_qm.id_for_label }}').addEventListener('input', calculateTotalArea);
    document.getElementById('{{ form.wald_qm.id_for_label }}').addEventListener('input', calculateTotalArea);
    document.getElementById('{{ form.sonstiges_qm.id_for_label }}').addEventListener('input', calculateTotalArea);
</script>
{% endblock %} 
