<div class="container" style="max-width: 100%;">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item active" aria-current="page">Lista de Decretos</li>
        </ol>
    </nav>

    <?php Sessao::mensagem('decretos'); ?>

    <a href="<?= URL ?>/decretos/cadastrar" class="btn btn-primary mb-3">
        <i class="fas fa-plus-circle"></i> Cadastrar Novo Decreto
    </a>

    <style>
        #tabela-decretos th,
        #tabela-decretos td {
            text-align: center;
            vertical-align: middle;
        }

        @media (max-width: 768px) {
            .table td,
            .table th {
                font-size: 0.8rem;
                padding: 0.4rem;
            }

            .btn-sm {
                font-size: 0.75rem;
                padding: 0.3rem 0.5rem;
            }
        }
    </style>

    <div class="table-responsive">
        <table id="tabela-decretos" class="table table-striped table-bordered table-hover datatable" style="width:100%">
            <thead class="table-light">
                <tr>
                    <th>#</th>
                    <th>Título</th>
                    <th>Ano</th>
                    <th>Descrição</th>
                    <th>PDF</th>
                    <th>Opções</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($dados['decretos'])): ?>
                    <?php foreach ($dados['decretos'] as $decreto): ?>
                        <tr>
                            <td><?= $decreto->id_decreto ?></td>
                            <td><?= htmlspecialchars($decreto->titulo) ?></td>
                            <td><?= htmlspecialchars($decreto->ano) ?></td>
                            <td><?= htmlspecialchars($decreto->descricao) ?></td>
                            <td>
                                <?php if (!empty($decreto->arquivo_pdf)): ?>
                                    <a href="<?= URL ?>/uploads/decretos/<?= $decreto->arquivo_pdf ?>" target="_blank" class="btn btn-sm btn-primary">
                                        <i class="fas fa-file-pdf"></i> Ver
                                    </a>
                                <?php else: ?>
                                    <span class="text-muted">Sem PDF</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center">
                                <div class="d-flex flex-wrap justify-content-start gap-1">
                                    <a href="<?= URL ?>/decretos/editar/<?= $decreto->id_decreto ?>" class="btn btn-warning btn-sm" title="Editar">
                                        <i class="fas fa-edit"></i> Editar
                                    </a>
                                    <button class="btn btn-danger btn-sm btn-excluir" data-id="<?= $decreto->id_decreto ?>" title="Excluir">
                                        <i class="fas fa-trash-alt"></i> Excluir
                                    </button>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="6" class="text-center">Nenhum decreto cadastrado.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- SweetAlert2 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<!-- DataTables -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap5.min.css">
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap5.min.js"></script>

<!-- Font Awesome (ícones) -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<script>
    document.addEventListener('DOMContentLoaded', function () {
        document.querySelectorAll('.btn-excluir').forEach(function (btn) {
            btn.addEventListener('click', function () {
                const id = this.dataset.id;
                Swal.fire({
                    title: 'Tem certeza?',
                    text: "Essa ação não poderá ser desfeita!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#d33',
                    cancelButtonColor: '#3085d6',
                    confirmButtonText: 'Sim, excluir!',
                    cancelButtonText: 'Cancelar'
                }).then((result) => {
                    if (result.isConfirmed) {
                        window.location.href = "<?= URL ?>/decretos/excluir/" + id;
                    }
                });
            });
        });

        $('#tabela-decretos').DataTable({
            language: {
                url: "//cdn.datatables.net/plug-ins/1.13.4/i18n/pt-BR.json"
            },
            columnDefs: [
                { orderable: false, targets: -1 }
            ]
        });
    });
</script>
