<?php
define('ACCESS_ALLOWED', true);

// --- Config e DB ---
require_once('base_functions/config.php');
require_once('base_functions/db_connection.php');
require_once('db_calls/db_calls.php');
require_once('base_functions/functions.php');

session_start();

// Controllo accesso/validità Recaptcha
if (!isset($_SESSION['accesso_ok']) || $_SESSION['accesso_ok'] !== true) {
    // Utente NON autenticato
    header("Location: login.php");
    exit;
}

// Assicurati che la connessione e la pagina siano UTF-8
header('Content-Type: text/html; charset=utf-8');
$conn->set_charset('utf8mb4');

// Funzione helper per escape sicuro
function e($string) {
    return htmlspecialchars($string ?? '', ENT_QUOTES, 'UTF-8');
}

// --- Sanitizzazione titolo ---
$titolocontenuto_safe = ($titoloContenuto ?? 'Titolo non disponibile');

// --- Verifica esito query ---
if (!isset($result) || !$result) {
    error_log("Errore query database: " . ($conn->error ?? 'Errore sconosciuto'));
    echo '<p class="error">Errore interno. Contattare l\'amministratore.</p>';

    // Crea un oggetto vuoto per evitare errori nel while
    $result = new ArrayObject();
}
?>

<!-- Header -->
<?php include('layout/header.php'); ?>

<!-- Sidebar -->
<?php include('layout/sidebar.php'); ?>

<h2 id="titolocontenuto"><?php echo $safeNomePortale . " " . $safeNomeAzienda; ?></h2>
<div class="table-container">
	
	<!-- WARNING SCORREVOLE CONDIZIONALE SE ESISTE DOCUMENTO VARIAZIONE PIANO LOGISTICO PER FESTIVITA'-->
	<?php
		if(checkFileExists($safedocpl)){		
			 include('base_functions/alertbox_scorrevole.php'); 
		}
	?>

	<br>
    <button id="printBtn" class="print-button">
        <img src="icons/print.svg" alt="Stampa" title="Stampa" width="20" height="20">
        Stampa
    </button>

    <input type="text" id="searchInput" placeholder="Cerca per comune, provincia o cap..." aria-label="Cerca in Piano Logistico">

    <div id="printArea">
        <table id="userTable">
            <colgroup>
                <col width=20%>
                <col width="2%">
                <col width="2%">
				<col width="20%">
                <col width="5%">
                <col width="5%">
                <col width="5%">
                <col width="5%">
                <col width="5%">
                <col width="5%">
            </colgroup>

            <thead>
                <tr>
                    <th>Comune</th>
                    <th>Prov</th>
                    <th>Cap</th>
					<th></th>
                    <th class="green">1°ordine</th>
                    <th class="green">1°consegna</th>
                    <th class="orange">2°ordine</th>
                    <th class="orange">2°consegna</th>
                   	<th class="red">3°ordine</th>
                    <th class="red">3°consegna</th>
                </tr>
            </thead>

            <tbody>
                <?php while ($row = $result->fetch_assoc()): ?>
                    <tr>
                        <td data-label="Comune"><?= e($row['comune']) ?></td>
                        <td data-label="Prov"><?= e($row['provincia']) ?></td>
                        <td data-label="Cap"><?= e($row['cap']) ?></td>
					
						<td data label="spacer"></td>
					
                        <td class="green" data-label="1°ordine "><?= e($row['ordine1visita']) ?></td>
                        <td class="green" data-label="1°consegna"><?= e($row['1visita']) ?></td>
                        <td class="orange" data-label="2°ordine"><?= e($row['ordine2visita']) ?></td>
                        <td class="orange" data-label="2°consegna"><?= e($row['2visita']) ?></td>
                        <td class="red" data-label="3°ordine"><?= e($row['ordine3visita']) ?></td>
                        <td class="red" data-label="3°consegna"><?= e($row['3visita']) ?></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>

        </table>
    </div>

</div>

<?php include('layout/footer.php'); ?>
<script src="scripts/script.js"></script>
<script src="scripts/slider.js"></script>
<script src="scripts/warning.js"></script>
<script src="scripts/updwidgetclock.js"></script>
</body>
</html>
