@extends('admin.layouts.app')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h3 class="card-title">Modern Certificate Generator</h3>
                        <div>
                            <a href="{{ route('admin.modern-certificates.create') }}" class="btn btn-primary">
                                <i class="fas fa-plus"></i> Generate Single Certificate
                            </a>
                            <a href="{{ route('admin.modern-certificates.bulk-create') }}" class="btn btn-success">
                                <i class="fas fa-upload"></i> Bulk Generate
                            </a>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    @if(session('success'))
                        <div class="alert alert-success alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                            {{ session('success') }}
                        </div>
                    @endif

                    @if(session('error'))
                        <div class="alert alert-danger alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                            {{ session('error') }}
                        </div>
                    @endif

                    <!-- Available Templates -->
                    <div class="row mb-4">
                        <div class="col-12">
                            <h5><i class="fas fa-paint-brush"></i> Available Templates</h5>
                            @if($templates->count() > 0)
                                <div class="row">
                                    @foreach($templates as $template)
                                        <div class="col-md-4 mb-3">
                                            <div class="card">
                                                @if($template->background_image)
                                                    <img src="{{ $template->background_url }}" class="card-img-top"
                                                         alt="{{ $template->name }}" style="height: 150px; object-fit: cover;">
                                                @else
                                                    <div class="card-img-top bg-light d-flex align-items-center justify-content-center"
                                                         style="height: 150px;">
                                                        <i class="fas fa-image fa-2x text-muted"></i>
                                                    </div>
                                                @endif
                                                <div class="card-body">
                                                    <h6 class="card-title">{{ $template->name }}</h6>
                                                    <p class="card-text text-muted small">
                                                        {{ Str::limit($template->description, 80) }}
                                                    </p>
                                                    <div class="d-flex justify-content-between align-items-center">
                                                        <span class="badge badge-{{ $template->is_active ? 'success' : 'secondary' }}">
                                                            {{ $template->is_active ? 'Active' : 'Inactive' }}
                                                        </span>
                                                        <span class="badge badge-info">{{ $template->category }}</span>
                                                    </div>
                                                </div>
                                                <div class="card-footer">
                                                    <a href="{{ route('admin.modern-certificates.create') }}?template={{ $template->id }}"
                                                       class="btn btn-sm btn-primary btn-block">
                                                        <i class="fas fa-magic"></i> Use This Template
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    @endforeach
                                </div>
                            @else
                                <div class="alert alert-info">
                                    <i class="fas fa-info-circle"></i> No templates available.
                                    <a href="{{ route('admin.template-builder.create') }}">Create a template first</a>.
                                </div>
                            @endif
                        </div>
                    </div>

                    <!-- Recent Certificates -->
                    <div class="row">
                        <div class="col-12">
                            <h5><i class="fas fa-certificate"></i> Recent Certificates</h5>
                            @if($recentCertificates->count() > 0)
                                <div class="table-responsive">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Student</th>
                                                <th>Template</th>
                                                <th>Course</th>
                                                <th>Date</th>
                                                <th>Status</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($recentCertificates as $certificate)
                                                <tr>
                                                    <td>
                                                        <strong>{{ $certificate->student_name }}</strong>
                                                        @if($certificate->student_email)
                                                            <br><small class="text-muted">{{ $certificate->student_email }}</small>
                                                        @endif
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-info">{{ $certificate->template->name ?? 'N/A' }}</span>
                                                    </td>
                                                    <td>{{ $certificate->course_name ?? 'N/A' }}</td>
                                                    <td>{{ $certificate->formatted_graduation_date }}</td>
                                                    <td>
                                                        <span class="badge badge-{{ $certificate->status_badge }}">
                                                            {{ ucfirst($certificate->status) }}
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <div class="btn-group">
                                                            @if($certificate->generated_image_path)
                                                                <a href="{{ $certificate->download_url }}"
                                                                   class="btn btn-sm btn-success" title="Download">
                                                                    <i class="fas fa-download"></i>
                                                                </a>
                                                            @endif
                                                            <a href="{{ route('admin.modern-certificates.show', $certificate) }}"
                                                               class="btn btn-sm btn-info" title="View">
                                                                <i class="fas fa-eye"></i>
                                                            </a>
                                                            <form action="{{ route('admin.modern-certificates.destroy', $certificate) }}"
                                                                  method="POST" class="d-inline"
                                                                  onsubmit="return confirm('Are you sure you want to delete this certificate?')">
                                                                @csrf
                                                                @method('DELETE')
                                                                <button type="submit" class="btn btn-sm btn-danger" title="Delete">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </form>
                                                        </div>
                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            @else
                                <div class="alert alert-info">
                                    <i class="fas fa-info-circle"></i> No certificates generated yet.
                                    <a href="{{ route('admin.modern-certificates.create') }}">Generate your first certificate</a>.
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
.card-img-top {
    transition: transform 0.2s;
}

.card:hover .card-img-top {
    transform: scale(1.05);
}

.badge {
    font-size: 0.75rem;
}
</style>
@endpush

@push('scripts')
<script>
$(document).ready(function() {
    // Auto-hide alerts
    setTimeout(function() {
        $('.alert').fadeOut('slow');
    }, 5000);

    // Add template ID to form if template is selected
    const urlParams = new URLSearchParams(window.location.search);
    const templateId = urlParams.get('template');
    if (templateId) {
        // Store template ID for use in create form
        sessionStorage.setItem('selectedTemplateId', templateId);
    }
});
</script>
@endpush











