{% extends "base.html" %}
{% block title %}E-Mails - MailsAuswerten{% endblock %}
{% block content %}
<h1>E-Mails ({{ emails|length }})</h1>

<!-- Konto-Filter -->
{% if accounts %}
<div style="margin-bottom: 0.5rem;">
    <small style="color: #888; font-weight: 600;">Konto:</small>
    <a href="/emails{% if filter_domain %}?domain={{ filter_domain }}{% endif %}"
       class="btn btn-sm {{ 'btn-primary' if not filter_account else 'btn-success' }}">Alle</a>
    {% for acc in accounts %}
    <a href="/emails?account={{ acc.name }}{% if filter_domain %}&domain={{ filter_domain }}{% endif %}"
       class="btn btn-sm {{ 'btn-primary' if filter_account == acc.name else 'btn-success' }}">{{ acc.name }}</a>
    {% endfor %}
</div>
{% endif %}

<!-- Domain-Filter -->
{% if domains %}
<div style="margin-bottom: 1rem;">
    <small style="color: #888; font-weight: 600;">Domain:</small>
    <a href="/emails{% if filter_account %}?account={{ filter_account }}{% endif %}"
       class="btn btn-sm {{ 'btn-primary' if not filter_domain else 'btn-success' }}">Alle</a>
    {% for d in domains[:20] %}
    <a href="/emails?domain={{ d.domain }}{% if filter_account %}&account={{ filter_account }}{% endif %}"
       class="btn btn-sm {{ 'btn-primary' if filter_domain == d.domain else 'btn-success' }}"
       title="{{ d.total_emails }} E-Mails von {{ d.sender_count }} Absendern">
        {{ d.domain }} ({{ d.total_emails }})
    </a>
    {% endfor %}
    {% if domains|length > 20 %}
    <a href="/senders" class="link" style="margin-left: 0.5rem;">alle {{ domains|length }} Domains &rarr;</a>
    {% endif %}
</div>
{% endif %}

<div class="card">
    {% if emails %}
    <table>
        <thead>
            <tr>
                <th>Konto</th>
                <th>Datum</th>
                <th>Absender</th>
                <th>Domain</th>
                <th>Betreff</th>
                <th>Anhaenge</th>
            </tr>
        </thead>
        <tbody>
            {% for email in emails %}
            <tr>
                <td>{{ email.account_name or '-' }}</td>
                <td>{{ email.date[:16] if email.date else '-' }}</td>
                <td>{{ email.sender }}</td>
                <td>
                    {% if email.sender_domain %}
                    <a href="/emails?domain={{ email.sender_domain }}" class="link" style="font-size: 0.85rem;">{{ email.sender_domain }}</a>
                    {% else %}-{% endif %}
                </td>
                <td><a href="/emails/{{ email.id }}" class="link">{{ email.subject or '(kein Betreff)' }}</a></td>
                <td>{{ email.attachment_count }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p style="color: #888; padding: 2rem; text-align: center;">Keine E-Mails gefunden.</p>
    {% endif %}
</div>
{% endblock %}
