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

<div class="card">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;">
        <h2>Konfigurierte Konten ({{ accounts|length }})</h2>
        <a href="/accounts/new" class="btn btn-success">Neues Konto</a>
    </div>

    {% if accounts %}
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Server</th>
                <th>Benutzer</th>
                <th>Ordner</th>
                <th>Limit</th>
                <th>Aktionen</th>
            </tr>
        </thead>
        <tbody>
            {% for acc in accounts %}
            <tr>
                <td><strong>{{ acc.name }}</strong></td>
                <td>{{ acc.server }}:{{ acc.port }}</td>
                <td>{{ acc.username }}</td>
                <td>{{ acc.folder }}{% if acc.get('include_subfolders', false) in (true, 'true', True) %} <small style="color:#8e44ad;">(+Unterordner)</small>{% endif %}</td>
                <td>{{ acc.max_emails if acc.max_emails else 'unbegrenzt' }}</td>
                <td>
                    <div class="btn-group" style="flex-wrap: wrap;">
                        <form method="post" action="/accounts/{{ acc.name }}/test" style="display:inline;">
                            <button type="submit" class="btn btn-sm" style="background:#8e44ad;color:#fff;"
                                    onclick="this.disabled=true; this.textContent='...'; this.form.submit();">
                                Testen
                            </button>
                        </form>
                        <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();">
                                Abrufen
                            </button>
                        </form>
                        <a href="/accounts/{{ acc.name }}/edit" class="btn btn-success btn-sm">Bearbeiten</a>
                        <form method="post" action="/accounts/{{ acc.name }}/delete" style="display:inline;"
                              onsubmit="return confirm('Konto \'{{ acc.name }}\' wirklich loeschen?');">
                            <button type="submit" class="btn btn-danger btn-sm">Loeschen</button>
                        </form>
                        {% if account_stats.get(acc.name, 0) > 0 %}
                        <form method="post" action="/accounts/{{ acc.name }}/backup" style="display:inline;"
                              onsubmit="return confirm('Alle {{ account_stats[acc.name] }} E-Mails von \'{{ acc.name }}\' in Backup sichern und aus der Hauptdatenbank entfernen?');">
                            <button type="submit" class="btn btn-sm" style="background:#e67e22;color:#fff;">
                                Backup ({{ account_stats[acc.name] }})
                            </button>
                        </form>
                        {% endif %}
                    </div>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p style="color: #888; padding: 2rem; text-align: center;">Noch keine Konten konfiguriert.</p>
    {% endif %}
</div>
{% endblock %}
