{% extends "base.html" %}
{% block title %}Dashboard - MailsAuswerten{% endblock %}
{% block content %}
<h1>Dashboard</h1>

<div class="stat-grid">
    <div class="stat-card">
        <div class="number">{{ stats.emails }}</div>
        <div class="label">E-Mails gesamt</div>
    </div>
    <div class="stat-card">
        <div class="number">{{ stats.attachments }}</div>
        <div class="label">Anhaenge gesamt</div>
    </div>
    <div class="stat-card">
        <div class="number">{{ accounts|length }}</div>
        <div class="label">Konten konfiguriert</div>
    </div>
    <div class="stat-card">
        <div class="number">{{ stats.domains }}</div>
        <div class="label"><a href="/senders" class="link">Absender-Domains</a></div>
    </div>
</div>

<div class="card">
    <h2 style="margin-bottom: 1rem;">Mails abrufen</h2>
    <div style="display: flex; gap: 1rem; flex-wrap: wrap; align-items: center;">
        <form method="post" action="/fetch">
            <button type="submit" class="btn btn-primary"
                    onclick="this.disabled=true; this.textContent='Wird abgerufen...'; this.form.submit();">
                Alle Konten abrufen
            </button>
        </form>
        {% for acc in accounts %}
        <form method="post" action="/accounts/{{ acc.name }}/fetch" style="display: inline;">
            <button type="submit" class="btn btn-primary btn-sm"
                    onclick="this.disabled=true; this.textContent='...'; this.form.submit();">
                {{ acc.name }}
                {% if acc.max_emails %}(max {{ acc.max_emails }}){% endif %}
            </button>
        </form>
        {% endfor %}
    </div>
</div>

{% if results %}
<div class="card">
    <h2 style="margin-bottom: 1rem;">Ergebnis</h2>
    <table>
        <thead>
            <tr>
                <th>Konto</th>
                <th>Neue Mails</th>
                <th>Neue Anhaenge</th>
                <th>Uebersprungen</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            {% for r in results %}
            <tr>
                <td><strong>{{ r.account }}</strong></td>
                <td>{{ r.new_emails }}</td>
                <td>{{ r.new_attachments }}</td>
                <td>{{ r.skipped }}</td>
                <td>
                    {% if r.error %}
                        {% if r.error_type == 'auth' %}
                            <span style="color: #e74c3c; font-weight: 600;">Ungueltige Zugangsdaten</span>
                            <br><small style="color: #888;">Benutzername oder Passwort pruefen.
                            <a href="/accounts/{{ r.account }}/edit" class="link">Konto bearbeiten</a></small>
                        {% elif r.error_type == 'connection' %}
                            <span style="color: #e74c3c; font-weight: 600;">Server nicht erreichbar</span>
                            <br><small style="color: #888;">Server-Adresse und Port pruefen.
                            <a href="/accounts/{{ r.account }}/edit" class="link">Konto bearbeiten</a></small>
                        {% else %}
                            <span style="color: #e74c3c;">Fehler: {{ r.error }}</span>
                        {% endif %}
                    {% else %}
                        <span style="color: #27ae60;">OK</span>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endif %}
{% endblock %}
