{% extends "base.html" %}
{% block content %}
<div class="level">
    <div class="level-left"><h1 class="title is-3">{{ company.name }}</h1></div>
    <div class="level-right" style="display:flex;gap:4px">
        <a class="button is-small is-link" href="{{ url_for('superadmin.firma_edit', company_id=company.id) }}">⚙ Bearbeiten</a>
        <a class="button is-small" href="{{ url_for('superadmin.dashboard') }}">← Alle Firmen</a>
    </div>
</div>

<div class="box">
    <h2 class="title is-5">Firmendaten</h2>
    <div class="columns is-multiline is-size-7">
        <div class="column is-3"><strong>Name:</strong> {{ company.name }}</div>
        <div class="column is-5"><strong>Str./HsNr:</strong> {{ company.strasse or '–' }} {{ company.house_number or '–' }}</div>
        <div class="column is-4"><strong>PLZ/Ort:</strong> {{ company.plz or '–' }} {{ company.ort or '–' }}</div>
        <div class="column is-3"><strong>Telefon:</strong> {{ company.telefon or '–' }}</div>
        <div class="column is-3"><strong>E-Mail:</strong> {{ company.email or '–' }}</div>
        <div class="column is-3"><strong>Status:</strong> <span class="tag {{ 'is-success' if company.aktiv else 'is-danger' }}">{{ 'aktiv' if company.aktiv else 'deaktiviert' }}</span></div>
    </div>
</div>

<div class="box">
    <h2 class="title is-5">Benutzer</h2>
    <details class="mb-3">
        <summary class="has-text-link" style="cursor:pointer">+ Benutzer hinzufügen</summary>
        <form method="POST" action="{{ url_for('superadmin.firma_user_create', company_id=company.id) }}" class="mt-2 box" style="padding:1rem">
            <div class="columns is-multiline is-variable is-2">
                <div class="column is-4"><input class="input is-small" type="email" name="email" placeholder="E-Mail *" required></div>
                <div class="column is-3"><input class="input is-small" type="text" name="vorname" placeholder="Vorname"></div>
                <div class="column is-3"><input class="input is-small" type="text" name="nachname" placeholder="Nachname"></div>
                <div class="column is-2">
                    <div class="select is-small is-fullwidth">
                        <select name="rolle">
                            <option value="mitarbeiter">Mitarbeiter</option>
                            <option value="firmadmin">Firmadmin</option>
                        </select>
                    </div>
                </div>
                <div class="column is-4"><input class="input is-small" type="password" name="password" placeholder="Passwort *" minlength="6" required></div>
                <div class="column is-8">
                    <label class="checkbox is-size-7 mr-3"><input type="checkbox" name="darf_projekte_anlegen" value="1" checked> Projekte anlegen</label>
                    <label class="checkbox is-size-7 mr-3"><input type="checkbox" name="darf_lv_verwalten" value="1" checked> LV verwalten</label>
                    <label class="checkbox is-size-7 mr-3"><input type="checkbox" name="darf_preise_sehen" value="1"> Preise sehen</label>
                    <label class="checkbox is-size-7"><input type="checkbox" name="darf_aufmass_verwalten" value="1" checked> Aufmaße verwalten</label>
                    {% if company.evergabe_aktiviert %}
                    <br>
                    <label class="checkbox is-size-7 mr-3"><input type="checkbox" name="darf_evergabe_nutzen" value="1"> E-Vergabe</label>
                    <label class="checkbox is-size-7 mr-3"><input type="checkbox" name="darf_kopfdaten_holen" value="1"> Kopfdaten holen</label>
                    <label class="checkbox is-size-7"><input type="checkbox" name="darf_aufmass_uebertragen" value="1"> Aufmaße übertragen</label>
                    {% endif %}
                </div>
                <div class="column is-12"><button class="button is-small is-primary" type="submit">Anlegen</button></div>
            </div>
        </form>
    </details>
    <table class="table is-fullwidth is-hoverable">
        <thead><tr><th>E-Mail</th><th>Name</th><th>Rolle</th><th>Aktiv</th><th>Aktion</th></tr></thead>
        <tbody>
            {% for u in users %}
            <tr>
                <td>{{ u.email }}</td>
                <td>{{ u.full_name }}</td>
                <td><span class="tag {{ 'is-info' if u.is_superadmin() else ('is-warning' if u.is_firmadmin() else 'is-light') }}">{{ u.rolle }}</span></td>
                <td><span class="tag {{ 'is-success' if u.aktiv else 'is-danger' }}">{{ 'aktiv' if u.aktiv else 'deaktiviert' }}</span></td>
                <td>
                    <a class="button is-small {{ 'is-warning' if u.aktiv else 'is-success' }}" href="{{ url_for('superadmin.user_toggle', user_id=u.id) }}">{{ 'Deaktivieren' if u.aktiv else 'Aktivieren' }}</a>
                    <a class="button is-small is-link" href="{{ url_for('admin.mitarbeiter_bearbeiten', user_id=u.id) }}">Bearbeiten</a>
                    <a class="button is-small is-link" href="{{ url_for('admin.mitarbeiter_rechte', user_id=u.id) }}">Rechte</a>
                    {% if not u.is_superadmin() %}
                    <a class="button is-small is-link" href="{{ url_for('superadmin.user_make_superadmin', user_id=u.id) }}">Zum Superadmin</a>
                    {% else %}
                    <a class="button is-small is-light" href="{{ url_for('superadmin.user_make_superadmin', user_id=u.id) }}">Superadmin entziehen</a>
                    {% endif %}
                    {% if u.id != current_user.id %}
                    <form method="POST" action="{{ url_for('superadmin.user_loeschen', user_id=u.id) }}" style="display:inline" onsubmit="return confirm('Benutzer {{ u.email }} wirklich löschen?')">
                        <button class="button is-small is-danger">Löschen</button>
                    </form>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

