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

// Filtros seguros
$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 LIKE :busca OR cpf LIKE :busca OR cargo LIKE :busca)";
    $params['busca'] = "%{$busca}%";
}
if ($status_filtro !== '') {
    $where .= " AND status = :status";
    $params['status'] = $status_filtro;
}

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

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

<div class="page-header">
    <div>
        <h2>Gestao de Funcionarios</h2>
        <div class="subtitle"><?= $total ?> funcionario(s) cadastrado(s)</div>
    </div>
    <div class="d-flex gap-10">
        <a href="funcionario_form.php" class="btn btn-novo">+ Novo Funcionario</a>
    </div>
</div>

<form method="GET" class="filtros-bar">
    <div class="form-group">
        <label>Buscar</label>
        <input name="busca" placeholder="Nome, CPF ou cargo" 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</th>
    <th>CPF</th>
    <th>Cargo</th>
    <th>Telefone</th>
    <th>Admissao</th>
    <th>Salario</th>
    <th>Status</th>
    <th class="text-end">Acoes</th>
</tr>
</thead>
<tbody>
<?php if (empty($funcionarios)): ?>
<tr><td colspan="8" class="text-center text-muted" style="padding:30px;">Nenhum funcionario encontrado</td></tr>
<?php else: ?>
<?php foreach ($funcionarios as $f): ?>
<tr>
    <td><strong><?= sanitize($f['nome']) ?></strong>
        <?php if (!empty($f['codigo'])): ?><br><small class="text-muted"><?= sanitize($f['codigo']) ?></small><?php endif; ?>
    </td>
    <td><?= sanitize($f['cpf'] ?? '-') ?></td>
    <td><?= sanitize($f['cargo'] ?? '-') ?></td>
    <td><?= sanitize($f['telefone'] ?? '-') ?></td>
    <td><?= !empty($f['data_admissao']) ? data_br($f['data_admissao']) : '-' ?></td>
    <td><?= !empty($f['salario']) ? moeda($f['salario']) : '-' ?></td>
    <td>
        <span class="badge <?= $f['status'] === 'Ativo' ? 'badge-ativo' : 'badge-inativo' ?>">
            <?= sanitize($f['status']) ?>
        </span>
    </td>
    <td class="text-end">
        <a href="funcionario_form.php?id=<?= intval($f['id']) ?>" class="btn btn-sm btn-info" title="Editar">&#9998;</a>
        <a href="funcionario_status.php?id=<?= intval($f['id']) ?>" class="btn btn-sm btn-warning" title="Ativar/Inativar">&#9899;</a>
        <a href="funcionario_excluir.php?id=<?= intval($f['id']) ?>" class="btn btn-sm btn-danger" title="Excluir" onclick="return confirm('Deseja excluir este funcionario?')">&#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'; ?>
