<!--
   Copyright 2025-2026 AIIrondev

   Licensed under the Inventarsystem EULA (Endbenutzer-Lizenzvertrag).
   See Legal/LICENSE for the full license text.
   Unauthorized commercial use, SaaS hosting, or removal of branding is prohibited.
   For commercial licensing inquiries: https://github.com/AIIrondev
-->
{% extends "base.html" %}

{% block title %}Schulstammdaten verwalten{% endblock %}

{% block content %}
<div class="container">
    <div class="page-header">
        <h1>Schulstammdaten verwalten</h1>
        <p>Diese Angaben werden für amtliche Exporte, PDF-Berichte und die Zuordnung in Behörden verwendet.</p>
    </div>

    <div class="notice-card">
        <strong>Hinweis:</strong> Die hier gespeicherten Daten werden tenantweise in der MongoDB gespeichert und sofort für die PDF-Exporte verwendet.
    </div>

    <div class="notice-card" style="background:#f8fafc; border-color:#dbe3ee; color:#334155;">
        <strong>Aktueller Kontext:</strong>
        Tenant {{ tenant_id or 'default' }} · Datenbank {{ tenant_db or 'Inventarsystem' }}
    </div>

    <div class="settings-grid">
        <div class="card settings-card">
            <div class="card-header">
                <h2>Schuldaten</h2>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ url_for('admin_school_settings') }}" enctype="multipart/form-data">
                    <div class="form-row">
                        <div class="form-group">
                            <label for="name">Schulname *</label>
                            <input type="text" id="name" name="name" value="{{ school_info.name or '' }}" required placeholder="Grundschule Beispielstadt">
                        </div>
                        <div class="form-group">
                            <label for="school_number">Schulnummer *</label>
                            <input type="text" id="school_number" name="school_number" value="{{ school_info.school_number or '' }}" required placeholder="042123">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="address">Adresse *</label>
                        <input type="text" id="address" name="address" value="{{ school_info.address or '' }}" required placeholder="Musterstraße 12">
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="postal_code">PLZ *</label>
                            <input type="text" id="postal_code" name="postal_code" value="{{ school_info.postal_code or '' }}" required placeholder="12345">
                        </div>
                        <div class="form-group">
                            <label for="city">Ort *</label>
                            <input type="text" id="city" name="city" value="{{ school_info.city or '' }}" required placeholder="Musterstadt">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="it_admin">Verantwortliche Person</label>
                        <input type="text" id="it_admin" name="it_admin" value="{{ school_info.it_admin or '' }}" placeholder="IT-Beauftragte/r, Sekretariat oder Schulleitung">
                    </div>

                    <div class="form-group">
                        <label for="logo_upload">Schullogo hochladen</label>
                        <input type="file" id="logo_upload" name="logo_upload" accept=".png,.jpg,.jpeg,.gif,.webp,.svg,image/*">
                        <small>Upload ersetzt das bisherige Logo. Wenn kein neues Logo gewählt wird, bleibt das aktuelle erhalten.</small>
                    </div>

                    <div class="actions-row">
                        <button type="submit" class="btn btn-primary">Schulstammdaten speichern</button>
                        <a href="{{ url_for('home_admin') }}" class="btn btn-secondary">Zurück zur Admin-Übersicht</a>
                    </div>
                </form>
            </div>
        </div>

        <div class="card preview-card">
            <div class="card-header">
                <h2>Aktuelle Vorschau</h2>
            </div>
            <div class="card-body">
                {% if school_info.logo_path %}
                <div style="margin-bottom:16px; padding:12px; border:1px solid #e5e7eb; border-radius:10px; background:#fff;">
                    <div style="font-weight:700; margin-bottom:8px; color:#374151;">Aktuelles Logo</div>
                    <img src="{{ url_for('uploaded_file', filename=school_info.logo_path) }}" alt="Aktuelles Schullogo" style="max-width:100%; max-height:180px; object-fit:contain; display:block;">
                    <div style="margin-top:8px; font-size:0.85rem; color:#6b7280; word-break:break-word;">{{ school_info.logo_path }}</div>
                </div>
                {% endif %}
                <p><strong>Schulname:</strong> {{ school_info.name or 'Nicht gesetzt' }}</p>
                <p><strong>Adresse:</strong> {{ school_info.address or 'Nicht gesetzt' }}</p>
                <p><strong>Schulnummer:</strong> {{ school_info.school_number or 'Nicht gesetzt' }}</p>
                <p><strong>Ort:</strong> {{ school_info.postal_code or '' }} {{ school_info.city or '' }}</p>
                <p><strong>Verantwortliche Person:</strong> {{ school_info.it_admin or 'Nicht gesetzt' }}</p>
                <p><strong>Logo:</strong> {{ school_info.logo_path or 'Nicht gesetzt' }}</p>
                <hr>
                <p>Diese Angaben erscheinen künftig im amtlichen Audit-PDF und in anderen Behördenberichten.</p>
            </div>
        </div>
    </div>
