<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>
                                <div class="d-flex gap-2">
                                    <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-delete"
                                        data-id="{{ $item['id'] }}"
                                        data-title="{{ $item['title'] }}">
                                        <i class="bi bi-trash3-fill"></i>
                                    </button>
                                    <form id="delete-form-{{ $item['id'] }}" action="{{ route('admin.pengumuman.destroy', $item['id']) }}" method="POST" style="display: none;">
                                        @csrf
                                        @method('DELETE')
                                    </form>
                                </div>
                            </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).on('click', '.btn-delete', function() {
            const id = $(this).data('id');
            const title = $(this).data('title');

            modernSwal.fire({
                title: 'Hapus Pengumuman?',
                text: `Apakah Anda yakin ingin menghapus pengumuman "${title}"?`,
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Ya, Hapus',
                confirmButtonColor: '#dc3545',
                cancelButtonText: 'Batal'
            }).then((result) => {
                if (result.isConfirmed) {
                    $(`#delete-form-${id}`).submit();
                }
            });
        });
    </script>
    @endpush
</x-app-layout>