<?php
require_once __DIR__ . '/../includes/header.php';

$busca = sanitize($_GET['busca'] ?? '');
$pagina = max(1, intval($_GET['pg'] ?? 1));
$por_pagina = 20;
$offset = ($pagina - 1) * $por_pagina;

$where = "1=1";
$params = [];

if ($busca !== '') {
    $where .= " AND (r.nome LIKE :busca OR r.cpf LIKE :busca)";
    $params['busca'] = "%{$busca}%";
}

$stmt_total = $pdo->prepare("SELECT COUNT(*) FROM responsaveis r WHERE {$where}");
$stmt_total->execute($params);
$total = $stmt_total->fetchColumn();
$total_paginas = ceil($total / $por_pagina);

$sql = "SELECT r.*, a.nome AS nome_aluno
        FROM responsaveis r
        LEFT JOIN alunos a ON r.aluno_id = a.id
        WHERE {$where}
        ORDER BY r.nome ASC
        LIMIT {$por_pagina} OFFSET {$offset}";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$responsaveis = $stmt->fetchAll();
?>

<div class="page-header">
    <div>
        <h2>Gestao de Responsaveis</h2>
        <div class="subtitle"><?= $total ?> responsavel(is) cadastrado(s)</div>
    </div>
    <div class="d-flex gap-10">
        <a href="responsavel_form.php" class="btn btn-novo">+ Novo Responsavel</a>
    </div>
</div>

<form method="GET" class="filtros-bar">
    <div class="form-group">
        <label>Buscar</label>
        <input name="busca" placeholder="Nome ou CPF" value="<?= $busca ?>">
    </div>
    <div class="form-group" style="max-width:100px;">
        <label>&nbsp;</label>
        <button class="btn btn-primary w-100">Filtrar</button>
    </div>
</form>

<div class="card-premium">
<div class="table-responsive">
<table class="table-premium">
<thead>
<tr>
    <th>Nome</th>
    <th>CPF</th>
    <th>Telefone</th>
    <th>Aluno Vinculado</th>
    <th>Parentesco</th>
    <th class="text-end">Acoes</th>
</tr>
</thead>
<tbody>
<?php if (empty($responsaveis)): ?>
<tr><td colspan="6" class="text-center text-muted" style="padding:30px;">Nenhum responsavel encontrado</td></tr>
<?php else: ?>
<?php foreach ($responsaveis as $r): ?>
<tr>
    <td><strong><?= sanitize($r['nome']) ?></strong></td>
    <td><?= sanitize($r['cpf'] ?? '-') ?></td>
    <td><?= sanitize($r['telefone'] ?? '-') ?></td>
    <td><?= sanitize($r['nome_aluno'] ?? '-') ?></td>
    <td><?= sanitize($r['parentesco'] ?? $r['grau_parentesco'] ?? '-') ?></td>
    <td class="text-end">
        <a href="responsavel_form.php?id=<?= intval($r['id']) ?>" class="btn btn-sm btn-info" title="Editar">&#9998;</a>
        <a href="responsavel_excluir.php?id=<?= intval($r['id']) ?>" class="btn btn-sm btn-danger" title="Excluir" onclick="return confirm('Deseja excluir este responsavel?')">&#10005;</a>
    </td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>

<?php if ($total_paginas > 1): ?>
<div class="paginacao">
    <?php for ($i = 1; $i <= $total_paginas; $i++): ?>
        <a href="?pg=<?= $i ?>&busca=<?= urlencode($busca) ?>"
           class="<?= $i === $pagina ? 'ativo' : '' ?>"><?= $i ?></a>
    <?php endfor; ?>
</div>
<?php endif; ?>
</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