</div>

<style>
    .container {
        max-width: 1100px;
        margin: 0 auto;
        padding: 20px;
    }

    .page-header {
        text-align: center;
        margin-bottom: 18px;
    }

    .page-header h1 {
        margin-bottom: 8px;
        color: #1f2937;
    }

    .page-header p {
        color: #4b5563;
        margin: 0;
    }

    .notice-card {
        background: #eff6ff;
        border: 1px solid #bfdbfe;
        color: #1d4ed8;
        border-radius: 10px;
        padding: 14px 16px;
        margin-bottom: 18px;
    }

    .settings-grid {
        display: grid;
        grid-template-columns: 1.4fr 0.9fr;
        gap: 18px;
        align-items: start;
    }

    .card {
        background: var(--ui-surface);
        border: 1px solid #e5e7eb;
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 6px 20px rgba(15, 23, 42, 0.06);
    }

    .card-header {
        background: var(--ui-surface-soft);
        padding: 16px 18px;
        border-bottom: 1px solid #e5e7eb;
    }

    .card-header h2 {
        margin: 0;
        font-size: 1.1rem;
        color: #111827;
    }

    .card-body {
        padding: 18px;
    }

    .form-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 14px;
    }

    .form-group {
        margin-bottom: 14px;
    }

    .form-group label {
        display: block;
        font-weight: 700;
        margin-bottom: 6px;
        color: #374151;
    }

    .form-group input {
        width: 100%;
        padding: 11px 12px;
        border: 1px solid #cbd5e1;
        border-radius: 8px;
        background: #fff;
        color: #111827;
        font-size: 1rem;
    }

    .form-group input:focus {
        outline: none;
        border-color: #2563eb;
        box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
    }

    .form-group small {
        display: block;
        margin-top: 6px;
        color: #6b7280;
        line-height: 1.35;
    }

    .actions-row {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        margin-top: 18px;
    }

    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
        padding: 10px 16px;
        border-radius: 8px;
        font-weight: 700;
        text-decoration: none;
        border: 1px solid transparent;
        cursor: pointer;
    }

    .btn-primary {
        background: #2563eb;
        color: #fff;
    }

    .btn-primary:hover {
        background: #1d4ed8;
        color: #fff;
    }

    .btn-secondary {
        background: #f3f4f6;
        color: #111827;
        border-color: #d1d5db;
    }

    .btn-secondary:hover {
        background: #e5e7eb;
        color: #111827;
    }

    .preview-card p {
        margin: 0 0 10px;
        line-height: 1.45;
    }

    .preview-card hr {
        border: 0;
        border-top: 1px solid #e5e7eb;
        margin: 16px 0;
    }

    @media (max-width: 900px) {
        .settings-grid,
        .form-row {
            grid-template-columns: 1fr;
        }
    }
</style>
{% endblock %}
