<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
 * IP visit log admin screen (grouped per IP, last 24h).
 *
 * @package    Eurostocks
 * @subpackage Eurostocks/admin/partials
 */

$page_type_labels = Eurostocks_Anti_Spam_Settings::get_default_page_types();
?>

<div class="eurostocks-wrap">
  <h2><?php esc_html_e('IP visit log', 'eurostocks'); ?></h2>

  <?php if (empty($results)) : ?>
    <p><?php esc_html_e('No IP visits recorded in the last 24 hours.', 'eurostocks'); ?></p>
  <?php else : ?>
    <div class="tablenav top">
      <div class="tablenav-pages">
        <span class="displaying-num"><?php echo esc_html(count($results)); ?> <?php esc_html_e('results', 'eurostocks'); ?></span>
      </div>
    </div>

    <table class="wp-list-table widefat fixed striped items">
      <thead>
        <tr>
          <th scope="col" class="manage-column sortable desc column-primary column-url"><a href="#"><span><?php esc_html_e('IP', 'eurostocks'); ?></span><span class="sorting-indicator"></span></a></th>
          <th scope="col" class="manage-column column-int"><?php esc_html_e('Visits (24h)', 'eurostocks'); ?></th>
          <th scope="col" class="manage-column column-int"><?php esc_html_e('Allowed (24h)', 'eurostocks'); ?></th>
          <th scope="col" class="manage-column column-int"><?php esc_html_e('Blocked (24h)', 'eurostocks'); ?></th>
          <th scope="col" class="manage-column column-int"><?php esc_html_e('Detail / search ratio', 'eurostocks'); ?></th>
          <th scope="col" class="manage-column column-int"><?php esc_html_e('Page types', 'eurostocks'); ?></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach($results as $result): ?>
          <tr class="result-row">
            <td class="column-primary column-url has-row-actions">
              <div class="redirect-column-wrap">
                <div class="redirect-source__details">
                  <p><span class=""><?php echo esc_html($result['remote_addr']); ?></span></p>
                  <div class="row-actions">
                    <a data-id="<?php echo esc_html($result['remote_addr']); ?>" data-action="block" class="block_ip" style="color:red;" href="#">
                      <?php esc_html_e('Block IP', 'eurostocks'); ?>
                    </a>
                  </div>
                </div>
              </div>
            </td>
            <td class="column-code"><?php echo esc_html($result['total']); ?></td>
            <td class="column-code"><?php echo isset($result['allowed_total']) ? esc_html((int) $result['allowed_total']) : '0'; ?></td>
            <td class="column-code"><?php echo isset($result['blocked_total']) ? esc_html((int) $result['blocked_total']) : '0'; ?></td>
            <td class="column-code">
              <?php
                $detail_requests = isset($result['detail_requests']) ? (int) $result['detail_requests'] : 0;
                $search_requests = isset($result['search_requests']) ? (int) $result['search_requests'] : 0;
                $ratio_value = isset($result['ratio']) ? (float) $result['ratio'] : 0;
                if ($detail_requests > 0 || $search_requests > 0) {
                  $rounded_ratio = round($ratio_value, 1);
                  printf(
                    '%s%%<br><small>%s %d / %s %d</small>',
                    esc_html($rounded_ratio),
                    esc_html__('Detail', 'eurostocks'),
                    $detail_requests,
                    esc_html__('Search', 'eurostocks'),
                    $search_requests
                  );
                } else {
                  echo '&mdash;';
                }
              ?>
            </td>
            <td class="column-code">
              <?php if (!empty($result['page_types'])) : ?>
                <ul style="margin:0; padding-left:16px;">
                  <?php foreach ($result['page_types'] as $page_type => $visits) :
                    $allowed_visits = isset($visits['allowed']) ? (int) $visits['allowed'] : 0;
                    $blocked_visits = isset($visits['blocked']) ? (int) $visits['blocked'] : 0;
                    $total_visits = isset($visits['total']) ? (int) $visits['total'] : $allowed_visits + $blocked_visits;
                    $page_label = isset($page_type_labels[$page_type]) ? $page_type_labels[$page_type] : $page_type;
                    ?>
                    <li>
                      <strong><?php echo esc_html($page_label); ?>:</strong>
                      <?php echo esc_html($total_visits); ?>
                      <small><?php printf(esc_html__('Allowed: %1$d / Blocked: %2$d', 'eurostocks'), $allowed_visits, $blocked_visits); ?></small>
                    </li>
                  <?php endforeach; ?>
                </ul>
              <?php else : ?>
                &mdash;
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach ?>
      </tbody>
    </table>
  <?php endif; ?>
</div>
