@extends('admin.layouts.app')

@section('title', $pageTitle ?? 'IMT Montessori 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">{{ $pageTitle ?? 'شهادات المنتسورى' }}</h3>
                </div>
                <div class="card-body">
                    <h5>IMT Montessori Certificates</h5>

                    @if(isset($error))
                        <div class="alert alert-danger">
                            <strong>Error:</strong> {{ $error }}
                        </div>
                    @endif

                    @if(isset($debug))
                        <div class="alert alert-info">
                            <h6>Debug Information:</h6>
                            <ul>
                                @foreach($debug as $key => $value)
                                    <li><strong>{{ $key }}:</strong>
                                        @if(is_array($value))
                                            {{ implode(', ', $value) }}
                                        @else
                                            {{ $value }}
                                        @endif
                                    </li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    @if(isset($certificates) && $certificates->count() > 0)
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Student</th>
                                        <th>Certificate</th>
                                        <th>Upload Date</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($certificates as $certificate)
                                        <tr>
                                            <td>{{ $certificate->id }}</td>
                                            <td>
                                                @if($certificate->student)
                                                    <strong>{{ $certificate->student->full_name }}</strong><br>
                                                    <small class="text-muted">{{ $certificate->student->email ?? 'No email' }}</small>
                                                @else
                                                    <span class="text-muted">Student not found</span>
                                                @endif
                                            </td>
                                            <td>
                                                @if($certificate->template)
                                                    @php
                                                        $fileExt = pathinfo($certificate->template, PATHINFO_EXTENSION);
                                                        $isImage = in_array(strtolower($fileExt), ['jpg', 'jpeg', 'png']);
                                                    @endphp

                                                    @if($isImage)
                                                        <img src="{{ asset('storage/' . $certificate->template) }}"
                                                             alt="Certificate" style="max-width: 100px; max-height: 60px; border: 1px solid #ddd;">
                                                    @else
                                                        <i class="fas fa-file-pdf fa-2x text-danger"></i>
                                                    @endif
                                                    <br>
                                                    <small class="text-muted">{{ basename($certificate->template) }}</small>
                                                @else
                                                    <span class="text-muted">No file</span>
                                                @endif
                                            </td>
                                            <td>
                                                {{ date('M j, Y', $certificate->created_at) }}
                                            </td>
                                            <td>
                                                <div class="btn-group" role="group">
                                                    @if($certificate->template)
                                                        <a href="{{ asset('storage/' . $certificate->template) }}"
                                                           target="_blank" class="btn btn-sm btn-info">
                                                            <i class="fas fa-eye"></i> View
                                                        </a>
                                                        <a href="{{ asset('storage/' . $certificate->template) }}"
                                                           download class="btn btn-sm btn-success">
                                                            <i class="fas fa-download"></i> Download
                                                        </a>
                                                    @endif
                                                    <button type="button" class="btn btn-sm btn-danger"
                                                            onclick="deleteCertificate({{ $certificate->id }})">
                                                        <i class="fas fa-trash"></i> Delete
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>

                            <!-- Pagination -->
                            <div class="d-flex justify-content-center">
                                {{ $certificates->links() }}
                            </div>
                        </div>
                    @else
                        <div class="text-center py-4">
                            <i class="fas fa-certificate fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">No IMT Montessori Certificates Found</h5>
                            <p class="text-muted">Start by uploading certificates for your students.</p>
                        </div>
                    @endif

                    <div class="mt-4">
                        <a href="{{ route('admin.certificates.create_imtmontessori') }}" class="btn btn-primary">
                            <i class="fas fa-upload"></i>
                            Upload New Certificate
                        </a>
                        <a href="{{ route('admin.certificates.imtmontessori') }}" class="btn btn-secondary ml-2">
                            <i class="fas fa-refresh"></i>
                            Refresh List
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function deleteCertificate(certificateId) {
    if (confirm('Are you sure you want to delete this certificate? This action cannot be undone.')) {
        window.location.href = `/admin/certificates/${certificateId}/imtmontessori-certificates/delete`;
    }
}
</script>
@endsection
