<x-app-layout>
    @section('page-title', $pageTitle)
    <div class="card shadow-sm border-0">
        <div class="card-header bg-white d-flex justify-content-between align-items-center">
            <h5 class="my-0 fw-bold">Kelola Rekomendasi Pembelajaran</h5>
            <a href="{{ route('admin.rekomendasi.create') }}" class="btn btn-primary">
                <i class="bi bi-plus-circle-fill me-2"></i>Buat Rekomendasi
            </a>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>No</th>
                            <th>Thumbnail</th>
                            <th>Judul</th>
                            <th>Kategori</th>
                            <th class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($semuaRekomendasi as $item)
                        <tr>
                            <td>{{ $loop->iteration }}</td>
                            <td><img src="{{ asset($item['thumbnail']) }}" width="120" class="rounded shadow-sm" alt="Thumbnail"></td>
                            <td class="fw-semibold">{{ $item['judul'] }}</td>
                            <td><span class="badge bg-primary">{{ $item['kategori'] }}</span></td>
                            <td class="text-center">
                                <div class="d-flex justify-content-center gap-2">
                                    <a href="{{ route('admin.rekomendasi.edit', $item['id']) }}" class="btn btn-sm btn-outline-primary" title="Edit">
                                        <i class="bi bi-pencil-fill"></i>
                                    </a>
                                    
                                    <button type="button" class="btn btn-sm btn-outline-danger btn-hapus" 
                                        data-id="{{ $item['id'] }}" 
                                        data-judul="{{ $item['judul'] }}" 
                                        title="Hapus">
                                        <i class="bi bi-trash-fill"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const deleteButtons = document.querySelectorAll('.btn-hapus');
            
            deleteButtons.forEach(button => {
                button.addEventListener('click', function() {
                    const id = this.getAttribute('data-id');
                    const judul = this.getAttribute('data-judul');
                    const btnElement = this;
                    
                    modernSwal.fire({
                        title: 'Hapus Rekomendasi?',
                        text: `Anda yakin ingin menghapus "${judul}"? Tindakan ini tidak dapat dibatalkan.`,
                        icon: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#dc3545', 
                        cancelButtonColor: '#6c757d',  
                        confirmButtonText: 'Ya, Hapus!',
                        cancelButtonText: 'Batal'
                    }).then((result) => {
                        if (result.isConfirmed) {
                            
                            
                            modernSwal.fire({
                                title: 'Menghapus...',
                                timer: 800,
                                didOpen: () => Swal.showLoading()
                            }).then(() => {
                                const row = btnElement.closest('tr');
                                
                                row.style.transition = "opacity 0.5s ease";
                                row.style.opacity = 0;
                                
                                setTimeout(() => {
                                    row.remove();
                                }, 500);

                                Toast.fire({
                                    icon: 'success',
                                    title: 'Berhasil',
                                    text: 'Data rekomendasi berhasil dihapus.'
                                });
                            });
                            
                        }
                    });
                });
            });
        });
    </script>
    @endpush
</x-app-layout>