{% 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">
        <a class="button is-light" href="{{ url_for('admin.dashboard') }}">← Zurück</a>
    </div>
</div>

<div class="box">
    <h2 class="title is-5">Mitarbeiter</h2>
    <table class="table is-fullwidth is-hoverable">
        <thead>
            <tr><th>Name</th><th>E-Mail</th><th>Rolle</th><th>Status</th><th>Rechte</th><th>Aktion</th></tr>
        </thead>
        <tbody>
            {% for u in users %}
            <tr>
                <td>{{ u.full_name }}</td>
                <td>{{ u.email }}</td>
                <td>
                    <span class="tag {{ '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>
                    {% if u.rolle == 'mitarbeiter' %}
                    <a class="button is-small is-link is-outlined" href="{{ url_for('admin.mitarbeiter_rechte', user_id=u.id) }}">Rechte</a>
                    {% else %}
                    <span class="has-text-grey is-size-7">–</span>
                    {% endif %}
                </td>
                <td>
                    {% if current_user.is_firmadmin() and u.id != current_user.id %}
                    <a class="button is-small is-info is-outlined" href="{{ url_for('admin.mitarbeiter_bearbeiten', user_id=u.id) }}">Bearbeiten</a>
                    <a class="button is-small {{ 'is-warning' if u.aktiv else 'is-success' }}"
                       href="{{ url_for('admin.mitarbeiter_toggle', user_id=u.id) }}">
                        {{ 'Deaktivieren' if u.aktiv else 'Aktivieren' }}
                    </a>
                    <form method="POST" action="{{ url_for('admin.mitarbeiter_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>

    {% if current_user.is_firmadmin() %}
    <details class="mt-3">
        <summary class="has-text-link">+ Mitarbeiter hinzufügen</summary>
        <form method="POST" action="{{ url_for('admin.mitarbeiter_neu') }}" class="mt-2 box">
            <div class="columns is-multiline">
                <div class="column is-4">
                    <input class="input" type="email" name="email" placeholder="E-Mail *" required>
                </div>
                <div class="column is-3">
                    <input class="input" type="text" name="vorname" placeholder="Vorname">
                </div>
                <div class="column is-3">
                    <input class="input" type="text" name="nachname" placeholder="Nachname">
                </div>
                <div class="column is-2">
                    <div class="select 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" type="password" name="password" placeholder="Start-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"> Projekte anlegen</label>
                    <label class="checkbox is-size-7 mr-3"><input type="checkbox" name="darf_lv_verwalten" value="1"> 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"> 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 Addon nutzen</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-2">
                    <button class="button is-primary" type="submit">Anlegen</button>
                </div>
            </div>
        </form>
    </details>
    {% endif %}
</div>

<div class="box">
    <h2 class="title is-5">Firmenlogo</h2>
    {% if company.logo %}
    <div style="margin-bottom:12px">
        <img src="{{ url_for('admin.firma_logo') }}" alt="Logo" style="max-width:200px;max-height:80px;border:1px solid #ddd;border-radius:6px;padding:8px">
        <p class="is-size-7 has-text-grey mt-1">Aktuelles Logo</p>
        <form method="POST" action="{{ url_for('admin.firma_logo_upload') }}" style="display:inline">
            <input type="hidden" name="delete" value="1">
            <button class="button is-small is-danger is-outlined" type="submit">Entfernen</button>
        </form>
    </div>
    {% else %}
    <div style="margin-bottom:12px;display:flex;align-items:center;gap:12px;border:2px dashed #ccc;border-radius:8px;padding:16px;background:#fafafa">
        <div style="width:60px;height:60px;background:#e0e0e0;border-radius:8px;display:flex;align-items:center;justify-content:center;font-size:24px;color:#999">🏢</div>
        <div>
            <p style="font-weight:600;font-size:1rem;color:#555">{{ company.name }}</p>
            <p class="is-size-7 has-text-grey">Kein Logo hochgeladen</p>
        </div>
    </div>
    {% endif %}
    <form method="POST" action="{{ url_for('admin.firma_logo_upload') }}" enctype="multipart/form-data">
        <div class="field has-addons">
            <div class="control">
                <input class="input is-small" type="file" name="logo" accept="image/png,image/jpeg,image/gif,image/webp" required>
            </div>
            <div class="control">
                <button class="button is-small is-primary" type="submit">Hochladen</button>
            </div>
        </div>
        <p class="is-size-7 has-text-grey">Erlaubte Formate: PNG, JPG, GIF, WebP</p>
    </form>
</div>

<div class="box">
    <h2 class="title is-5">Verfügbare Module ({{ modules|length }})</h2>
    <p class="is-size-7 has-text-grey mb-2">Diese Module stehen Ihrer Firma zur Verfügung.</p>
    <table class="table is-fullwidth is-hoverable">
        <thead><tr><th>Modul</th><th>Kategorie</th></tr></thead>
        <tbody>
            {% for m in modules %}
            <tr>
                <td>{{ m.icon }} {{ m.titel }}</td>
                <td>{{ m.kategorie or '–' }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

<div class="box">
    <h2 class="title is-5">Lizenz</h2>
    {% if licenses %}
    <table class="table is-fullwidth">
        <thead><tr><th>UID</th><th>Mitarbeiter-Plätze</th><th>Modul-Plätze</th><th>Status</th></tr></thead>
        <tbody>
            {% for lic in licenses %}
            <tr>
                <td><code>{{ lic.uid }}</code></td>
                <td>{{ lic.user_slots_display() }}</td>
                <td>{{ lic.module_slots_display() }}</td>
                <td><span class="tag {{ 'is-success' if lic.aktiv else 'is-danger' }}">{{ 'Aktiv' if lic.aktiv else 'Inaktiv' }}</span></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p class="has-text-grey">Keine Lizenz hinterlegt. Standard-Module sind aktiv.</p>
    {% endif %}
</div>

{% if company.evergabe_aktiviert %}
<div class="box">
    <h2 class="title is-5">E-Vergabe</h2>
    <p class="is-size-7 has-text-grey mb-2">Logindaten für die E-Vergabe-Plattform.</p>
    <form method="POST" action="{{ url_for('admin.firma_evergabe_save') }}">
        <div class="columns is-multiline">
            <div class="column is-3">
                <div class="field">
                    <label class="label is-small">Benutzer</label>
                    <div class="control"><input class="input is-small" name="evergabe_benutzer" value="{{ company.evergabe_benutzer or '' }}"></div>
                </div>
            </div>
            <div class="column is-3">
                <div class="field">
                    <label class="label is-small">Passwort</label>
                    <div class="control"><input class="input is-small" name="evergabe_passwort" type="password" value="{{ company.evergabe_passwort or '' }}"></div>
                </div>
            </div>
            <div class="column is-3">
                <div class="field">
                    <label class="label is-small">Name</label>
                    <div class="control"><input class="input is-small" name="evergabe_name" value="{{ company.evergabe_name or '' }}"></div>
                </div>
            </div>
        </div>
        <div class="field mt-3">
            <button class="button is-small is-primary" type="submit">Speichern</button>
        </div>
    </form>
</div>
{% endif %}
{% endblock %}