{% extends "base.html" %}
{% block title %}Absender - MailsAuswerten{% endblock %}
{% block content %}
<h1>Absender{% if filter_domain %} - {{ filter_domain }}{% endif %}</h1>

<!-- Domain-Filter -->
<div style="margin-bottom: 1rem;">
    <a href="/senders" class="btn btn-sm {{ 'btn-primary' if not filter_domain else 'btn-success' }}">Alle Domains</a>
    {% for d in domains %}
    <a href="/senders?domain={{ d.domain }}"
       class="btn btn-sm {{ 'btn-primary' if filter_domain == d.domain else 'btn-success' }}"
       title="{{ d.total_emails }} E-Mails">
        {{ d.domain }} ({{ d.sender_count }})
    </a>
    {% endfor %}
</div>

<!-- Domains-Uebersicht -->
{% if not filter_domain %}
<div class="card">
    <h2 style="margin-bottom: 1rem;">Domains ({{ domains|length }})</h2>
    <table>
        <thead>
            <tr>
                <th>Domain</th>
                <th>Absender</th>
                <th>E-Mails gesamt</th>
                <th>Aktionen</th>
            </tr>
        </thead>
        <tbody>
            {% for d in domains %}
            <tr>
                <td><strong>{{ d.domain }}</strong></td>
                <td>{{ d.sender_count }}</td>
                <td>{{ d.total_emails }}</td>
                <td>
                    <a href="/senders?domain={{ d.domain }}" class="btn btn-sm btn-primary">Absender zeigen</a>
                    <a href="/emails?domain={{ d.domain }}" class="btn btn-sm btn-success">E-Mails filtern</a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endif %}

<!-- Absender-Liste -->
<div class="card">
    <h2 style="margin-bottom: 1rem;">
        {% if filter_domain %}
            Absender von {{ filter_domain }} ({{ senders|length }})
        {% else %}
            Alle Absender ({{ senders|length }})
        {% endif %}
    </h2>
    {% if senders %}
    <table>
        <thead>
            <tr>
                <th>E-Mail-Adresse</th>
                <th>Name</th>
                <th>Domain</th>
                <th>Anzahl E-Mails</th>
                <th>Erste Mail</th>
                <th>Letzte Mail</th>
                <th>Aktionen</th>
            </tr>
        </thead>
        <tbody>
            {% for s in senders %}
            <tr>
                <td><strong>{{ s.email_address }}</strong></td>
                <td>{{ s.display_name or '-' }}</td>
                <td>
                    <a href="/senders?domain={{ s.domain }}" class="link" style="font-size: 0.85rem;">{{ s.domain }}</a>
                </td>
                <td>{{ s.email_count }}</td>
                <td>{{ s.first_seen[:16] if s.first_seen else '-' }}</td>
                <td>{{ s.last_seen[:16] if s.last_seen else '-' }}</td>
                <td>
                    <a href="/emails?domain={{ s.domain }}" class="btn btn-sm btn-success">E-Mails</a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p style="color: #888; padding: 2rem; text-align: center;">Keine Absender gefunden.</p>
    {% endif %}
</div>
{% endblock %}
