{% extends "base.html" %}
{% block content %}
<div class="level">
    <div class="level-left"><h1 class="title is-3">Superadmin – Dashboard</h1></div>
    <div class="level-right">
        <a class="button is-primary" href="{{ url_for('superadmin.firma_create') }}">+ Neue Firma</a>
    </div>
</div>

<div class="columns is-multiline mt-3">
    <div class="column is-one-third">
        <div class="box has-text-centered">
            <p class="heading">Firmen</p>
            <p class="title">{{ firmsen|length }}</p>
        </div>
    </div>
    <div class="column is-one-third">
        <div class="box has-text-centered">
            <p class="heading">Benutzer (gesamt)</p>
            <p class="title">{{ gesamt_user }}</p>
        </div>
    </div>
    <div class="column is-one-third">
        <div class="box has-text-centered">
            <p class="heading">Projekte (gesamt)</p>
            <p class="title">{{ gesamt_projekte }}</p>
        </div>
    </div>
</div>

<div class="columns is-multiline">
    <div class="column is-one-quarter">
        <div class="box has-text-centered">
            <p class="heading">Lizenzen (gesamt)</p>
            <p class="title">{{ gesamt_lizenzen }}</p>
        </div>
    </div>
    <div class="column is-one-quarter">
        <div class="box has-text-centered">
            <p class="heading">Mitarbeiter-Plätze</p>
            <p class="title">{{ gesamt_user }} / {{ gesamt_max_mitarbeiter }}</p>
            <p class="is-size-7 has-text-grey">belegt / verfügbar</p>
        </div>
    </div>
    <div class="column is-one-quarter">
        <div class="box has-text-centered">
            <p class="heading">Modul-Plätze (belegt)</p>
            <p class="title">{{ belegte_module_slots }} / {{ gesamt_max_module_slots }}</p>
            <p class="is-size-7 has-text-grey">belegt / verfügbar</p>
        </div>
    </div>
    <div class="column is-one-quarter">
        <div class="box has-text-centered">
            <p class="heading">Module (verfügbar)</p>
            <p class="title">{{ gesamt_module_anzahl }}</p>
        </div>
    </div>
</div>

<div class="box mt-4" style="background:#fff8e6;border-color:#ffc107">
    <div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px">
        <div>
            <h3 class="title is-5 mb-1">Neue Registrierungen</h3>
            <p class="has-text-grey is-size-7">Aktiviert/deaktiviert die Registrierung neuer Firmen über die Startseite.</p>
        </div>
        <div style="display:flex;align-items:center;gap:8px">
            <input type="checkbox" id="reg-checkbox" {{ 'checked' if registration_enabled else '' }}
                   hx-post="{{ url_for('superadmin.registration_toggle') }}"
                   hx-swap="none"
                   onchange="var l=document.getElementById('reg-label');l.textContent=this.checked?'Aktiviert':'Deaktiviert';l.className='has-text-weight-semibold '+(this.checked?'has-text-success':'has-text-danger')">
            <label for="reg-checkbox" id="reg-label" class="has-text-weight-semibold {{ 'has-text-success' if registration_enabled else 'has-text-danger' }}">
                {{ 'Aktiviert' if registration_enabled else 'Deaktiviert' }}
            </label>
        </div>
    </div>
</div>

<div class="box mt-4">
    <h2 class="title is-5">Alle Firmen</h2>
    <table class="table is-fullwidth is-hoverable">
        <thead>
            <tr><th>Name</th><th>Benutzer</th><th>Lizenz-Plätze</th><th>Modul-Plätze</th><th>E-Vergabe</th><th>Aktiv</th><th>Aktion</th></tr>
        </thead>
        <tbody>
            {% for f in firmsen %}
            <tr>
                <td><a href="{{ url_for('superadmin.firma_detail', company_id=f.id) }}">{{ f.name }}</a></td>
                <td>{{ f._user_count }}</td>
                <td>{{ f._license_slots }}</td>
                <td>{{ f._module_slots }}</td>
                <td><span class="tag {{ 'is-info' if f.evergabe_aktiviert else 'is-light' }}">{{ 'aktiv' if f.evergabe_aktiviert else '–' }}</span></td>
                <td><span class="tag {{ 'is-success' if f.aktiv else 'is-danger' }}">{{ 'aktiv' if f.aktiv else 'deaktiviert' }}</span></td>
                <td>
                    <a class="button is-small is-link" href="{{ url_for('superadmin.firma_detail', company_id=f.id) }}">Details</a>
                    <a class="button is-small {{ 'is-warning' if f.aktiv else 'is-success' }}" href="{{ url_for('superadmin.firma_toggle', company_id=f.id) }}">
                        {{ 'Deaktivieren' if f.aktiv else 'Aktivieren' }}
                    </a>
                    <a class="button is-small {{ 'is-info' if f.evergabe_aktiviert else 'is-light' }}" href="{{ url_for('superadmin.firma_evergabe_toggle', company_id=f.id) }}">
                        {{ 'EV ✕' if f.evergabe_aktiviert else 'EV +' }}
                    </a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %}
