{% extends "base.html" %}

{% block title %}Admin Dashboard{% endblock %}

{% block head %}
{{ super() }}
<style>
    .admin-grid {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 1rem;
        margin-bottom: 1.2rem;
    }

    .stat-card {
        background: #ffffff;
        border: 1px solid #d4e0e9;
        border-radius: 14px;
        padding: 1.2rem;
        box-shadow: 0 10px 28px rgba(20, 40, 55, 0.05);
        text-align: center;
    }

    .stat-value {
        font-size: 2.2rem;
        font-weight: 700;
        color: #0a5c88;
        margin-bottom: 0.3rem;
    }

    .stat-label {
        font-size: 0.9rem;
        color: #5a7a8f;
        font-weight: 600;
    }

    .appointments-section {
        background: #ffffff;
        border: 1px solid #d4e0e9;
        border-radius: 16px;
        padding: 1.4rem;
        box-shadow: 0 14px 32px rgba(20, 39, 55, 0.06);
    }

    .quick-admin-link {
        margin-bottom: 1rem;
        border: 1px solid #d8e3ec;
        border-radius: 12px;
        background: #f6fbff;
        padding: 0.85rem 0.95rem;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.8rem;
    }

    .quick-admin-link a {
        border-radius: 999px;
        padding: 0.42rem 0.8rem;
        border: 1px solid #b8ccda;
        background: #ffffff;
        color: #0b4d73;
        font-weight: 700;
        white-space: nowrap;
    }

    .appointments-section h2 {
        font-size: 1.3rem;
        color: #0f354d;
        margin-bottom: 0.8rem;
    }

    .block-days-section {
        margin-bottom: 1.2rem;
        padding: 1rem;
        border: 1px solid #d8e3ec;
        border-radius: 12px;
        background: #f9fbfd;
    }

    .block-days-section h3 {
        margin: 0 0 0.6rem;
        color: #143a55;
        font-size: 1.05rem;
    }

    .block-form {
        display: grid;
        grid-template-columns: 180px 1fr auto;
        gap: 0.5rem;
        margin-bottom: 0.8rem;
    }

    .block-form input {
        width: 100%;
        border: 1px solid #c4d4e0;
        border-radius: 8px;
        padding: 0.45rem 0.55rem;
        font: inherit;
    }

    .block-btn,
    .unblock-btn {
        border: none;
        border-radius: 8px;
        padding: 0.45rem 0.65rem;
        font-weight: 700;
        cursor: pointer;
    }

    .block-btn {
        color: #ffffff;
        background: linear-gradient(120deg, #0b5b89 0%, #0b4262 100%);
    }

    .blocked-list {
        display: grid;
        gap: 0.45rem;
    }

    .blocked-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.6rem;
        border: 1px solid #d5dee6;
        border-radius: 9px;
        padding: 0.5rem 0.65rem;
        background: #ffffff;
    }

    .blocked-item p {
        margin: 0;
        font-size: 0.88rem;
    }

    .unblock-btn {
        color: #7e2d2d;
        background: #fff3f3;
        border: 1px solid #e8b8b8;
    }

    .appointment-card {
        border: 1px solid #d8e3ec;
        border-radius: 12px;
        padding: 1rem;
        margin-bottom: 0.8rem;
        background: #f9fbfd;
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 1rem;
    }

    .appointment-info > strong {
        display: block;
        color: #113d59;
        margin-bottom: 0.25rem;
    }

    .appointment-info p {
        margin: 0.15rem 0;
        font-size: 0.9rem;
    }

    .appointment-status {
        display: inline-block;
        border-radius: 999px;
        padding: 0.25rem 0.6rem;
        font-size: 0.8rem;
        font-weight: 700;
        margin-top: 0.35rem;
    }

    .status-pending {
        border: 1px solid #fba316;
        background: #fffbf0;
        color: #b8730f;
    }

    .status-confirmed {
        border: 1px solid #0e9f6e;
        background: #f0fdf4;
        color: #0b5c47;
    }

    .status-rejected {
        border: 1px solid #d3413e;
        background: #fdf5f5;
        color: #8b2a27;
    }

    .appointment-actions {
        display: flex;
        gap: 0.4rem;
    }

    .action-form {
        display: grid;
        gap: 0.6rem;
    }

    .response-input {
        width: 100%;
        min-height: 60px;
        border: 1px solid #c4d4e0;
        border-radius: 8px;
        padding: 0.5rem;
        font-family: inherit;
        font-size: 0.85rem;
        resize: vertical;
    }

    .action-btn {
        border: none;
        border-radius: 6px;
        padding: 0.4rem 0.7rem;
        font-size: 0.85rem;
        font-weight: 700;
        cursor: pointer;
        color: #ffffff;
    }

    .confirm-btn {
        background: linear-gradient(120deg, #0a7c4e 0%, #065f3f 100%);
    }

    .reject-btn {
        background: linear-gradient(120deg, #a74444 0%, #8a3535 100%);
    }

    @media (max-width: 980px) {
        .admin-grid {
            grid-template-columns: 1fr 1fr;
        }

        .block-form {
            grid-template-columns: 1fr;
        }
    }

    @media (max-width: 600px) {
        .admin-grid {
            grid-template-columns: 1fr;
        }

        .appointment-card {
            flex-direction: column;
        }
    }
</style>
{% endblock %}

{% block content %}
<h1 style="font-size: 2rem; color: #0f354d; margin-bottom: 1rem;">Admin Dashboard</h1>

<section class="admin-grid">
    <article class="stat-card">
        <div class="stat-value">{{ status_counts.Angefragt }}</div>
        <div class="stat-label">Neue Anfragen</div>
    </article>
    <article class="stat-card">
        <div class="stat-value">{{ status_counts.Bestaetigt }}</div>
        <div class="stat-label">Bestätigt</div>
    </article>
    <article class="stat-card">
        <div class="stat-value">{{ total_posts }}</div>
        <div class="stat-label">Blog-Beiträge</div>
    </article>
</section>

<section class="appointments-section">
    <div class="quick-admin-link">
        <p><strong>Team verwalten:</strong> Bilder, Rolle und Arbeit der zwei Team-Cards zentral pflegen.</p>
        <a href="{{ url_for('admin_team') }}">Team Cards öffnen</a>
    </div>
    <div class="quick-admin-link">
        <p><strong>Schul-Instanzen:</strong> Neue Subdomain-Instanzen für Schulen aus dem Inventarsystem-Repository starten.</p>
        <a href="{{ url_for('admin_instances') }}">Instanzen öffnen</a>
    </div>
    <h2>Buchungsanfragen verwalten</h2>
    <div class="block-days-section">
        <h3>Kalender-Tage sperren</h3>
        <form method="POST" action="{{ url_for('admin_block_day') }}" class="block-form">
            <input type="hidden" name="action" value="add">
            <input type="date" name="block_date" required>
            <input type="text" name="reason" placeholder="Grund (optional)">
            <button type="submit" class="block-btn">Tag sperren</button>
        </form>
        <div class="blocked-list">
            {% if blocked_days %}
                {% for blocked in blocked_days %}
                    <div class="blocked-item">
                        <p>
                            <strong>{{ blocked.date }}</strong>
                            {% if blocked.reason %}
                                - {{ blocked.reason }}
                            {% endif %}
                        </p>
                        <form method="POST" action="{{ url_for('admin_block_day') }}">
                            <input type="hidden" name="action" value="remove">
                            <input type="hidden" name="block_date" value="{{ blocked.date }}">
                            <button type="submit" class="unblock-btn">Entsperren</button>
                        </form>
                    </div>
                {% endfor %}
            {% else %}
                <p>Aktuell sind keine Tage gesperrt.</p>
            {% endif %}
        </div>
    </div>
    {% if appointments %}
        {% for appointment in appointments %}
            <article class="appointment-card">
                <div class="appointment-info">
                    <strong>{{ appointment.display_name }} - {{ appointment.subject }}</strong>
                    <p><strong>Datum:</strong> {{ appointment.date }} um {{ appointment.time }}</p>
                    <p><strong>Buchungsart:</strong> {{ appointment.meeting_label or ('Vor Ort' if appointment.meeting_type == 'vor_ort' else 'Digital') }}</p>
                    {% if appointment.location_name %}
                        <p><strong>Ort:</strong> {{ appointment.location_name }}</p>
                    {% endif %}
                    {% if appointment.location_maps_url %}
                        <p><strong>Maps:</strong> <a href="{{ appointment.location_maps_url }}" target="_blank" rel="noopener noreferrer">Link öffnen</a></p>
                    {% endif %}
                    <p><strong>Angefragt:</strong> {{ appointment.created_at[:10] }}</p>
                    {% if appointment.note %}
                        <p><strong>Notiz:</strong> {{ appointment.note }}</p>
                    {% endif %}
                    {% if appointment.response %}
                        <p><strong>Antwort:</strong> {{ appointment.response }}</p>
                    {% endif %}
                    <span class="appointment-status status-{{ appointment.status|lower|replace('Ä', 'ae')|replace('ö', 'oe')|replace('ü', 'ue') if appointment.status == 'Angefragt' else 'confirmed' if appointment.status == 'Bestaetigt' else 'rejected' }}">{{ appointment.status }}</span>
                </div>
                {% if appointment.status == 'Angefragt' %}
                    <div class="appointment-actions">
                        <form method="POST" action="{{ url_for('update_appointment', appointment_id=appointment.id) }}" class="action-form" style="min-width: 280px;">
                            <textarea class="response-input" name="response" placeholder="Antwort (optional)"></textarea>
                            <div style="display: flex; gap: 0.4rem;">
                                <button type="submit" name="action" value="confirm" class="confirm-btn action-btn">Bestätigen</button>
                                <button type="submit" name="action" value="reject" class="reject-btn action-btn">Ablehnen</button>
                            </div>
                        </form>
                    </div>
                {% endif %}
            </article>
        {% endfor %}
    {% else %}
        <p>Keine Buchungsanfragen vorhanden.</p>
    {% endif %}
</section>

<section style="margin-top: 1.5rem;">
    <a href="{{ url_for('admin_blog') }}" style="color: #ffffff; background: linear-gradient(120deg, #0b5b89 0%, #0b4262 100%); padding: 0.68rem 1.15rem; border-radius: 999px; font-weight: 700; display: inline-block; text-decoration: none;">Blog verwalten</a>
</section>
{% endblock %}
