<?php
include '../config.php';
include '../configuracao/modelo.php'; // Garante que a função validaAcessoTela está carregada

session_start();
$numero_da_tela = 10; // ID gerado pelo banco

    if (!validaAcessoTela($mysqli, $_SESSION['id'], $numero_da_tela)) {
        echo "<script>alert('Sem permissão para o teste número $numero_da_tela!'); window.location.href='../lista/index.php';</script>";
        exit();
    }


?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body { background-color: #3a6ea5; font-family: "Tahoma", sans-serif; margin: 0; padding: 5px; font-size: 11px; }
        .win-window { background: #c0c0c0; border: 2px solid; border-color: #fff #808080 #808080 #fff; box-shadow: 1px 1px 0 0 #000; width: 100%; height: 98vh; display: flex; flex-direction: column; }
        .win-title { background: #000080; color: white; padding: 3px 6px; font-weight: bold; display: flex; justify-content: space-between; }
        .win-content { padding: 10px; flex: 1; overflow: auto; }
        .win-btn { background: #c0c0c0; border: 2px solid; border-color: #fff #808080 #808080 #fff; padding: 4px 10px; cursor: pointer; font-size: 11px; margin-right: 2px; }
        .win-btn:active { border-color: #808080 #fff #fff #808080; }
        .win-table { width: 100%; border-collapse: collapse; background: #fff; border: 2px solid #808080; }
        .win-table th { background: #c0c0c0; border: 1px solid #808080; padding: 4px; text-align: left; }
        .win-table td { border: 1px solid #dfdfdf; padding: 3px; }
        .win-input { border: 2px solid; border-color: #808080 #fff #fff #808080; padding: 2px; }
    </style>
</head>
<body>
<div class="win-window">
    <div class="win-title"><span>Explorar: <?php echo $_GET['tabela']; ?></span><span>[?] [X]</span></div>
    <div class="win-content">
        <form action="index.php" method="GET" style="margin-bottom:10px; display:flex; gap:5px; align-items:center;">
            <input type="hidden" name="tabela" value="<?php echo $_GET['tabela'] ?>">
            <span>Filtrar:</span>
            <select name="vlr_filtro" class="win-input">
                <?php
                $constructor = $mysqli->prepare("SELECT campo_nome from campo WHERE campo_tabela ='".$_GET['tabela']."';");
                $constructor->execute(); $constructor->store_result(); $constructor->bind_result($codigo_tab);
                while ($constructor->fetch()) echo "<option value=\"$codigo_tab\">$codigo_tab</option>";
                ?>
            </select>
            <input name="vlr_pesq" class="win-input" type="text" placeholder="Busca...">
            <button type="submit" class="win-btn">Buscar</button>
        </form>

        <form action="reg_.php" method="POST" style="margin-bottom:10px;">
            <input type="hidden" name="tabela" value="<?php echo $_GET['tabela'] ?>">
            <input type="hidden" name="acao" value="novo">
            <button type="submit" class="win-btn"><b>Novo Registro</b></button>
        </form>

        <table class="win-table">
            <?php 
            $constructor = $mysqli->prepare("select tabela_campo_1,tabela_campo_2,tabela_campo_3 from tabela where tabela_nome='".$_GET['tabela']."';");
            $constructor->execute(); $constructor->store_result(); $constructor->bind_result($v1,$v2,$v3); $constructor->fetch();
            ?>
            <thead>
                <tr>
                    <th><?php echo $v1; ?></th><th><?php echo $v2; ?></th><th><?php echo $v3; ?></th>
                    <th width="180">Operações</th>
                </tr>
            </thead>
            <tbody>
            <?php
            // Lógica de busca original mantida
            $sql = "select codigo,".$v1.",".$v2.",".$v3.",data,enumsistema from ".$_GET['tabela']." limit 99";
            if(!empty($_GET['vlr_pesq'])) {
                $sql = "select codigo,".$v1.",".$v2.",".$v3.",data,enumsistema from ".$_GET['tabela']." WHERE ".$_GET['vlr_filtro']." like '%".$_GET['vlr_pesq']."%' limit 99";
            }
            $res = $mysqli->query($sql);
            while ($r = $res->fetch_array()){ ?>
            <tr>
                <td><?php echo $r[1]; ?></td><td><?php echo $r[2]; ?></td><td><?php echo $r[3]; ?></td>
                <td style="display:flex;">
                    <form action="reg_.php" method="post"><input type="hidden" name="acao" value="visualizar"><input type="hidden" name="enumsistema" value="<?php echo $r['enumsistema']?>"><input type="hidden" name="tabela" value="<?php echo $_GET['tabela'] ?>"><input type="submit" value="Ver" class="win-btn"></form>
                    <form action="reg_.php" method="post"><input type="hidden" name="acao" value="editar"><input type="hidden" name="tabela" value="<?php echo $_GET['tabela'] ?>"><input type="hidden" name="enumsistema" value="<?php echo $r['enumsistema']?>"><input type="submit" value="Editar" class="win-btn"></form>
                    <form action="reg_acao.php" method="post"><input type="hidden" name="acao" value="apagar"><input type="hidden" name="enumsistema" value="<?php echo $r['enumsistema']?>"><input type="hidden" name="tabela" value="<?php echo $_GET['tabela'] ?>"><input type="submit" value="Excluir" class="win-btn" onclick="return confirm('Apagar?')"></form>
                </td>
            </tr>
            <?php } ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>