<div class="box">
    <h2 class="title is-5">Lizenzen</h2>
    {% if licenses %}
    <table class="table is-fullwidth is-hoverable">
        <thead><tr><th>UID</th><th>Max. Benutzer</th><th>Max. Module</th><th>Unlimited Benutzer</th><th>Unlimited Module</th><th>Aktiv</th><th>Aktion</th></tr></thead>
        <tbody>
            {% for lic in licenses %}
            <tr>
                <td><code style="font-size:0.7rem">{{ lic.uid[:16] }}…</code></td>
                <td>{{ lic.user_slots_display() }}</td>
                <td>{{ lic.module_slots_display() }}</td>
                <td><span class="tag {{ 'is-success' if lic.unlimited_users else 'is-light' }}">{{ 'Ja' if lic.unlimited_users else 'Nein' }}</span></td>
                <td><span class="tag {{ 'is-success' if lic.unlimited_modules else 'is-light' }}">{{ 'Ja' if lic.unlimited_modules else 'Nein' }}</span></td>
                <td><span class="tag {{ 'is-success' if lic.aktiv else 'is-danger' }}">{{ 'aktiv' if lic.aktiv else 'inaktiv' }}</span></td>
                <td>
                    <a class="button is-small is-link" onclick="toggleEditLic(this, {{ lic.id }})">Bearbeiten</a>
                    <form method="POST" action="{{ url_for('superadmin.license_delete', license_id=lic.id) }}" style="display:inline" onsubmit="return confirm('Lizenz wirklich löschen?')">
                        <button class="button is-small is-danger">Löschen</button>
                    </form>
                </td>
            </tr>
            <tr id="edit-lic-{{ lic.id }}" style="display:none">
                <td colspan="7" style="padding:0">
                    <form method="POST" action="{{ url_for('superadmin.license_edit', license_id=lic.id) }}" class="box" style="margin:0.5rem;padding:0.75rem">
                        <div class="columns is-multiline is-variable is-2">
                            <div class="column is-3"><label class="is-size-7">Max. Benutzer</label><input class="input is-small" type="number" name="max_mitarbeiter" value="{{ lic.max_mitarbeiter }}" min="1"></div>
                            <div class="column is-3"><label class="is-size-7">Max. Module</label><input class="input is-small" type="number" name="max_module_slots" value="{{ lic.max_module_slots }}" min="1"></div>
                            <div class="column is-3"><label class="checkbox is-size-7"><input type="checkbox" name="unlimited_users" value="1" {{ 'checked' if lic.unlimited_users }}> Unbegrenzte Benutzer</label></div>
                            <div class="column is-3"><label class="checkbox is-size-7"><input type="checkbox" name="unlimited_modules" value="1" {{ 'checked' if lic.unlimited_modules }}> Unbegrenzte Module</label></div>
                            <div class="column is-12"><label class="is-size-7">Zugewiesene Module</label>
                                <div style="display:flex;flex-wrap:wrap;gap:6px;margin-top:4px">
                                {% for m in all_modules %}
                                    {% set lm_active = lic.modules.filter_by(module_id=m.id, aktiv=True).first() %}
                                    <label class="checkbox is-size-7"><input type="checkbox" name="modules" value="{{ m.id }}" {{ 'checked' if lm_active }}> {{ m.icon }} {{ m.titel }}</label>
                                {% endfor %}
                                </div>
                            </div>
                            <div class="column is-12"><button class="button is-small is-primary">Speichern</button> <a class="button is-small" onclick="toggleEditLic(this, {{ lic.id }})">Abbrechen</a></div>
                        </div>
                    </form>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p class="is-size-7 has-text-grey mb-2">Keine Lizenzen vorhanden.</p>
    {% endif %}
    <details>
        <summary class="has-text-link" style="cursor:pointer">+ Neue Lizenz erstellen</summary>
        <form method="POST" action="{{ url_for('superadmin.license_create', company_id=company.id) }}" class="mt-2 box" style="padding:0.75rem">
            <div class="columns is-multiline is-variable is-2">
                <div class="column is-3"><label class="is-size-7">Max. Benutzer</label><input class="input is-small" type="number" name="max_mitarbeiter" value="5" min="1"></div>
                <div class="column is-3"><label class="is-size-7">Max. Module</label><input class="input is-small" type="number" name="max_module_slots" value="5" min="1"></div>
                <div class="column is-3"><label class="checkbox is-size-7"><input type="checkbox" name="unlimited_users" value="1"> Unbegrenzte Benutzer</label></div>
                <div class="column is-3"><label class="checkbox is-size-7"><input type="checkbox" name="unlimited_modules" value="1"> Unbegrenzte Module</label></div>
                <div class="column is-12"><label class="is-size-7">Module auswählen</label>
                    <div style="display:flex;flex-wrap:wrap;gap:6px;margin-top:4px">
                    {% for m in all_modules %}
                    <label class="checkbox is-size-7"><input type="checkbox" name="modules" value="{{ m.id }}"> {{ m.icon }} {{ m.titel }}</label>
                    {% endfor %}
                    </div>
                </div>
                <div class="column is-12"><button class="button is-small is-primary">Lizenz erstellen</button></div>
            </div>
        </form>
    </details>
