<?php

use Easymailing\App\Core\Provider\Woocommerce\Model\LogType;

$this->include("Admin/Partials/synchronization_header.php")

?>
<div class="wrap em-synchronization-page">
    <div class="em-d-flex">
        <div class="">
            <div class="em-fw-bold em-fs-5"><?php echo __('Logs', 'easymailing') ?></div>
            <div class="em-mb-4">
                <p><?php echo __('Activa los logs para visualizar y depurar los registros de sincronización, hooks y llamadas al API entre tu tienda y Easymailing', 'easymailing') ?></p>
            </div>
			<?php if ($synchronizationConfig->isEnableLogs()) { ?>
                <div>
                    <div class="em-d-flex em-justify-content-between">
                        <div class="em-d-flex">
                            <form action="" method="post" id="easymailing_log_form" class="em-mr-2">
                                <input type="hidden" name="action" value="easymailing_log_action">
                                <div class="mt-4">
                                    <button type="submit" class="em-btn em-btn-small em-btn-primary"><?php echo __('Desactivar logs', 'easymailing') ?></button>
                                </div>
                            </form>
                            <form action="" method="post">
                                <input type="hidden" name="action" value="delete_logs">
                                <button type="submit" class="em-btn em-btn-small  em-btn-danger">
			                        <?php echo __('Eliminar todos los logs', 'easymailing'); ?>
                                </button>
                            </form>
                        </div>

                        <form action="" method="post">
                            <input type="hidden" name="action" value="download_logs">
                            <button type="submit" class="em-btn em-btn-small  em-btn-info">
			                    <?php echo __('Descargar logs', 'easymailing'); ?>
                            </button>
                        </form>

                    </div>
                    <div class="mt-4">
                        <!-- Tabla con los logs -->
                        <table class="widefat fixed striped logs-table">
                            <thead>
                            <tr>
                                <th class="log-date"><?php echo __('Fecha', 'easymailing'); ?></th>
                                <th class="log-type"><?php echo __('Tipo', 'easymailing'); ?></th>
                                <th class="log-status"><?php echo __('Estado', 'easymailing'); ?></th>
                                <th class="log-url"><?php echo __('URL', 'easymailing'); ?></th>
                                <th class="log-message"><?php echo __('Mensaje', 'easymailing'); ?></th>
                            </tr>
                            </thead>
                            <tbody>
		                    <?php if (!empty($logs)): ?>
			                    <?php foreach ($logs as $log): ?>
                                    <tr>
                                        <td class="log-date"><?php echo esc_html($log['date']); ?></td>
                                        <td class="log-type">
                                            <span class="em-badge em-uppercase em-badge-<?php echo LogType::getClass($log['type']) ?>">
                                                 <?php echo esc_html($log['type']); ?>
                                            </span>
                                        </td>
                                        <td class="log-status">
                                            <?php if ($log['status']){ ?>
	                                            <?php
                                                $statusColor = 'info';
                                                if ($log['status'] >= 200 and $log['status'] <= 300){
	                                                $statusColor =  "success";
                                                }else if($log['status'] > 300 and $log['status'] <= 422 ){
	                                                $statusColor =  "warning";
                                                }else if($log['status'] >= 500 ){
	                                                $statusColor =  "danger";
                                                }
                                                ?>
                                            <span class="em-badge em-badge-<?php echo $statusColor ?>">
                                                 <?php echo esc_html($log['status']); ?>
                                            </span>
                                            <?php } ?>
                                        </td>
                                        <td class="log-url"><?php echo esc_html($log['url']); ?></td>
                                        <td class="log-message"><?php echo $log['message']; ?></td>
                                    </tr>
			                    <?php endforeach; ?>
		                    <?php else: ?>
                                <tr>
                                    <td colspan="5"><?php echo __('No hay logs para mostrar.', 'easymailing'); ?></td>
                                </tr>
		                    <?php endif; ?>
                            </tbody>
                        </table>

                        <!-- Paginación -->
                        <div class="tablenav">
                            <div class="tablenav-pages">
			                    <?php
			                    $total_pages = (int) ceil($total_logs / $logs_per_page); // Total de páginas
			                    $base_url = remove_query_arg('paged'); // Quitamos 'paged' de la URL base
			                    $current = max(1, $paged); // Obtenemos página actual o asumimos 1

			                    // Generamos los enlaces con paginate_links
			                    $pagination_links = paginate_links([
				                    'base'      => add_query_arg('paged', '%#%', $base_url), // Construcción URL por páginas
				                    'format'    => '',
				                    'current'   => $current,
				                    'total'     => $total_pages,
				                    'type'      => 'array', // Tipo array, para personalización de diseño
				                    'prev_text' => '&laquo; ' . __('Anterior', 'easymailing'),
				                    'next_text' => __('Siguiente', 'easymailing') . ' &raquo;',
				                    'mid_size'  => 2, // Tamaño de las páginas visibles en el rango
				                    'end_size'  => 1, // Tamaño inicial/final
			                    ]);

			                    // Si hay enlaces disponibles, renderizamos la paginación
			                    if (!empty($pagination_links)) {
				                    echo '<span class="displaying-num">' . sprintf(__('Mostrando %1$d registros de %2$d', 'easymailing'), count($logs), $total_logs) . '</span>';
				                    echo '<ul class="pagination-links">';

				                    foreach ($pagination_links as $link) {
					                    echo "<li>$link</li>"; // Marcup estándar
				                    }

				                    echo '</ul>';
			                    }
			                    ?>
                            </div>
                        </div>
                    </div>
                </div>
			<?php } else { ?>
                <form action="" method="post" id="easymailing_log_form">
                    <input type="hidden" name="action" value="easymailing_log_action">
                    <div class="em-alert em-alert-warning ">  <?php echo __('Los logs están desactivados') ?></div>
                    <div class="mt-4">
                        <button type="submit" class="em-submit-button"><?php echo __('Activar logs', 'easymailing') ?></button>
                    </div>
                </form>
			<?php } ?>
        </div>
    </div>
</div>

