@extends('admin.layouts.app')

@section('title', 'Certificates')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Generated Certificates</h3>
                    <div class="card-tools">
                        <a href="{{ route('admin.graduation-certificates.create') }}" class="btn btn-primary">
                            <i class="fas fa-plus"></i> Generate Certificate
                        </a>
                    </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') }}
                        </button>
                    @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') }}
                        </button>
                    @endif

                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Student</th>
                                    <th>Template</th>
                                    <th>Graduation Date</th>
                                    <th>Preview</th>
                                    <th>Created</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($certificates as $certificate)
                                    <tr>
                                        <td>{{ $certificate->id }}</td>
                                        <td>
                                            <strong>{{ $certificate->user->name }}</strong><br>
                                            <small class="text-muted">{{ $certificate->user->email }}</small>
                                        </td>
                                        <td>{{ $certificate->template->name }}</td>
                                        <td>{{ $certificate->graduation_date->format('M d, Y') }}</td>
                                        <td>
                                            @if($certificate->generated_image_path)
                                                <img src="{{ $certificate->generated_image_url }}"
                                                     alt="Certificate"
                                                     style="max-width: 100px; max-height: 60px; object-fit: cover;">
                                            @else
                                                <span class="text-muted">No image</span>
                                            @endif
                                        </td>
                                        <td>{{ $certificate->created_at->format('M d, Y') }}</td>
                                        <td>
                                            <a href="{{ route('admin.graduation-certificates.show', $certificate) }}"
                                               class="btn btn-sm btn-info" title="View">
                                                <i class="fas fa-eye"></i>
                                            </a>

                                            <a href="{{ route('admin.graduation-certificates.download', $certificate) }}"
                                               class="btn btn-sm btn-success" title="Download">
                                                <i class="fas fa-download"></i>
                                            </a>

                                            <form action="{{ route('admin.graduation-certificates.destroy', $certificate) }}"
                                                  method="POST"
                                                  style="display: inline-block;"
                                                  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>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="7" class="text-center">No certificates found.</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="d-flex justify-content-center">
                        {{ $certificates->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
