{% extends "base.html" %}

{% block title %}Druckanträge Übersicht - Mercedes-Benz MYP Platform{% endblock %}

{% block head %}
{{ super() }}
{% endblock %}

{% block content %}
<div class="space-y-8">
    <!-- Page Header -->
    <div class="dashboard-card p-6">
        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-6">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 flex-shrink-0">
                    <svg class="w-full h-full text-slate-900 dark:text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                    </svg>
                </div>
                <div>
                    <h1 class="text-3xl font-bold text-slate-900 dark:text-white tracking-tight">Anträge Übersicht</h1>
                    <p class="text-slate-500 dark:text-slate-400 mt-1">Transparente Übersicht aller eingereichten Druckanträge</p>
                </div>
            </div>
            <div class="flex flex-wrap gap-3">
                <a href="{{ url_for('guest.guest_request_form') }}" 
                   class="btn-primary flex items-center gap-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"/>
                    </svg>
                    <span>Neuen Antrag stellen</span>
                </a>
                <button onclick="location.reload()" 
                        class="btn-secondary flex items-center gap-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>
                    </svg>
                    <span>Aktualisieren</span>
                </button>
                <a href="{{ url_for('index') }}" 
                   class="btn-secondary flex items-center gap-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/>
                    </svg>
                    <span>Startseite</span>
                </a>
            </div>
        </div>
    </div>

    <!-- Info Banner -->
    <div class="dashboard-card p-6">
        <div class="flex items-start gap-4">
            <div class="w-12 h-12 bg-blue-100 dark:bg-blue-900/30 rounded-xl flex items-center justify-center flex-shrink-0">
                <svg class="h-6 w-6 text-blue-600 dark:text-blue-400" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"></path>
                </svg>
            </div>
            <div class="flex-1">
                <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">
                    Datenschutz & Transparenz
                </h3>
                <p class="text-slate-600 dark:text-slate-400 mb-4">
                    Diese Übersicht zeigt alle eingereichten Druckanträge in anonymisierter Form. Persönliche Daten sind durch "***" zensiert, um die Privatsphäre zu schützen und gleichzeitig Transparenz über den Bearbeitungsstand zu gewährleisten.
                </p>
                <div class="flex flex-wrap gap-2">
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400">
                        Datenkonform
                    </span>
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400">
                        Anonymisiert
                    </span>
                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-400">
                        Transparent
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
        {% set total_requests = requests|length %}
        {% set pending_requests = requests|selectattr("status", "equalto", "pending")|list|length %}
        {% set approved_requests = requests|selectattr("status", "equalto", "approved")|list|length %}
        {% set denied_requests = requests|selectattr("status", "equalto", "denied")|list|length %}
        
        <div class="dashboard-card p-6">
            <div class="flex justify-between">
                <div>
                    <h3 class="stat-label">Gesamt</h3>
                    <div class="stat-value">{{ total_requests }}</div>
                </div>
                <div class="mb-stat-icon text-slate-600 dark:text-slate-400">
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                    </svg>
                </div>
            </div>
        </div>
        <div class="dashboard-card p-6">
            <div class="flex justify-between">
                <div>
                    <h3 class="stat-label">Prüfung</h3>
                    <div class="stat-value text-yellow-600 dark:text-yellow-400">{{ pending_requests }}</div>
                </div>
                <div class="mb-stat-icon text-yellow-600 dark:text-yellow-400">
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                </div>
            </div>
        </div>
        <div class="dashboard-card p-6">
            <div class="flex justify-between">
                <div>
                    <h3 class="stat-label">Genehmigt</h3>
                    <div class="stat-value text-green-600 dark:text-green-400">{{ approved_requests }}</div>
                </div>
                <div class="mb-stat-icon text-green-600 dark:text-green-400">
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                    </svg>
                </div>
            </div>
        </div>
        <div class="dashboard-card p-6">
            <div class="flex justify-between">
                <div>
                    <h3 class="stat-label">Abgelehnt</h3>
                    <div class="stat-value text-red-600 dark:text-red-400">{{ denied_requests }}</div>
                </div>
                <div class="mb-stat-icon text-red-600 dark:text-red-400">
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                    </svg>
                </div>
            </div>
        </div>
    </div>

    <!-- Requests List -->
    {% if error %}
    <div class="dashboard-card p-8 text-center">
        <div class="text-red-500 dark:text-red-400 mb-4">
            <svg class="w-12 h-12 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
            </svg>
        </div>
        <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Fehler beim Laden</h3>
        <p class="text-slate-500 dark:text-slate-400">{{ error }}</p>
    </div>
    {% elif requests|length == 0 %}
    <div class="dashboard-card p-8 text-center">
        <div class="text-slate-400 dark:text-slate-500 mb-4">
            <svg class="w-12 h-12 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
            </svg>
        </div>
        <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Keine Druckanträge</h3>
        <p class="text-slate-500 dark:text-slate-400 mb-4">
            Derzeit sind keine Druckanträge vorhanden.
        </p>
        <a href="{{ url_for('guest.guest_request_form') }}" 
           class="btn-primary">
            Ersten Antrag stellen
        </a>
    </div>
    {% else %}
    <div class="space-y-4">
        {% for request in requests %}
        <div class="dashboard-card p-6 border-l-4 {% if request.status == 'pending' %}border-yellow-400{% elif request.status == 'approved' %}border-green-400{% elif request.status == 'denied' %}border-red-400{% endif %}">
            <div class="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-4">
                
                <!-- Left Section: Request Info -->
                <div class="flex-1">
                    <div class="flex items-center gap-4 mb-4">
                        <span class="text-lg font-bold text-slate-900 dark:text-white">
                            #{{ request.id }}
                        </span>
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium {% if request.status == 'pending' %}bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400{% elif request.status == 'approved' %}bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400{% elif request.status == 'denied' %}bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400{% endif %}">
                            {% if request.status == 'pending' %}
                                Wird geprüft
                            {% elif request.status == 'approved' %}
                                Genehmigt
                            {% elif request.status == 'denied' %}
                                Abgelehnt
                            {% endif %}
                        </span>
                        {% if request.job_status %}
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-400">
                            Druckstatus: {{ request.job_status|title }}
                        </span>
                        {% endif %}
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 text-sm">
                        <div class="bg-gray-50 dark:bg-slate-700/30 p-3 rounded-lg">
                            <div class="text-slate-500 dark:text-slate-400 font-medium mb-1">Antragsteller</div>
                            <div class="font-mono text-slate-600 dark:text-slate-300">{{ request.name }}</div>
                        </div>
                        <div class="bg-gray-50 dark:bg-slate-700/30 p-3 rounded-lg">
                            <div class="text-slate-500 dark:text-slate-400 font-medium mb-1">E-Mail</div>
                            <div class="font-mono text-slate-600 dark:text-slate-300">{{ request.email }}</div>
                        </div>
                        <div class="bg-gray-50 dark:bg-slate-700/30 p-3 rounded-lg">
                            <div class="text-slate-500 dark:text-slate-400 font-medium mb-1">Drucker</div>
                            <div class="text-slate-600 dark:text-slate-300">{{ request.printer_name if request.printer_name else 'Automatisch' }}</div>
                        </div>
                        <div class="bg-gray-50 dark:bg-slate-700/30 p-3 rounded-lg">
                            <div class="text-slate-500 dark:text-slate-400 font-medium mb-1">Dauer</div>
                            <div class="text-slate-600 dark:text-slate-300">{{ request.duration_min }} Min</div>
                        </div>
                    </div>
                    
                    {% if request.reason %}
                    <div class="mt-4 bg-gray-50 dark:bg-slate-700/30 p-3 rounded-lg">
                        <div class="text-slate-500 dark:text-slate-400 font-medium mb-2">Projektbeschreibung</div>
                        <div class="font-mono text-sm text-slate-600 dark:text-slate-300">{{ request.reason }}</div>
                    </div>
                    {% endif %}
                </div>
                
                <!-- Right Section: Timestamp and Actions -->
                <div class="lg:text-right">
                    <div class="bg-gray-50 dark:bg-slate-700/30 p-4 rounded-lg text-center lg:text-right">
                        <div class="text-sm font-medium text-slate-900 dark:text-white mb-1">
                            {{ request.created_at.strftime('%d.%m.%Y') }}
                        </div>
                        <div class="text-xs text-slate-500 dark:text-slate-400">
                            {{ request.created_at.strftime('%H:%M') }} Uhr
                        </div>
                        
                        {% if request.status == 'approved' %}
                        <div class="mt-3">
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400">
                                Startbereit
                            </span>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
    {% endif %}
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Live-Zeitanzeige im Browser-Tab
    function updateTabTitle() {
        const now = new Date();
        const timeString = now.toLocaleTimeString('de-DE');
        document.title = `Druckanträge (${timeString}) - Mercedes-Benz MYP Platform`;
    }
    
    updateTabTitle();
    setInterval(updateTabTitle, 1000);
    
    // Auto-Refresh alle 30 Sekunden
    setInterval(function() {
        if (!document.hidden) {
            window.location.reload();
        }
    }, 30000);
});
</script>
{% endblock %} 