{% extends "base.html" %}

{% block title %}Anfrage-Status - Mercedes-Benz MYP Platform{% endblock %}

{% block head %}
{{ super() }}
<meta name="csrf-token" content="{{ csrf_token() }}">
<meta http-equiv="refresh" content="30">
{% 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 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                </div>
                <div>
                    <h1 class="text-3xl font-bold text-slate-900 dark:text-white tracking-tight">Anfrage-Status</h1>
                    <p class="text-slate-500 dark:text-slate-400 mt-1">Verfolgen Sie den Status Ihrer Gastanfrage für 3D-Druck</p>
                </div>
            </div>
            <div class="flex flex-wrap gap-3">
                <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('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>Neue Anfrage</span>
                </a>
            </div>
        </div>
    </div>

    <!-- Status Badge -->
    <div class="dashboard-card p-6 text-center">
        {% if request.status == 'pending' %}
        <div class="inline-flex items-center px-6 py-3 rounded-full bg-yellow-100 dark:bg-yellow-900/30 text-yellow-800 dark:text-yellow-200 text-lg font-semibold">
            <svg class="w-6 h-6 mr-3 animate-spin" 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>
            Wird geprüft
        </div>
        {% elif request.status == 'approved' %}
        <div class="inline-flex items-center px-6 py-3 rounded-full bg-green-100 dark:bg-green-900/30 text-green-800 dark:text-green-200 text-lg font-semibold">
            <svg class="w-6 h-6 mr-3" 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>
            Genehmigt
        </div>
        {% elif request.status == 'denied' %}
        <div class="inline-flex items-center px-6 py-3 rounded-full bg-red-100 dark:bg-red-900/30 text-red-800 dark:text-red-200 text-lg font-semibold">
            <svg class="w-6 h-6 mr-3" 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>
            Abgelehnt
        </div>
        {% endif %}
    </div>

    <!-- Anfrage-Details -->
    <div class="dashboard-card p-6">
        <div class="text-center mb-8">
            <h2 class="text-2xl font-bold text-slate-900 dark:text-white mb-2">
                Anfrage Details
            </h2>
            <p class="text-slate-500 dark:text-slate-400">
                Übersicht Ihrer eingereichten Gastanfrage
            </p>
        </div>
        
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div class="bg-gray-50 dark:bg-slate-700/30 p-4 rounded-lg">
                <h3 class="text-sm font-medium text-slate-500 dark:text-slate-400 mb-2">Anfrage-ID</h3>
                <p class="text-xl font-bold text-slate-900 dark:text-white">#{{ request.id }}</p>
            </div>
            <div class="bg-gray-50 dark:bg-slate-700/30 p-4 rounded-lg">
                <h3 class="text-sm font-medium text-slate-500 dark:text-slate-400 mb-2">Erstellt am</h3>
                <p class="text-xl font-bold text-slate-900 dark:text-white">{{ request.created_at|format_datetime }}</p>
            </div>
            <div class="bg-gray-50 dark:bg-slate-700/30 p-4 rounded-lg">
                <h3 class="text-sm font-medium text-slate-500 dark:text-slate-400 mb-2">Name</h3>
                <p class="text-xl font-bold text-slate-900 dark:text-white">{{ request.name }}</p>
            </div>
            <div class="bg-gray-50 dark:bg-slate-700/30 p-4 rounded-lg">
                <h3 class="text-sm font-medium text-slate-500 dark:text-slate-400 mb-2">Gewünschte Dauer</h3>
                <p class="text-xl font-bold text-slate-900 dark:text-white">{{ request.duration_min }} Minuten</p>
            </div>
        </div>
        
        {% if request.reason %}
        <div class="mt-6 bg-gray-50 dark:bg-slate-700/30 p-4 rounded-lg">
            <h3 class="text-sm font-medium text-slate-500 dark:text-slate-400 mb-2">Begründung</h3>
            <p class="text-slate-700 dark:text-slate-300">{{ request.reason }}</p>
        </div>
        {% endif %}
        
        {% if request.printer %}
        <div class="mt-6 bg-gray-50 dark:bg-slate-700/30 p-4 rounded-lg">
            <h3 class="text-sm font-medium text-slate-500 dark:text-slate-400 mb-2">Drucker</h3>
            <p class="text-slate-700 dark:text-slate-300">{{ request.printer.name }} {% if request.printer.location %}({{ request.printer.location }}){% endif %}</p>
        </div>
        {% endif %}
    </div>

    <!-- Status-spezifische Inhalte -->
    {% if request.status == 'pending' %}
    <div class="dashboard-card p-6 border-l-4 border-yellow-400">
        <div class="flex items-start gap-4">
            <div class="w-12 h-12 bg-yellow-100 dark:bg-yellow-900/30 rounded-xl flex items-center justify-center flex-shrink-0">
                <svg class="w-6 h-6 text-yellow-600 dark:text-yellow-400" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/>
                </svg>
            </div>
            <div class="flex-1">
                <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Ihre Anfrage wird geprüft</h3>
                <p class="text-slate-600 dark:text-slate-400">
                    Unser Team prüft Ihre Anfrage mit höchster Priorität. Sie erhalten eine sofortige Benachrichtigung, sobald eine Entscheidung getroffen wurde. 
                    Diese Seite aktualisiert sich automatisch alle 30 Sekunden.
                </p>
            </div>
        </div>
    </div>
    
    {% elif request.status == 'approved' %}
    <div class="dashboard-card p-6 border-l-4 border-green-400">
        <div class="flex items-start gap-4">
            <div class="w-12 h-12 bg-green-100 dark:bg-green-900/30 rounded-xl flex items-center justify-center flex-shrink-0">
                <svg class="w-6 h-6 text-green-600 dark:text-green-400" 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 class="flex-1">
                <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Anfrage genehmigt!</h3>
                <p class="text-slate-600 dark:text-slate-400">Ihr Druckauftrag wurde genehmigt und ist bereit zum Start.</p>
            </div>
        </div>
    </div>
        
    {% if otp_code %}
    <!-- Code-Anzeige -->
    <div class="dashboard-card p-6">
        <h4 class="text-xl font-bold text-slate-900 dark:text-white mb-6 text-center">Ihr Zugangscode</h4>
        
        <!-- 6-stelliger Code in schönen Boxen -->
        <div class="flex justify-center gap-3 mb-6">
            {% for char in otp_code %}
            <div class="w-16 h-16 bg-blue-100 dark:bg-blue-900/30 text-blue-900 dark:text-blue-100 rounded-lg flex items-center justify-center text-2xl font-bold">
                {{ char }}
            </div>
            {% endfor %}
        </div>
        
        <!-- Code zum Kopieren -->
        <div class="text-center mb-6">
            <div class="inline-flex items-center bg-gray-50 dark:bg-slate-700/30 rounded-lg px-4 py-3">
                <span class="text-xl font-mono font-bold text-slate-900 dark:text-white mr-3" id="otpCode">{{ otp_code }}</span>
                <button onclick="copyCode()" class="text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300 transition-colors" title="Code kopieren">
                    <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="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
                    </svg>
                </button>
            </div>
        </div>
        
        <!-- Wichtige Hinweise -->
        <div class="bg-orange-50 dark:bg-orange-900/30 border border-orange-200 dark:border-orange-800 rounded-lg p-4 mb-6">
            <div class="flex items-start gap-3">
                <div class="w-8 h-8 bg-orange-100 dark:bg-orange-900/50 rounded-lg flex items-center justify-center flex-shrink-0">
                    <svg class="w-5 h-5 text-orange-600 dark:text-orange-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.732-.833-2.5 0L3.734 16.5c-.77.833.192 2.5 1.732 2.5z"/>
                    </svg>
                </div>
                <div class="flex-1">
                    <h5 class="text-sm font-semibold text-orange-800 dark:text-orange-300 mb-2">Wichtige Hinweise:</h5>
                    <ul class="text-sm text-orange-700 dark:text-orange-400 space-y-1">
                        <li>• Dieser Code ist nur <strong>einmalig verwendbar</strong></li>
                        <li>• Notieren Sie sich den Code oder speichern Sie diese Seite</li>
                        <li>• Bei Verlust des Codes kontaktieren Sie den Administrator</li>
                    </ul>
                </div>
            </div>
        </div>
        
        <!-- Start-Button -->
        <div class="text-center">
            <a href="{{ url_for('guest.guest_start_job_form') }}" 
               class="btn-primary flex items-center gap-2 mx-auto">
                <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="M14.828 14.828a4 4 0 01-5.656 0M9 10h1m4 0h1m-6 4h.01M12 5v.01M12 19v.01M12 12h.01M12 9a3 3 0 100-6 3 3 0 000 6zm0 0a3 3 0 100 6 3 3 0 000-6z"/>
                </svg>
                Job jetzt starten
            </a>
        </div>
    </div>
    {% elif show_start_link %}
    <!-- Code bereits vorhanden, aber noch nicht verwendet -->
    <div class="dashboard-card p-6 text-center">
        <div class="w-16 h-16 bg-blue-100 dark:bg-blue-900/30 rounded-xl flex items-center justify-center mx-auto mb-4">
            <svg class="w-8 h-8 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-3a1 1 0 011-1h2.586l6.243-6.243A6 6 0 0121 9z"/>
            </svg>
        </div>
        <h4 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Zugangscode bereits generiert</h4>
        <p class="text-slate-500 dark:text-slate-400 mb-6">Ihr persönlicher Code wurde bereits erstellt und ist bereit zur Verwendung.</p>
        
        <a href="{{ url_for('guest.guest_start_job_form') }}" 
           class="btn-primary flex items-center gap-2 mx-auto">
            <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="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-3a1 1 0 011-1h2.586l6.243-6.243A6 6 0 0121 9z"/>
            </svg>
            Code eingeben und starten
        </a>
    </div>
    {% endif %}
    
    {% elif request.status == 'denied' %}
    <div class="dashboard-card p-6 border-l-4 border-red-400">
        <div class="flex items-start gap-4">
            <div class="w-12 h-12 bg-red-100 dark:bg-red-900/30 rounded-xl flex items-center justify-center flex-shrink-0">
                <svg class="w-6 h-6 text-red-600 dark:text-red-400" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/>
                </svg>
            </div>
            <div class="flex-1">
                <h3 class="text-lg font-semibold text-slate-900 dark:text-white mb-2">Anfrage abgelehnt</h3>
                <p class="text-slate-600 dark:text-slate-400">
                    Ihre Anfrage wurde leider abgelehnt. Bei Fragen wenden Sie sich bitte an unser Team.
                </p>
            </div>
        </div>
    </div>
    {% endif %}
</div>

<script>
// Code-Kopier-Funktion
function copyCode() {
    const codeElement = document.getElementById('otpCode');
    if (codeElement) {
        const code = codeElement.textContent;
        
        // Versuche den Code in die Zwischenablage zu kopieren
        if (navigator.clipboard) {
            navigator.clipboard.writeText(code).then(function() {
                showCopySuccess();
            }).catch(function(err) {
                console.error('Fehler beim Kopieren:', err);
                fallbackCopyText(code);
            });
        } else {
            fallbackCopyText(code);
        }
    }
}

// Fallback für ältere Browser
function fallbackCopyText(text) {
    const textArea = document.createElement('textarea');
    textArea.value = text;
    textArea.style.position = 'fixed';
    textArea.style.left = '-999999px';
    textArea.style.top = '-999999px';
    document.body.appendChild(textArea);
    textArea.focus();
    textArea.select();
    
    try {
        document.execCommand('copy');
        showCopySuccess();
    } catch (err) {
        console.error('Fallback-Kopieren fehlgeschlagen:', err);
        alert('Code konnte nicht kopiert werden. Bitte manuell markieren und kopieren.');
    }
    
    document.body.removeChild(textArea);
}

// Erfolgs-Animation für das Kopieren
function showCopySuccess() {
    const button = event.target.closest('button');
    const originalHTML = button.innerHTML;
    
    button.innerHTML = `
        <svg class="w-5 h-5 text-green-600" 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>
    `;
    
    // Nach 2 Sekunden zurück zum Original
    setTimeout(() => {
        button.innerHTML = originalHTML;
    }, 2000);
    
    // Optional: Toast-Benachrichtigung
    showToast('Code wurde in die Zwischenablage kopiert!');
}

// Toast-Benachrichtigung
function showToast(message) {
    // Toast-Element erstellen, falls nicht vorhanden
    let toast = document.getElementById('copyToast');
    if (!toast) {
        toast = document.createElement('div');
        toast.id = 'copyToast';
        toast.className = 'fixed top-4 right-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg transform transition-all duration-300 translate-x-full opacity-0 z-50';
        document.body.appendChild(toast);
    }
    
    toast.textContent = message;
    
    // Animation einblenden
    setTimeout(() => {
        toast.classList.remove('translate-x-full', 'opacity-0');
    }, 100);
    
    // Nach 3 Sekunden ausblenden
    setTimeout(() => {
        toast.classList.add('translate-x-full', 'opacity-0');
    }, 3000);
}
</script>
{% endblock %} 