<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 Pengumuman</h5>
            <a href="{{ route('admin.pengumuman.create') }}" class="btn btn-primary">
                <i class="bi bi-plus-circle-fill me-2"></i>Buat Pengumuman
            </a>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead>
                        <tr>
                            <th scope="col">No</th>
                            <th scope="col">Tipe</th>
                            <th scope="col">Judul</th>
                            <th scope="col">Isi</th>
                            <th scope="col" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($semuaPengumuman as $item)
                        <tr>
                            <td class="row-number">{{ $loop->iteration }}</td>
                            <td>
                                <span class="badge bg-{{ $item['type'] }}-subtle text-{{ $item['type'] }}-emphasis">{{ Str::title($item['type']) }}</span>
                            </td>
                            <td>{{ $item['title'] }}</td>
                            <td class="truncate-text" style="max-width: 300px;">
                                {{ $item['content'] }}
                            </td>
                            <td class="text-center">
                                <a href="{{ route('admin.pengumuman.edit', $item['id']) }}" class="btn btn-sm btn-outline-secondary">
                                    <i class="bi bi-pencil-fill"></i>
                                </a>
                                <button class="btn btn-sm btn-outline-danger btn-hapus" data-judul="{{ $item['title'] }}">
                                    <i class="bi bi-trash3-fill"></i>
                                </button>
                            </td>
                        </tr>
                        @empty
                        <tr class="empty-row">
                            <td colspan="5" class="text-center py-4">
                                <p class="text-muted mb-0">Belum ada pengumuman yang dibuat.</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            $(document).on('click', '.btn-hapus', function() {
                const judul = $(this).data('judul');
                const row = $(this).closest('tr');

                modernSwal.fire({
                    title: 'Hapus Pengumuman?',
                    text: `Apakah Anda yakin ingin menghapus pengumuman "${judul}"?`,
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonText: 'Ya, Hapus!',
                    cancelButtonText: 'Batal',
                    confirmButtonColor: '#dc3545',
                    cancelButtonColor: '#6c757d'
                }).then((result) => {
                    if (result.isConfirmed) {
                        modernSwal.fire({
                            title: 'Menghapus...',
                            timer: 800,
                            didOpen: () => Swal.showLoading()
                        }).then(() => {
                            Toast.fire({
                                icon: 'success',
                                title: 'Berhasil',
                                text: 'Pengumuman telah dihapus.'
                            });
                            
                            row.fadeOut('slow', function() {
                                $(this).remove();
                                
                                let count = 0;
                                $('tbody tr').not('.empty-row').each(function(index) {
                                    $(this).find('.row-number').text(index + 1);
                                    count++;
                                });

                                if(count === 0) {
                                    $('tbody').append(`
                                        <tr class="empty-row">
                                            <td colspan="5" class="text-center py-4">
                                                <p class="text-muted mb-0">Belum ada pengumuman yang dibuat.</p>
                                            </td>
                                        </tr>
                                    `);
                                }
                            });
                        });
                    }
                });
            });
        });
    </script>
    @endpush
</x-app-layout>