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

{% block title %}Förderungen - Stiftungsverwaltung{% endblock %}

{% block content %}

    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3">
            <i class="fas fa-gift text-primary me-2"></i>Förderungen
        </h1>
        <div>
            <button type="button" class="btn btn-success me-2" data-bs-toggle="modal" data-bs-target="#exportModal">
                <i class="fas fa-download me-2"></i> CSV/PDF Export
            </button>
            <a href="{% url 'stiftung:foerderung_create' %}" class="btn btn-primary">
                <i class="fas fa-plus me-2"></i> Neue Förderung
            </a>
        </div>
    </div>

<!-- Export Modal -->
<div class="modal fade" id="exportModal" tabindex="-1">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Export Optionen</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form id="exportForm" method="post" action="{% url 'stiftung:foerderung_list' %}">
        {% csrf_token %}
        <div class="modal-body">
          <div class="row">
            <div class="col-md-6">
              <h6 class="fw-bold">Export Format</h6>
              <div class="mb-3">
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="format" id="format_csv" value="csv" checked>
                  <label class="form-check-label" for="format_csv">
                    <i class="fas fa-file-csv me-2"></i>CSV (Excel-kompatibel)
                  </label>
                </div>
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="format" id="format_pdf" value="pdf">
                  <label class="form-check-label" for="format_pdf">
                    <i class="fas fa-file-pdf me-2"></i>PDF (Tabelle)
                  </label>
                </div>
              </div>
              
              <h6 class="fw-bold">Export Umfang</h6>
              <div class="mb-3">
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="scope" id="scope_all" value="all" checked>
                  <label class="form-check-label" for="scope_all">
                    Alle Einträge (<span id="total-count">{{ foerderungen.count }}</span>)
                  </label>
                </div>
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="scope" id="scope_selected" value="selected">
                  <label class="form-check-label" for="scope_selected">
                    Nur ausgewählte Einträge (<span id="selected-count">0</span>)
                  </label>
                </div>
              </div>
            </div>
            
            <div class="col-md-6" id="field-selection" style="max-height: 400px; overflow-y: auto;">
              <h6 class="fw-bold">Felder auswählen</h6>
              <div class="mb-2">
                <button type="button" class="btn btn-sm btn-outline-primary me-2" onclick="selectAllFields()">Alle auswählen</button>
                <button type="button" class="btn btn-sm btn-outline-secondary" onclick="selectDefaultFields()">Standard</button>
              </div>
              
              <div class="field-groups">
                <!-- Kernfelder -->
                <div class="mb-3">
                  <h6 class="text-muted mb-2">Kernfelder</h6>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="destinataer_name" id="field_destinataer_name" checked>
                    <label class="form-check-label" for="field_destinataer_name">Destinatär Name</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="jahr" id="field_jahr" checked>
                    <label class="form-check-label" for="field_jahr">Jahr</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="betrag" id="field_betrag" checked>
                    <label class="form-check-label" for="field_betrag">Betrag (€)</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="kategorie" id="field_kategorie" checked>
                    <label class="form-check-label" for="field_kategorie">Kategorie</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="status" id="field_status" checked>
                    <label class="form-check-label" for="field_status">Status</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="antragsdatum" id="field_antragsdatum" checked>
                    <label class="form-check-label" for="field_antragsdatum">Antragsdatum</label>
                  </div>
                </div>
                
                <!-- Zeitfelder -->
                <div class="mb-3">
                  <h6 class="text-muted mb-2">Datumsfelder</h6>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="bewilligungsdatum" id="field_bewilligungsdatum">
                    <label class="form-check-label" for="field_bewilligungsdatum">Bewilligungsdatum</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="auszahlungsdatum" id="field_auszahlungsdatum">
                    <label class="form-check-label" for="field_auszahlungsdatum">Auszahlungsdatum</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="erstellt_am" id="field_erstellt_am">
                    <label class="form-check-label" for="field_erstellt_am">Erstellt am</label>
                  </div>
                </div>
                
                <!-- Beschreibungsfelder -->
                <div class="mb-3">
                  <h6 class="text-muted mb-2">Details</h6>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="beschreibung" id="field_beschreibung" checked>
                    <label class="form-check-label" for="field_beschreibung">Beschreibung</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="begruendung" id="field_begruendung">
                    <label class="form-check-label" for="field_begruendung">Begründung</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="verwendungsnachweis_status" id="field_verwendungsnachweis_status">
                    <label class="form-check-label" for="field_verwendungsnachweis_status">Verwendungsnachweis Status</label>
                  </div>
                </div>
                
                <!-- Destinataer Details -->
                <div class="mb-3">
                  <h6 class="text-muted mb-2">Destinatär Details</h6>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="familienzweig" id="field_familienzweig">
                    <label class="form-check-label" for="field_familienzweig">Familienzweig</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="email" id="field_email">
                    <label class="form-check-label" for="field_email">E-Mail</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="telefon" id="field_telefon">
                    <label class="form-check-label" for="field_telefon">Telefon</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="adresse" id="field_adresse">
                    <label class="form-check-label" for="field_adresse">Adresse</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="fields" value="berufsgruppe" id="field_berufsgruppe">
                    <label class="form-check-label" for="field_berufsgruppe">Berufsgruppe</label>
                  </div>
                </div>
              </div>
            </div>
          </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">
            <i class="fas fa-download me-2"></i>Export starten
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <h5 class="card-title">Gesamtbetrag</h5>
                    <h3 class="card-text">€{{ total_betrag|floatformat:2 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white">
                <div class="card-body">
                    <h5 class="card-title">Durchschnitt</h5>
                    <h3 class="card-text">€{{ avg_betrag|floatformat:2 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <h5 class="card-title">Anzahl</h5>
                    <h3 class="card-text">{{ page_obj.paginator.count }}</h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="card mb-4">
        <div class="card-header">
            <h6 class="mb-0">
                <i class="fas fa-filter me-2"></i>Filter
            </h6>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-2">
                    <label for="jahr" class="form-label">Jahr</label>
                    <select name="jahr" id="jahr" class="form-select">
                        <option value="">Alle Jahre</option>
                        {% for year in jahre %}
                        <option value="{{ year }}" {% if filter_jahr == year %}selected{% endif %}>{{ year }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="kategorie" class="form-label">Kategorie</label>
                    <select name="kategorie" id="kategorie" class="form-select">
                        <option value="">Alle Kategorien</option>
                        {% for kategorie in kategorien %}
                        <option value="{{ kategorie.0 }}" {% if filter_kategorie == kategorie.0 %}selected{% endif %}>{{ kategorie.1 }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="status" class="form-label">Status</label>
                    <select name="status" id="status" class="form-select">
                        <option value="">Alle Status</option>
                        {% for status in status_choices %}
                        <option value="{{ status.0 }}" {% if filter_status == status.0 %}selected{% endif %}>{{ status.1 }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="person" class="form-label">Person</label>
                    <input type="text" name="person" id="person" class="form-control" value="{{ filter_person }}" placeholder="Nachname suchen">
                </div>
                <div class="col-md-3 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary me-2">
                        <i class="fas fa-search me-2"></i>Filtern
                    </button>
                    <a href="{% url 'stiftung:foerderung_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-times me-2"></i>Zurücksetzen
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Förderungen Table -->
    <div class="card">
        <div class="card-header">
            <h6 class="mb-0">
                <i class="fas fa-list me-2"></i>Förderungen
            </h6>
        </div>
        <div class="card-body">
            {% if page_obj %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th width="50">
                                <input type="checkbox" class="form-check-input" id="selectAll" onchange="toggleAllCheckboxes(this)">
                            </th>
                            <th>Person</th>
                            <th>Jahr</th>
                            <th>Betrag</th>
                            <th>Kategorie</th>
                            <th>Status</th>
                            <th>Antragsdatum</th>
                            <th>Aktionen</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for foerderung in page_obj %}
                        <tr>
                            <td>
                                <input type="checkbox" class="form-check-input entry-checkbox" name="entry" value="{{ foerderung.pk }}" onchange="updateSelectedCount()">
                            </td>
                            <td>
                                {% if foerderung.destinataer and foerderung.destinataer.pk %}
                                <a href="{% url 'stiftung:destinataer_detail' foerderung.destinataer.pk %}">
                                    {{ foerderung.destinataer.get_full_name }}
                                </a>
                                {% else %}
                                <em class="text-muted">Keine Person zugeordnet</em>
                                {% endif %}
                            </td>
                            <td>{{ foerderung.jahr }}</td>
                            <td>€{{ foerderung.betrag|floatformat:2 }}</td>
                            <td>
                                <span class="badge bg-secondary">{{ foerderung.get_kategorie_display }}</span>
                            </td>
                            <td>
                                <span class="badge bg-{{ foerderung.get_status_color }}">{{ foerderung.get_status_display }}</span>
                            </td>
                            <td>{{ foerderung.antragsdatum|date:"d.m.Y" }}</td>
                            <td>
                                <div class="btn-group" role="group">
                                    <a href="{% url 'stiftung:foerderung_detail' foerderung.pk %}" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <a href="{% url 'stiftung:foerderung_update' foerderung.pk %}" class="btn btn-sm btn-outline-warning">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="{% url 'stiftung:foerderung_delete' foerderung.pk %}" class="btn btn-sm btn-outline-danger">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            {% if page_obj.has_other_pages %}
            <nav aria-label="Förderungen Pagination">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if filter_jahr %}&jahr={{ filter_jahr }}{% endif %}{% if filter_kategorie %}&kategorie={{ filter_kategorie }}{% endif %}{% if filter_status %}&status={{ filter_status }}{% endif %}{% if filter_person %}&person={{ filter_person }}{% endif %}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% endif %}

                    {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <li class="page-item active">
                        <span class="page-link">{{ num }}</span>
                    </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ num }}{% if filter_jahr %}&jahr={{ filter_jahr }}{% endif %}{% if filter_kategorie %}&kategorie={{ filter_kategorie }}{% endif %}{% if filter_status %}&status={{ filter_status }}{% endif %}{% if filter_person %}&person={{ filter_person }}{% endif %}">{{ num }}</a>
                    </li>
                    {% endif %}
                    {% endfor %}

                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if filter_jahr %}&jahr={{ filter_jahr }}{% endif %}{% if filter_kategorie %}&kategorie={{ filter_kategorie }}{% endif %}{% if filter_status %}&status={{ filter_status }}{% endif %}{% if filter_person %}&person={{ filter_person }}{% endif %}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}

            {% else %}
            <div class="text-center py-5">
                <i class="fas fa-gift fa-3x text-muted mb-3"></i>
                <h5 class="text-muted">Keine Förderungen gefunden</h5>
                <p class="text-muted">Erstellen Sie Ihre erste Förderung oder passen Sie die Filter an.</p>
                <a href="{% url 'stiftung:foerderung_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>Neue Förderung erstellen
                </a>
            </div>
            {% endif %}
        </div>
    </div>

    <script>
        let selectedEntries = [];
        
        function toggleAllCheckboxes(source) {
            checkboxes = document.getElementsByName('entry');
            for(let i=0, n=checkboxes.length;i<n;i++) {
                checkboxes[i].checked = source.checked;
            }
            updateSelectedCount();
        }
        
        function updateSelectedCount() {
            selectedEntries = [];
            const checkboxes = document.getElementsByName('entry');
            for(let i=0, n=checkboxes.length;i<n;i++) {
                if(checkboxes[i].checked) {
                    selectedEntries.push(checkboxes[i].value);
                }
            }
            
            const count = selectedEntries.length;
            document.getElementById('selectedCount').textContent = count;
            document.getElementById('exportButton').disabled = count === 0;
            
            // Update text in buttons
            document.getElementById('exportText').textContent = 
                count === 0 ? 'Export' : `${count} Einträge exportieren`;
        }
        
        function selectAllFields() {
            document.querySelectorAll('#exportModal input[type="checkbox"]').forEach(cb => cb.checked = true);
        }
        
        function selectDefaultFields() {
            // First uncheck all
            document.querySelectorAll('#exportModal input[type="checkbox"]').forEach(cb => cb.checked = false);
            
            // Then check default fields
            const defaultFields = [
                'destinataer__person__vorname',
                'destinataer__person__nachname', 
                'jahr',
                'betrag',
                'kategorie',
                'status',
                'antragsdatum',
                'notizen'
            ];
            
            defaultFields.forEach(field => {
                const checkbox = document.querySelector(`#exportModal input[value="${field}"]`);
                if(checkbox) checkbox.checked = true;
            });
        }
        
        function submitExportForm() {
            // Set selected entries
            document.getElementById('selectedEntries').value = selectedEntries.join(',');
            
            // Collect selected fields
            const selectedFields = [];
            document.querySelectorAll('#exportModal input[type="checkbox"]:checked').forEach(cb => {
                selectedFields.push(cb.value);
            });
            document.getElementById('selectedFields').value = selectedFields.join(',');
            
            // Submit form
            document.getElementById('exportForm').submit();
        }
        
        // Initialize on page load
        document.addEventListener('DOMContentLoaded', function() {
            updateSelectedCount();
        });
    </script>

{% endblock %} 
