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

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

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

if ($busca !== '') {
    $where .= " AND nome_turma LIKE :busca";
    $params['busca'] = "%{$busca}%";
}
if ($status_filtro !== '') {
    $where .= " AND status = :status";
    $params['status'] = $status_filtro;
}

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

$sql = "SELECT * FROM turmas WHERE {$where} ORDER BY nome_turma ASC LIMIT {$por_pagina} OFFSET {$offset}";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$turmas = $stmt->fetchAll();
?>

<div class="page-header">
    <div>
        <h2>Gestao de Turmas</h2>
        <div class="subtitle"><?= $total ?> turma(s) cadastrada(s)</div>
    </div>
    <div class="d-flex gap-10">
        <a href="turma_form.php" class="btn btn-novo">+ Nova Turma</a>
    </div>
</div>

<form method="GET" class="filtros-bar">
    <div class="form-group">
        <label>Buscar</label>
        <input name="busca" placeholder="Nome da turma" value="<?= $busca ?>">
    </div>
    <div class="form-group" style="max-width:160px;">
        <label>Status</label>
        <select name="status">
            <option value="">Todos</option>
            <option value="Ativo" <?= $status_filtro === 'Ativo' ? 'selected' : '' ?>>Ativo</option>
            <option value="Inativo" <?= $status_filtro === 'Inativo' ? 'selected' : '' ?>>Inativo</option>
        </select>
    </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 da Turma</th>
    <th>Periodo</th>
    <th>Idade Min</th>
    <th>Idade Max</th>
    <th>Capacidade</th>
    <th>Professor</th>
    <th>Status</th>
    <th class="text-end">Acoes</th>
</tr>
</thead>
<tbody>
<?php if (empty($turmas)): ?>
<tr><td colspan="8" class="text-center text-muted" style="padding:30px;">Nenhuma turma encontrada</td></tr>
<?php else: ?>
<?php foreach ($turmas as $t): ?>
<tr>
    <td><strong><?= sanitize($t['nome_turma']) ?></strong></td>
    <td><?= sanitize($t['periodo'] ?? '-') ?></td>
    <td><?= intval($t['idade_min'] ?? 0) ?> anos</td>
    <td><?= intval($t['idade_max'] ?? 0) ?> anos</td>
    <td><?= intval($t['capacidade'] ?? 0) ?> alunos</td>
    <td><?= sanitize($t['professor_responsavel'] ?? '-') ?></td>
    <td>
        <span class="badge <?= $t['status'] === 'Ativo' ? 'badge-ativo' : 'badge-inativo' ?>">
            <?= sanitize($t['status']) ?>
        </span>
    </td>
    <td class="text-end">
        <a href="turma_form.php?id=<?= intval($t['id']) ?>" class="btn btn-sm btn-info" title="Editar">&#9998;</a>
        <a href="turma_excluir.php?id=<?= intval($t['id']) ?>" class="btn btn-sm btn-danger" title="Excluir" onclick="return confirm('Deseja excluir esta turma?')">&#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) ?>&status=<?= urlencode($status_filtro) ?>"
           class="<?= $i === $pagina ? 'ativo' : '' ?>"><?= $i ?></a>
    <?php endfor; ?>
</div>
<?php endif; ?>
</div>

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