<main class="container-fluid py-4">
    <div class="row g-4">
        <div class="col-lg-3"><?php require APP_PATH . '/Views/admin/_sidebar.php'; ?></div>
        <div class="col-lg-9">
            <h1 class="h3 fw-bold text-cfc-dark mb-4">Links Rápidos</h1>
            <div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4">
                <h2 class="h5 fw-bold mb-3">Salvar link de outro site</h2>
                <form method="post" action="<?= e(url('admin/links/save')) ?>" class="row g-3">
                    <?= CSRF::input() ?>
                    <div class="col-md-4"><label class="form-label">Nome do site</label><input name="name" class="form-control" required placeholder="Ex: DETRAN BA"></div>
                    <div class="col-md-5"><label class="form-label">Link</label><input name="url" class="form-control" required placeholder="https://..."></div>
                    <div class="col-md-3"><label class="form-label">Observação</label><input name="notes" class="form-control" placeholder="Opcional"></div>
                    <div class="col-12"><button class="btn btn-primary rounded-pill px-4"><i class="bi bi-save"></i> Salvar link</button></div>
                </form>
            </div></div>
            <div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4">
                <h2 class="h5 fw-bold mb-3">Meus Links</h2>
                <div class="table-responsive"><table class="table align-middle"><thead><tr><th>Site</th><th>Link</th><th>Status</th><th>Editar</th><th class="text-end">Ações</th></tr></thead><tbody>
                    <?php foreach ($links as $link): ?>
                        <tr>
                            <td><strong><?= e($link['name']) ?></strong><?php if (!empty($link['notes'])): ?><small class="d-block text-muted"><?= e($link['notes']) ?></small><?php endif; ?></td>
                            <td><a target="_blank" rel="noopener" class="btn btn-success btn-sm rounded-pill px-3" href="<?= e($link['url']) ?>"><i class="bi bi-box-arrow-up-right"></i> Acessar</a></td>
                            <td><?= !empty($link['is_active']) ? '<span class="badge text-bg-success">Ativo</span>' : '<span class="badge text-bg-secondary">Inativo</span>' ?></td>
                            <td style="min-width:330px"><form method="post" action="<?= e(url('admin/links/save')) ?>" class="row g-2"><?= CSRF::input() ?><input type="hidden" name="link_id" value="<?= (int)$link['id'] ?>"><div class="col-md-4"><input name="name" class="form-control form-control-sm" value="<?= e($link['name']) ?>"></div><div class="col-md-5"><input name="url" class="form-control form-control-sm" value="<?= e($link['url']) ?>"></div><div class="col-md-3"><button class="btn btn-sm btn-primary w-100">Salvar</button></div><div class="col-12"><input name="notes" class="form-control form-control-sm" value="<?= e($link['notes'] ?? '') ?>" placeholder="Observação"></div></form></td>
                            <td class="text-end"><div class="d-flex flex-wrap gap-2 justify-content-end"><a target="_blank" class="btn btn-sm btn-success rounded-pill" href="<?= e($link['url']) ?>">Abrir</a><form method="post" action="<?= e(url('admin/links/toggle')) ?>"><?= CSRF::input() ?><input type="hidden" name="link_id" value="<?= (int)$link['id'] ?>"><button class="btn btn-sm btn-outline-primary rounded-pill"><?= !empty($link['is_active']) ? 'Desativar' : 'Ativar' ?></button></form><form method="post" action="<?= e(url('admin/links/delete')) ?>" onsubmit="return confirm('Apagar este link?');"><?= CSRF::input() ?><input type="hidden" name="link_id" value="<?= (int)$link['id'] ?>"><button class="btn btn-sm btn-outline-danger rounded-pill">Apagar</button></form></div></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (!$links): ?><tr><td colspan="5" class="text-center text-muted py-4">Nenhum link salvo.</td></tr><?php endif; ?>
                </tbody></table></div>
            </div></div>
        </div>
    </div>
</main>
