{% extends "base.html" %}
{% block title %}Aktivitäten{{ " - " + app_name() }}{% endblock %}

{% block content %}
<div class="page-header">
    <h1>Aktivitäten <span style="font-size: 0.9rem; color: var(--kw-text-muted); font-weight: normal;">({{ gesamt }})</span></h1>
    <a href="/aktivitaeten/neu" role="button" class="btn-sm">+ Neue Aktivität</a>
</div>

<!-- Status Tabs -->
{% set params = [] %}
{% if art_filter %}{% set _ = params.append('art=' + art_filter) %}{% endif %}
{% if von_filter %}{% set _ = params.append('von=' + von_filter) %}{% endif %}
{% if bis_filter %}{% set _ = params.append('bis=' + bis_filter) %}{% endif %}
{% if zugewiesen_an_filter %}{% set _ = params.append('zugewiesen_an=' + zugewiesen_an_filter) %}{% endif %}
{% if q %}{% set _ = params.append('q=' + q) %}{% endif %}
{% if kontakt_q_filter %}{% set _ = params.append('kontakt_q=' + kontakt_q_filter) %}{% endif %}
{% set base_params = '&' + params|join('&') if params else '' %}
<div style="display:flex; gap:0.4rem; margin-bottom:0.5rem; flex-wrap:wrap;">
    <a href="/aktivitaeten?{{ params|join('&') }}" role="button" class="btn-sm {% if not status_filter %}contrast{% else %}outline{% endif %}">Alle</a>
    {% for opt in kat_optionen("aktivitaet_status") %}
    {% if opt.wert != 'information' %}
    <a href="/aktivitaeten?status={{ opt.wert }}{{ base_params }}" role="button" class="btn-sm {% if status_filter == opt.wert %}contrast{% else %}outline{% endif %}">{{ opt.label }}</a>
    {% endif %}
    {% endfor %}
</div>

<!-- Filter + Tabelle kombiniert -->
<form method="get" action="/aktivitaeten">
    <button type="submit" style="display:none"></button>
    <table class="data-table">
        <thead>
            <tr>
                <th>Datum</th>
                <th>Art</th>
                <th>Betreff</th>
                <th>Kontakt</th>
                <th>Status</th>
                <th>Von</th>
                <th style="min-width:130px;"></th>
            </tr>
            <tr class="tbl-filter-row">
                <td>
                    <input type="date" name="von" value="{{ von_filter or '' }}" title="Von"
                           style="display:block; margin-bottom:2px !important;">
                    <input type="date" name="bis" value="{{ bis_filter or '' }}" title="Bis"
                           style="display:block;">
                </td>
                <td>
                    <select name="art" onchange="this.form.submit()">
                        <option value="">Alle</option>
                        {% for opt in kat_optionen("aktivitaet_art") %}
                        <option value="{{ opt.wert }}" {% if art_filter == opt.wert %}selected{% endif %}>{{ opt.label }}</option>
                        {% endfor %}
                    </select>
                </td>
                <td>
                    <input type="text" name="q" value="{{ q or '' }}" placeholder="Suchen…">
                </td>
                <td>
                    <input type="text" name="kontakt_q" value="{{ kontakt_q_filter or '' }}" placeholder="Name…">
                </td>
                <td>
                    <select name="status" onchange="this.form.submit()">
                        <option value="">Alle</option>
                        {% for opt in kat_optionen("aktivitaet_status") %}
                        <option value="{{ opt.wert }}" {% if status_filter == opt.wert %}selected{% endif %}>{{ opt.label }}</option>
                        {% endfor %}
                    </select>
                </td>
                <td>
                    <select name="zugewiesen_an" onchange="this.form.submit()">
                        <option value="">Alle Benutzer</option>
                        <option value="{{ user.kuerzel }}" {% if zugewiesen_an_filter == user.kuerzel %}selected{% endif %}>Meine ({{ user.kuerzel }})</option>
                        {% for b in benutzer %}
                        {% if b.kuerzel != user.kuerzel %}
                        <option value="{{ b.kuerzel }}" {% if zugewiesen_an_filter == b.kuerzel %}selected{% endif %}>{{ b.kuerzel }}</option>
                        {% endif %}
                        {% endfor %}
                    </select>
                </td>
                <td>
                    {% if alle_kampagnen %}
                    <select name="kampagne_id" onchange="this.form.submit()" style="display:block; margin-bottom:2px !important;">
                        <option value="">Alle Kampagnen</option>
                        {% for k in alle_kampagnen %}
                        <option value="{{ k.id }}" {% if kampagne_id_filter == k.id %}selected{% endif %}>{{ k.titel }}</option>
                        {% endfor %}
                    </select>
                    {% endif %}
                    {% if art_filter or von_filter or bis_filter or zugewiesen_an_filter or kampagne_id_filter or q or status_filter or kontakt_q_filter %}
                    <a href="/aktivitaeten" class="btn-sm outline" style="margin:0; font-size:0.7rem; padding:0.15rem 0.4rem; display:inline-block;" title="Filter zurücksetzen">✕</a>
                    {% endif %}
                </td>
            </tr>
        </thead>
        <tbody>
            {% for akt in aktivitaeten %}
            <tr style="cursor:pointer;" onclick="window.location='/aktivitaeten/{{ akt.id }}'">
                <td style="white-space:nowrap;">{{ akt.datum.strftime('%d.%m.%Y') if akt.datum else '-' }}</td>
                <td style="white-space:nowrap;">{{ akt.art_icon }} {{ akt.art_label }}</td>
                <td>{{ akt.betreff or '-' }}</td>
                <td style="font-size:0.85rem;">{{ akt.kontakt.anzeigename if akt.kontakt else '-' }}</td>
                <td>
                    {% if akt.status == 'offen' %}
                    <span class="tag-chip" style="background:#dd6b20; color:white;">offen</span>
                    {% else %}
                    <span style="font-size:0.8rem; color:var(--kw-text-muted);">erledigt</span>
                    {% endif %}
                </td>
                <td style="font-size:0.85rem;">{{ akt.erfasst_von or '-' }}</td>
                <td><a href="/aktivitaeten/{{ akt.id }}/bearbeiten" onclick="event.stopPropagation()" title="Bearbeiten" style="font-size:0.8rem;">✏️</a></td>
            </tr>
            {% else %}
            <tr><td colspan="7" style="text-align:center; padding:2rem; color:var(--kw-text-muted);">Keine Aktivitäten gefunden.</td></tr>
            {% endfor %}
        </tbody>
    </table>
</form>

{% set extra = '' %}
{% if art_filter %}{% set extra = extra + '&art=' + art_filter %}{% endif %}
{% if status_filter %}{% set extra = extra + '&status=' + status_filter %}{% endif %}
{% if von_filter %}{% set extra = extra + '&von=' + von_filter %}{% endif %}
{% if bis_filter %}{% set extra = extra + '&bis=' + bis_filter %}{% endif %}
{% if zugewiesen_an_filter %}{% set extra = extra + '&zugewiesen_an=' + zugewiesen_an_filter %}{% endif %}
{% if q %}{% set extra = extra + '&q=' + q %}{% endif %}
{% if kampagne_id_filter %}{% set extra = extra + '&kampagne_id=' + kampagne_id_filter|string %}{% endif %}
{% if kontakt_q_filter %}{% set extra = extra + '&kontakt_q=' + kontakt_q_filter %}{% endif %}
{% with base_url='/aktivitaeten', extra_params=extra %}
{% include "components/pagination.html" %}
{% endwith %}
{% endblock %}
