<div class="wrap menu-settings tools">
<hr class="wp-header-end" />

    <!-- intestazione -->
    <?php include 'intestazione.php';?>

    <div class="postbox tools">
        <div class="inside">
            <div>
                <div class="upload-form" data-file-size="3145728" data-file-type="settings" class="wrap">
                    <div class="iupload-form" data-file-size="3145728" data-file-type="settings" class="wrap">
                        <h3>Elenco Cron Log</h3>
                        Elenco delle operazioni registrate.

                        <table class="wp-list-table widefat fixed striped table-view-list">                 
                        <thead>
                        <tr>
                            <th scope="col" class="manage-column column-name" width="50"><strong>ID</strong></th>
                        	<th scope="col" class="manage-column column-name" width="150"><strong>DATA ORA</strong></th>
                        	<th scope="col" class="manage-column column-name" width="200"><strong>OPERATORE</strong></th>	
                        	<th scope="col" class="manage-column column-name"><strong>OPERAZIONE</strong></th>
                        </tr>
                        </thead>
                        
                        <tbody id="the-list" >
    						<tr>
    							<td colspan="4" style="padding-left:0px; padding-right:0px;">
    								<div style="overflow-y:scroll;">
    									<table class="wp-list-table widefat fixed striped table-view-list" style="border:0px;">      											
                                        	<?php    
                                                // elenco delle operazioni log
                                                global $wpdb;

                                                $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_log';
                                                $ricercaQuery = "SELECT * FROM $table_name WHERE 1 ORDER BY `data_ora` DESC LIMIT %d OFFSET %d";

                                                // paginazione
                                                $pagina_corrente = isset($_GET['pagina']) ? max(1, intval($_GET['pagina'])) : 1;
                                                $record_per_pagina = 20;
                                                $offset = ($pagina_corrente - 1) * $record_per_pagina;
                                            
                                                $results = $wpdb->get_results($wpdb->prepare(
                                                    $ricercaQuery,
                                                    $record_per_pagina,
                                                    $offset
                                                ));
                                            
                                                // paginazione
                                                $totale_record = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
                                                $totale_pagine = ceil($totale_record / $record_per_pagina);
                                                
                                                if ($results == null) { 
                                            ?>       
                                                <tr> 
                                                    <td class="column-primary"  colspan="4">
                                                    Nessuna operazione registrata.
                                                    </td>                                                   
                                                </tr>
                                      		<?php 
                                                } else {     
                                                    foreach ($results as $result) {
                                            ?>
                                        		<tr>
                                                    <td class="column-primary" width="50"><?php echo $result->id;?></td>
                                                    <td class="column-primary" width="150"><?php echo date_format(date_create($result->data_ora),'d-m-Y H:i:s');?></td>
                                      		        <td class="column-primary" width="200"><?php echo $result->operatore;?></td>
                                                    <td class="column-primary"><?php echo $result->operazione;?></td> 
                                                </tr>
                                            <?php          
                                                    }//foreach ($results as $result)
                                                }//if (mysqli_num_rows($results) != 0)
                                            ?>                                            
        								</table>
    								</div>
    							</td>
							</tr>
                        </tbody>
                        </table>

                        <!-- Paginazione -->
                        <div class="bottom" style="padding-top: 10px;">
                            <div class="tablenav-pages">
                                <div style="display: flex; justify-content: space-between; align-items: center;">
                                    <span class="displaying-num">Visualizzato da <?php echo $offset + 1; ?> a <?php echo min($offset + $record_per_pagina, $totale_record); ?> di <?php echo $totale_record; ?></span>
                                    <div class="pagination-links">
                                        <?php if ($pagina_corrente > 1) : ?>
                                            <a href="<?php echo RINNOVOBOOKINGSTAGING ?>/wp-admin/admin.php?page=<?php echo RINNOVOBOOKINGPREFIX ?>log&pagina=<?php echo $pagina_corrente - 1; ?>" class="prev-page button">Precedente</a>
                                        <?php endif; ?>
                                        
                                        <?php
                                        $start_page = max(1, $pagina_corrente - 2);
                                        $end_page = min($totale_pagine, $start_page + 4);
                                        $start_page = max(1, $end_page - 4);

                                        for ($i = $start_page; $i <= $end_page; $i++) {
                                            $class = ($i == $pagina_corrente) ? 'button current' : 'button';
                                        ?>
                                            <a href="<?php echo RINNOVOBOOKINGSTAGING ?>/wp-admin/admin.php?page=<?php echo RINNOVOBOOKINGPREFIX ?>log&pagina=<?php echo $i; ?>" class="<?php echo $class; ?>"><?php echo $i; ?></a>
                                        <?php } ?>
                                        
                                        <?php if ($pagina_corrente < $totale_pagine) { ?>
                                            <a href="<?php echo RINNOVOBOOKINGSTAGING ?>/wp-admin/admin.php?page=<?php echo RINNOVOBOOKINGPREFIX ?>log&pagina=<?php echo $pagina_corrente + 1; ?>" class="next-page button">Successiva</a>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- Paginazione -->
                            
                    </div>
                </div>
            </div>
        </div>  
    </div>
</div>