<?php
require '../conexao/conexao.php';
require '../conexao/acesso.php';

$pageTitle = "Detalhe de Erros";

$idPessoa = $_GET['id_funcionario'];

// Preparando a declaração
$stmt = $connectionmysql->prepare("SELECT f.id_funcionario, f.nome, e.nome_erro, ml.obs_erro, ml.data_lancamento
    FROM funcionarios as f
    LEFT JOIN mapa_erros_lancados as ml ON f.id_funcionario = ml.id_funcionario
    INNER JOIN mapa_erros as e ON ml.id_erro = e.id_erro
    WHERE f.id_funcionario = ?");

if ($stmt) {
    // Vinculando parâmetros e executando a declaração
    $stmt->bind_param("i", $idPessoa); // 'i' indica que é um inteiro
    $stmt->execute();

    // Obtendo os resultados
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $pessoa = $result->fetch_assoc(); // preenchendo as variáveis com o retorno da query
    } else {
        echo "Nenhuma pessoa encontrada com o ID especificado.";
    }

    $stmt->close();
} else {
    echo "Erro ao preparar a declaração: " . $connectionmysql->error;
}

// Consulta para contar o número total de registros
$countStmt = $connectionmysql->prepare("SELECT COUNT(*) as total FROM mapa_erros_lancados WHERE id_funcionario = ?");
$countStmt->bind_param("i", $idPessoa);
$countStmt->execute();
$countResult = $countStmt->get_result();
$countRow = $countResult->fetch_assoc();
$total_records = $countRow['total'];

$records_per_page = 10;
$current_page = $_GET['page'] ?? 1;
$start_from = ($current_page - 1) * $records_per_page;

// Consulta paginada
$paginationStmt = $connectionmysql->prepare("SELECT f.id_funcionario, f.nome, e.nome_erro, ml.obs_erro, ml.data_lancamento
    FROM funcionarios as f
    LEFT JOIN mapa_erros_lancados as ml ON f.id_funcionario = ml.id_funcionario
    INNER JOIN mapa_erros as e ON ml.id_erro = e.id_erro
    WHERE f.id_funcionario = ?
    LIMIT ?, ?");
$paginationStmt->bind_param("iii", $idPessoa, $start_from, $records_per_page);
$paginationStmt->execute();
$paginationResult = $paginationStmt->get_result();

?>

<!DOCTYPE html>
<html lang="pt-br">
<?php

include('../geral/header.php');
?>
<body>

<!-- Navigation Bar-->
<?php
include('../geral/menu.php');
?>
<!-- End Navigation Bar-->

<!-- ============================================================== -->
<!-- Start Page Content here -->
<!-- ============================================================== -->

<div class="wrapper">
    <div class="container-fluid">
        <div class="card-box mt-3">
            <div class="row">
                <div class="col-6 text-left">
                    <h4 class="page-title">
                        <?php echo $pageTitle; ?>
                    </h4>
                </div>
                <div class="col-6 text-right">
                    <button onclick="goBack()" class="btn btn-light"><i class="mdi mdi-keyboard-backspace"></i> Voltar
                    </button>
                </div>
                <div class="col-12">
                    <div style="display: flex; justify-content: space-between; align-items: center;">
                       
                        <div>
                            <span>Total de registros: <?php echo $total_records; ?></span>
                        </div>
                    </div>

                    <table id="datatable-buttons"
                           class="table table-striped table-bordered dt-responsive nowrap">
                        <thead>
                        <tr class="text-center">
                            <th>Nome</th>
                            <th>Erro</th>
                            <th>Observação</th>
                            <th>Data</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        while ($erro = $paginationResult->fetch_assoc()) {
                            ?>
                            <tr class="text-center">
                                <td><?php echo htmlspecialchars($erro['nome']); ?></td>
                                <td><?php echo htmlspecialchars($erro['nome_erro']); ?></td>
                                <td><?php echo htmlspecialchars($erro['obs_erro']); ?></td>
                                <td><?php echo htmlspecialchars($erro['data_lancamento']); ?></td>
                            </tr>
                            <?php
                        }
                        ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div style="display: flex; justify-content: center;">
                <?php include('paginacao.php'); ?>
            </div>
        </div>
    </div>
</div>

<!-- end container -->

<!-- end wrapper -->

<!-- ============================================================== -->
<!-- End Page content -->
<!-- ============================================================== -->

<!-- Footer Start -->
<?php
include('../geral/footer.php');
?>
<!-- end Footer -->

<?php
include('../geral/mensagens_modais.php');
?>

<!-- Vendor js -->
<script src="../../assets/js/vendor.min.js"></script>

<!-- knob plugin -->
<script src="../../assets/libs/jquery-knob/jquery.knob.min.js"></script>
<!-- App js-->
<script