</div>

<div class="box">
    <h2 class="title is-5">Firmen-Modul-Zuweisung</h2>
    <p class="is-size-7 has-text-grey mb-2">Module direkt der Firma zuweisen (ohne Lizenz). Aktivierte Module stehen dem Firmadmin + allen Mitarbeitern zur Verfügung.</p>
    <table class="table is-fullwidth is-hoverable">
        <thead><tr><th>Modul</th><th>Kategorie</th><th>Standard</th><th>Status</th><th>Aktion</th></tr></thead>
        <tbody>
            {% for m in all_modules %}
            <tr>
                <td>{{ m.icon }} {{ m.titel }}</td>
                <td>{{ m.kategorie or '–' }}</td>
                <td><span class="tag {{ 'is-success' if m.standard else 'is-light' }}">{{ 'Standard' if m.standard else '–' }}</span></td>
                <td>
                    {% if m.id in company_modules %}
                    <span class="tag is-success">Aktiv</span>
                    {% else %}
                    <span class="tag is-light">Inaktiv</span>
                    {% endif %}
                </td>
                <td>
                    <a class="button is-small {{ 'is-warning' if m.id in company_modules else 'is-success' }}"
                       href="{{ url_for('superadmin.company_module_toggle', company_id=company.id, module_id=m.id) }}">
                        {{ 'Deaktivieren' if m.id in company_modules else 'Aktivieren' }}
                    </a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

<div class="box">
    <h2 class="title is-5">E-Vergabe Addon</h2>
    <div class="level">
        <div class="level-left">
            <p class="is-size-6">Status: <span class="tag {{ 'is-info' if company.evergabe_aktiviert else 'is-light' }}">{{ 'Freigeschaltet' if company.evergabe_aktiviert else 'Nicht aktiv' }}</span></p>
        </div>
        <div class="level-right">
            <a class="button is-small {{ 'is-warning' if company.evergabe_aktiviert else 'is-info' }}" href="{{ url_for('superadmin.firma_evergabe_toggle', company_id=company.id) }}">
                {{ 'Deaktivieren' if company.evergabe_aktiviert else 'Freischalten' }}
            </a>
        </div>
    </div>
    {% if company.evergabe_aktiviert %}
    <p class="is-size-7 has-text-grey mt-2">Benutzer: {{ company.evergabe_benutzer or '–' }} | Name: {{ company.evergabe_name or '–' }}</p>
    {% endif %}
</div>

<div class="box">
    <h2 class="title is-5">Projekte</h2>
    <table class="table is-fullwidth is-hoverable">
        <thead><tr><th>Projekte</th><th>Status</th></tr></thead>
        <tbody>
            {% for p in projekte %}
            <tr>
                <td>{{ p.bezeichnung or '–' }}</td>
                <td><span class="tag {{ 'is-success' if p.status == 'aktiv' else 'is-light' }}">{{ p.status }}</span></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

<script>
function toggleEditLic(el, licId){
    var row=document.getElementById('edit-lic-'+licId);
    if(!row)return;
    row.style.display=row.style.display==='none'?'':'none';
}
</script>
{% endblock %}