@extends('layouts.admin')

@section('title', 'Détails Template Email')

@section('content')
<style>
    @keyframes slideUp {
        from { opacity: 0; transform: translateY(30px); }
        to { opacity: 1; transform: translateY(0); }
    }
    .animate-slide-up { animation: slideUp 0.6s ease-out; }
    .gradient-purple {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    }
    .badge-variable {
        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    }
</style>

<div class="container-fluid p-4 animate-slide-up">
    <!-- Header avec gradient -->
    <div class="gradient-purple rounded-4 p-4 mb-4 shadow-lg">
        <div class="row align-items-center">
            <div class="col-md-8">
                <div class="d-flex align-items-center gap-3">
                    <div class="bg-white bg-opacity-25 rounded-3 p-3">
                        <i class="bi bi-envelope-open text-white" style="font-size: 2rem;"></i>
                    </div>
                    <div>
                        <h1 class="text-white mb-1 fw-bold" style="font-size: 2rem;">{{ $emailTemplate->name }}</h1>
                        <p class="text-white text-opacity-90 mb-0">
                            <i class="bi bi-info-circle me-2"></i>Prévisualisation et test du template email
                        </p>
                    </div>
                </div>
            </div>
            <div class="col-md-4 text-end">
                <div class="d-flex gap-2 justify-content-end">
                    <a href="{{ route('admin.email-templates.edit', $emailTemplate) }}" 
                       class="btn btn-warning btn-lg shadow-sm px-4 rounded-pill">
                        <i class="bi bi-pencil me-2"></i>Modifier
                    </a>
                    <a href="{{ route('admin.email-templates.index') }}" 
                       class="btn btn-light btn-lg shadow-sm px-4 rounded-pill">
                        <i class="bi bi-arrow-left me-2"></i>Retour
                    </a>
                </div>
            </div>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show shadow-sm border-0 rounded-4 mb-4" role="alert">
            <i class="bi bi-check-circle-fill me-2"></i>{{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show shadow-sm border-0 rounded-4 mb-4" role="alert">
            <i class="bi bi-exclamation-triangle-fill me-2"></i>{{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="row g-4">
    <div class="row g-4">
        <!-- Informations du template -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-header bg-primary bg-opacity-10 border-0 rounded-top-4 py-3">
                    <h5 class="mb-0 fw-bold text-primary">
                        <i class="bi bi-info-circle me-2"></i>Informations
                    </h5>
                </div>
                <div class="card-body p-4">
                    <div class="mb-4">
                        <label class="text-muted small d-block mb-2">
                            <i class="bi bi-tag me-1"></i>Slug
                        </label>
                        <div class="bg-light rounded-3 p-3">
                            <code class="text-dark">{{ $emailTemplate->slug }}</code>
                        </div>
                    </div>

                    <div class="mb-4">
                        <label class="text-muted small d-block mb-2">
                            <i class="bi bi-toggle-on me-1"></i>Statut
                        </label>
                        @if($emailTemplate->is_active)
                            <span class="badge bg-success px-3 py-2 rounded-pill">
                                <i class="bi bi-check-circle me-1"></i>Actif
                            </span>
                        @else
                            <span class="badge bg-secondary px-3 py-2 rounded-pill">
                                <i class="bi bi-pause-circle me-1"></i>Inactif
                            </span>
                        @endif
                    </div>

                    <div class="mb-4">
                        <label class="text-muted small d-block mb-2">
                            <i class="bi bi-chat-left-text me-1"></i>Sujet
                        </label>
                        <div class="bg-light rounded-3 p-3">
                            <p class="mb-0 text-dark">{{ $emailTemplate->subject }}</p>
                        </div>
                    </div>

                    @if($emailTemplate->variables && count($emailTemplate->variables) > 0)
                    <div class="mb-4">
                        <label class="text-muted small d-block mb-2">
                            <i class="bi bi-braces me-1"></i>Variables disponibles
                        </label>
                        <div class="d-flex flex-wrap gap-2">
                            @foreach($emailTemplate->variables as $var)
                                <span class="badge badge-variable text-white px-3 py-2">
                                    @{{ {{ $var }} }}
                                </span>
                            @endforeach
                        </div>
                    </div>
                    @endif

                    @if($emailTemplate->description)
                    <div>
                        <label class="text-muted small d-block mb-2">
                            <i class="bi bi-file-text me-1"></i>Description
                        </label>
                        <div class="bg-light rounded-3 p-3">
                            <p class="mb-0 text-dark">{{ $emailTemplate->description }}</p>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Formulaire de test -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm rounded-4 mb-4">
                <div class="card-header bg-success bg-opacity-10 border-0 rounded-top-4 py-3">
                    <h5 class="mb-0 fw-bold text-success">
                        <i class="bi bi-send me-2"></i>Envoyer un email de test
                    </h5>
                </div>
                <div class="card-body p-4">
                    <form action="{{ route('admin.email-templates.send-test', $emailTemplate) }}" method="POST">
                        @csrf
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label fw-semibold">
                                    <i class="bi bi-envelope me-2 text-success"></i>Email de test *
                                </label>
                                <input type="email" name="test_email" required 
                                    class="form-control form-control-lg rounded-3" 
                                    placeholder="votre-email@example.com">
                            </div>
                            
                            @if($emailTemplate->variables && count($emailTemplate->variables) > 0)
                            <div class="col-12">
                                <label class="form-label fw-semibold">
                                    <i class="bi bi-code-square me-2 text-success"></i>Données de test
                                    <small class="text-muted fw-normal">(optionnel - Format: variable=valeur par ligne)</small>
                                </label>
                                <textarea name="test_data" rows="4" 
                                    class="form-control rounded-3 font-monospace" 
                                    style="font-size: 13px;"
                                    placeholder="@foreach($emailTemplate->variables as $var){{ $var }}=Valeur Test
@endforeach"></textarea>
                                <small class="text-muted mt-1 d-block">
                                    <i class="bi bi-info-circle me-1"></i>Les variables non renseignées utiliseront leur nom par défaut
                                </small>
                            </div>
                            @endif

                            <div class="col-12">
                                <button type="submit" class="btn btn-success btn-lg rounded-pill px-5 shadow">
                                    <i class="bi bi-send-fill me-2"></i>Envoyer l'email de test
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Prévisualisation -->
            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-header bg-info bg-opacity-10 border-0 rounded-top-4 py-3">
                    <h5 class="mb-0 fw-bold text-info">
                        <i class="bi bi-eye me-2"></i>Prévisualisation
                    </h5>
                </div>
                <div class="card-body p-4">
                    <div class="border rounded-4 overflow-hidden shadow-sm bg-white" style="min-height: 400px;">
                        <iframe srcdoc="{!! htmlspecialchars($emailTemplate->body) !!}" 
                            class="w-100 border-0" style="height: 500px;"></iframe>
                    </div>
                </div>
            </div>
        </div>

        <!-- Code HTML -->
        <div class="col-12">
            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-header bg-dark border-0 rounded-top-4 py-3">
                    <h5 class="mb-0 fw-bold text-white">
                        <i class="bi bi-code-slash me-2"></i>Code HTML du template
                    </h5>
                </div>
                <div class="card-body p-0">
                    <pre class="bg-dark text-light p-4 m-0 rounded-bottom-4 overflow-auto" style="max-height: 400px;"><code class="text-success" style="font-size: 13px;">{{ $emailTemplate->body }}</code></pre>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
