<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Provide a admin area view for the plugin
*
* This file is used to markup the admin-facing aspects of the plugin.
*
* @link       https://www.eurostocks.com
* @since      1.0.0
*
* @package    Eurostocks
* @subpackage Eurostocks/admin/partials
*/

$europeanCountries = array(
  "al" => __("Albania"),
  "ad" => __("Andorra"),
  "at" => __("Austria"),
  "by" => __("Belarus"),
  "be" => __("Belgium"),
  "ba" => __("Bosnia and Herzegovina"),
  "bg" => __("Bulgaria"),
  "hr" => __("Croatia"),
  "cy" => __("Cyprus"),
  "cz" => __("Czech Republic"),
  "dk" => __("Denmark"),
  "ee" => __("Estonia"),
  "fi" => __("Finland"),
  "fr" => __("France"),
  "de" => __("Germany"),
  "gr" => __("Greece"),
  "hu" => __("Hungary"),
  "is" => __("Iceland"),
  "ie" => __("Ireland"),
  "it" => __("Italy"),
  "xk" => __("Kosovo"),
  "lv" => __("Latvia"),
  "li" => __("Liechtenstein"),
  "lt" => __("Lithuania"),
  "lu" => __("Luxembourg"),
  "mt" => __("Malta"),
  "md" => __("Moldova"),
  "mc" => __("Monaco"),
  "me" => __("Montenegro"),
  "nl" => __("Netherlands"),
  "mk" => __("Northern Macedonia"),
  "no" => __("Norway"),
  "pl" => __("Poland"),
  "pt" => __("Portugal"),
  "ro" => __("Romania"),
  "ru" => __("Russia"),
  "sm" => __("San Marino"),
  "rs" => __("Serbia"),
  "sk" => __("Slovakia"),
  "si" => __("Slovenia"),
  "es" => __("Spain"),
  "se" => __("Sweden"),
  "ch" => __("Switzerland"),
  "tr" => __("Turkeye"),
  "ua" => __("Ukraine"),
  "gb" => __("United Kingdom"),
  "va" => __("Vatican City"),
);

?>
<div class="eurostocks-wrap">
  <h2><?php echo esc_html(get_admin_page_title()); ?></h2>

  <?php 
  // check if extension bcmath is enabled
  if (!function_exists('bcadd')) {
    echo '<div class="notice notice-error"><p>';
    esc_html_e('The bcmath extension is not enabled. The IP blocking functionality requires the bcmath extension to work properly.', 'eurostocks');
    echo '</p></div>';
  }

  ?>


  <form method="POST" id="eurostocks_iplist" name="eurostocks_iplist" action="options.php">
    <?php
    settings_fields('eurostocks-anti-spam');
    do_settings_sections('eurostocks-anti-spam');
    wp_nonce_field( 'eurostocks-anti-spam', 'eurostocks-anti-spam_nonce' );

    $options = get_option('eurostocks-anti-spam');

    if (!isset($options['iplist'])){
      $iplist = '';
    }else{
      $iplist = $options['iplist'];
    }
    if (!isset($options['disable_ipfilter'])){
      $disable_ipfilter = 0;
    }else{
      $disable_ipfilter = $options['disable_ipfilter'];
    }

    if (!isset($options['block_unknown'])){
      $block_unknown = 0;
    }else{
      $block_unknown = $options['block_unknown'];
    }

    if (!isset($options['use_cloudflare_ip'])){
      $use_cloudflare_ip = 0;
    }else{
      $use_cloudflare_ip = (int) $options['use_cloudflare_ip'];
    }

    if (!isset($options['allow_countries'])){
      $allow_countries = array();
    }else{
      $allow_countries = $options['allow_countries'];
    }

    if (!isset($options['autoban'])){
      $autoban = 100;
    }else{
      $autoban = $options['autoban'];
    }

    $ratio_threshold = Eurostocks_Anti_Spam_Settings::get_ratio_min_detail_requests($options);
    $ratio_percent_limit = Eurostocks_Anti_Spam_Settings::get_ratio_percent_limit($options);

    $page_type_limits = isset($options['per_page_limits']) && is_array($options['per_page_limits']) ? $options['per_page_limits'] : array();
    $default_page_types = Eurostocks_Anti_Spam_Settings::get_default_page_types();
    $page_type_rows = array();

    foreach ($default_page_types as $slug => $label) {
      $default_autoban = Eurostocks_Anti_Spam_Settings::get_page_type_limit($slug, 'autoban', $options);
      $default_email_limit = Eurostocks_Anti_Spam_Settings::get_page_type_limit($slug, 'email_verification_limit', $options);

      $page_type_rows[$slug] = array(
        'label'      => $label,
        'is_default' => true,
        'slug'       => $slug,
        'autoban'    => isset($page_type_limits[$slug]['autoban']) ? (int) $page_type_limits[$slug]['autoban'] : (int) $default_autoban,
        'email_limit'=> isset($page_type_limits[$slug]['email_verification_limit']) ? (int) $page_type_limits[$slug]['email_verification_limit'] : (int) $default_email_limit,
      );

    }

  
    

    $anti_spam_texts = Eurostocks_Anti_Spam_Settings::get_texts($options);
    $verification_entries = isset($verification_entries) && is_array($verification_entries) ? $verification_entries : array();
    $verification_entries_by_ip = isset($verification_entries_by_ip) && is_array($verification_entries_by_ip) ? $verification_entries_by_ip : array();
    $blocked_ip_rows = isset($blocked_ip_rows) && is_array($blocked_ip_rows) ? $blocked_ip_rows : array();
    $verification_required_rows = isset($verification_required_rows) && is_array($verification_required_rows) ? $verification_required_rows : array();
    $ip_metadata = isset($ip_metadata) && is_array($ip_metadata) ? $ip_metadata : array();
    $global_autoban_placeholder = isset($options['autoban']) ? (int) $options['autoban'] : Eurostocks_Anti_Spam_Settings::DEFAULT_AUTOBAN;
    $global_email_limit_placeholder = Eurostocks_Anti_Spam_Settings::DEFAULT_EMAIL_LIMIT;

    $verification_fields = array(
      'verification_heading'        => array('label' => __('Verification heading', 'eurostocks'), 'type' => 'text'),
      'verification_description'    => array('label' => __('Verification description', 'eurostocks'), 'type' => 'textarea', 'rows' => 3),
      'verification_email_label'    => array('label' => __('Email field label', 'eurostocks'), 'type' => 'text'),
      'verification_button_text'    => array('label' => __('Button text', 'eurostocks'), 'type' => 'text'),
      'verification_helper_text'    => array('label' => __('Helper text', 'eurostocks'), 'type' => 'textarea', 'rows' => 2),
      'verification_notice_success' => array('label' => __('Success notice', 'eurostocks'), 'type' => 'text'),
      'verification_notice_failed'  => array('label' => __('Failed notice', 'eurostocks'), 'type' => 'text'),
    );

    $limit_message_fields = array(
      'limit_verify_message'       => array('label' => __('Message when verification is required', 'eurostocks'), 'type' => 'text'),
      'limit_block_message'        => array('label' => __('Message when requests are blocked', 'eurostocks'), 'type' => 'text'),
      'limit_generic_message'      => array('label' => __('Generic limit fallback message', 'eurostocks'), 'type' => 'text'),
      'pdf_limit_verify_message'   => array('label' => __('PDF download verification message', 'eurostocks'), 'type' => 'text'),
      'pdf_limit_block_message'    => array('label' => __('PDF download blocked message', 'eurostocks'), 'type' => 'text'),
      'ajax_success_message'       => array('label' => __('Verification request success message', 'eurostocks'), 'type' => 'text'),
      'ajax_invalid_email_message' => array('label' => __('Invalid email message', 'eurostocks'), 'type' => 'text'),
      'ajax_disposable_email_message' => array('label' => __('Disposable email message', 'eurostocks'), 'type' => 'text'),
      'ajax_missing_ip_message'    => array('label' => __('Missing IP message', 'eurostocks'), 'type' => 'text'),
      'ajax_send_error_message'    => array('label' => __('Email send failure message', 'eurostocks'), 'type' => 'text'),
      'ajax_generic_error_message' => array('label' => __('Generic error message', 'eurostocks'), 'type' => 'text'),
      'form_security_error_message'=> array('label' => __('Security validation message', 'eurostocks'), 'type' => 'text'),
    );

    $email_fields = array(
      'email_subject' => array('label' => __('Email subject', 'eurostocks'), 'type' => 'text'),
      'email_body'    => array(
        'label' => __('Email body', 'eurostocks'),
        'type'  => 'textarea',
        'rows'  => 6,
        'description' => __('Available placeholders: {verification_link}, {site_name}', 'eurostocks'),
      ),
    );

    ?>

    <p>
      <?php esc_html_e('Disable IP blocker?', 'eurostocks'); ?><br>
      <span for="eurostocks-disable-ipfilter">
        <input id="eurostocks-disable-ipfilter" value="1" type="checkbox" name="eurostocks-anti-spam[disable_ipfilter]" <?php checked($disable_ipfilter, 1); ?>> <?php esc_html_e('Disable IP blocker', 'eurostocks'); ?>
      </span>
    </p>

    <p>
      <?php esc_html_e('Block IP address when country of origin unkown', 'eurostocks'); ?><br>
      <span for="eurostocks-block-unknown">
        <input id="eurostocks-block-unknown" value="1" type="checkbox" name="eurostocks-anti-spam[block_unknown]" <?php checked($block_unknown, 1); ?>> <?php esc_html_e('Block IP address when country of origin unkown', 'eurostocks'); ?>
      </span>
    </p>

    <p>
      <?php esc_html_e('Behind Cloudflare?', 'eurostocks'); ?><br>
      <label>
        <input id="eurostocks-use-cloudflare-ip" value="1" type="checkbox" name="eurostocks-anti-spam[use_cloudflare_ip]" <?php checked($use_cloudflare_ip, 1); ?>> <?php esc_html_e('Use the CF-Connecting-IP header when available', 'eurostocks'); ?>
      </label>
    </p>

    <p>
      <?php _e('Accept IP addresses from the following countries. If no countries are selected everything is allowed', 'eurostocks'); ?><br>
      <span for="eurostocks-allow_countries">
        <select id="eurostocks-allow_countries" name="eurostocks-anti-spam[allow_countries][]"  multiple="multiple" class="euroselect" placeholder="<?php _e('Kies een land', 'eurostocks'); ?>">
        <?php foreach($europeanCountries as $key => $country){ ?>
          <option value="<?php echo esc_html($key); ?>" <?php if(in_array($key, $allow_countries)){ echo 'selected'; } ?>><?php echo esc_html($country); ?></option>
          ><?php } ?>
        </select>
      </span>
    </p>


   
   
    <h3><?php esc_html_e('Per-page request limits', 'eurostocks'); ?></h3>
    <p><?php esc_html_e('Configure how many requests are allowed per page type before visitors are blocked or asked to verify their email. Leave the block limit empty to use the global value above; leave the verification limit empty to disable the prompt for that page type.', 'eurostocks'); ?></p>

    <table class="widefat striped fixed eurostocks-anti-spam-limits">
      <thead>
        <tr>
          <th><?php esc_html_e('Page type', 'eurostocks'); ?></th>
          <th><?php esc_html_e('Block after (requests / 24h)', 'eurostocks'); ?></th>
          <th><?php esc_html_e('Require verification after (requests / 24h)', 'eurostocks'); ?></th>
          <th><?php esc_html_e('Actions', 'eurostocks'); ?></th>
        </tr>
      </thead>
      <tbody id="eurostocks-page-type-limit-rows">
        <?php foreach ($page_type_rows as $row_key => $row_data) : ?>
          <tr class="eurostocks-page-type-row" data-row-key="<?php echo esc_attr($row_key); ?>" data-default="<?php echo esc_attr($row_data['is_default'] ? '1' : '0'); ?>">
            <td>
              <?php if ($row_data['is_default']) : ?>
                <strong><?php echo esc_html($row_data['label']); ?></strong><br>
                <?php echo esc_html($row_data['slug']); ?>
                <input type="hidden" name="eurostocks-anti-spam[per_page_limits][<?php echo esc_attr($row_key); ?>][page_type]" value="<?php echo esc_attr($row_data['slug']); ?>">
              <?php else : ?>
                <input type="text" class="regular-text" name="eurostocks-anti-spam[per_page_limits][<?php echo esc_attr($row_key); ?>][page_type]" value="<?php echo esc_attr($row_data['slug']); ?>" placeholder="<?php esc_attr_e('Slug (e.g. vehicle_pdf)', 'eurostocks'); ?>">
              <?php endif; ?>
            </td>
            <td>
              <input type="number" min="0" class="small-text" name="eurostocks-anti-spam[per_page_limits][<?php echo esc_attr($row_key); ?>][autoban]" value="<?php echo esc_attr($row_data['autoban']); ?>" placeholder="<?php echo esc_attr($global_autoban_placeholder); ?>">
            </td>
            <td>
              <input type="number" min="0" class="small-text" name="eurostocks-anti-spam[per_page_limits][<?php echo esc_attr($row_key); ?>][email_limit]" value="<?php echo esc_attr($row_data['email_limit']); ?>" placeholder="<?php echo esc_attr($global_email_limit_placeholder); ?>">
            </td>
            <td>
              <?php if ($row_data['is_default']) : ?>
                <span aria-hidden="true">—</span>
              <?php else : ?>
                <button type="button" class="button-link eurostocks-remove-page-type"><?php esc_html_e('Remove', 'eurostocks'); ?></button>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

    <h3><?php esc_html_e('Page ratio request limits', 'eurostocks'); ?></h3>

    <p>
      <label for="eurostocks-ratio-threshold"><?php esc_html_e('Minimum detail requests before ratio blocking', 'eurostocks'); ?></label><br>
      <input type="number" min="0" class="regular-text input textinput" id="eurostocks-ratio-threshold" name="eurostocks-anti-spam[request_ratio_min_detail]" value="<?php echo esc_attr($ratio_threshold); ?>">
      <span class="description"><?php esc_html_e('Applies the ratio rule once this many detail requests are reached.', 'eurostocks'); ?></span>
    </p>

    <p>
      <label for="eurostocks-ratio-percent"><?php esc_html_e('Block when detail/search ratio exceeds (%)', 'eurostocks'); ?></label><br>
      <input type="number" min="0" class="regular-text input textinput" id="eurostocks-ratio-percent" name="eurostocks-anti-spam[request_ratio_percent]" value="<?php echo esc_attr($ratio_percent_limit); ?>">
      <span class="description"><?php esc_html_e('Example: set to 120 to block when detail requests are 20% higher than searches.', 'eurostocks'); ?></span>
    </p>


    <h3><?php esc_html_e('Anti spam texts', 'eurostocks'); ?></h3>
    <p><?php esc_html_e('Customize the texts shown to visitors during the verification flow. Leave a field empty to use the default translation.', 'eurostocks'); ?></p>

    <h4><?php esc_html_e('Verification screen', 'eurostocks'); ?></h4>
    <table class="form-table eurostocks-anti-spam-texts">
      <tbody>
        <?php foreach ($verification_fields as $field_key => $field_config) :
          $field_id = 'eurostocks-text-' . $field_key;
          $value = isset($anti_spam_texts[$field_key]) ? $anti_spam_texts[$field_key] : '';
          ?>
          <tr>
            <th scope="row">
              <label for="<?php echo esc_attr($field_id); ?>"><?php echo esc_html($field_config['label']); ?></label>
            </th>
            <td>
              <?php if ($field_config['type'] === 'textarea') : ?>
                <textarea class="large-text" rows="<?php echo isset($field_config['rows']) ? (int) $field_config['rows'] : 3; ?>" id="<?php echo esc_attr($field_id); ?>" name="eurostocks-anti-spam[texts][<?php echo esc_attr($field_key); ?>]"><?php echo esc_textarea($value); ?></textarea>
              <?php else : ?>
                <input type="text" class="regular-text" id="<?php echo esc_attr($field_id); ?>" name="eurostocks-anti-spam[texts][<?php echo esc_attr($field_key); ?>]" value="<?php echo esc_attr($value); ?>">
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

    <h4><?php esc_html_e('Limit & notification messages', 'eurostocks'); ?></h4>
    <table class="form-table eurostocks-anti-spam-texts">
      <tbody>
        <?php foreach ($limit_message_fields as $field_key => $field_config) :
          $field_id = 'eurostocks-text-' . $field_key;
          $value = isset($anti_spam_texts[$field_key]) ? $anti_spam_texts[$field_key] : '';
          ?>
          <tr>
            <th scope="row">
              <label for="<?php echo esc_attr($field_id); ?>"><?php echo esc_html($field_config['label']); ?></label>
            </th>
            <td>
              <input type="text" class="regular-text" id="<?php echo esc_attr($field_id); ?>" name="eurostocks-anti-spam[texts][<?php echo esc_attr($field_key); ?>]" value="<?php echo esc_attr($value); ?>">
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

    <h4><?php esc_html_e('Email contents', 'eurostocks'); ?></h4>
    <table class="form-table eurostocks-anti-spam-texts">
      <tbody>
        <?php foreach ($email_fields as $field_key => $field_config) :
          $field_id = 'eurostocks-text-' . $field_key;
          $value = isset($anti_spam_texts[$field_key]) ? $anti_spam_texts[$field_key] : '';
          ?>
          <tr>
            <th scope="row">
              <label for="<?php echo esc_attr($field_id); ?>"><?php echo esc_html($field_config['label']); ?></label>
            </th>
            <td>
              <?php if ($field_config['type'] === 'textarea') : ?>
                <textarea class="large-text code" rows="<?php echo isset($field_config['rows']) ? (int) $field_config['rows'] : 6; ?>" id="<?php echo esc_attr($field_id); ?>" name="eurostocks-anti-spam[texts][<?php echo esc_attr($field_key); ?>]"><?php echo esc_textarea($value); ?></textarea>
              <?php else : ?>
                <input type="text" class="regular-text" id="<?php echo esc_attr($field_id); ?>" name="eurostocks-anti-spam[texts][<?php echo esc_attr($field_key); ?>]" value="<?php echo esc_attr($value); ?>">
              <?php endif; ?>
              <?php if (isset($field_config['description'])) : ?>
                <p class="description"><?php echo esc_html($field_config['description']); ?></p>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

    <h3><?php esc_html_e('IPs requiring verification', 'eurostocks'); ?></h3>
    <?php if (empty($verification_required_rows)) : ?>
      <p><?php esc_html_e('No IPs are currently pending email verification.', 'eurostocks'); ?></p>
    <?php else : ?>
      <?php $verification_required_date_format = get_option('date_format') . ' ' . get_option('time_format'); ?>
      <table class="widefat fixed striped eurostocks-verification-required-table">
        <thead>
          <tr>
            <th><?php esc_html_e('IP address', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Reason', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Page type', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Last updated', 'eurostocks'); ?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($verification_required_rows as $pending_row) :
            $pending_ip = isset($pending_row['ip']) ? $pending_row['ip'] : '';
            $pending_reason = isset($pending_row['reason_label']) ? $pending_row['reason_label'] : Eurostocks_Anti_Spam_Settings::get_reason_label('');
            $pending_page_type = isset($pending_row['page_type']) ? $pending_row['page_type'] : '';
            $pending_updated = isset($pending_row['updated_at']) ? (int) $pending_row['updated_at'] : 0;
            $pending_updated_label = $pending_updated ? date_i18n($verification_required_date_format, $pending_updated) : '';
            ?>
            <tr>
              <td><?php echo '' !== $pending_ip ? esc_html($pending_ip) : '&mdash;'; ?></td>
              <td><?php echo esc_html($pending_reason); ?></td>
              <td><?php echo '' !== $pending_page_type ? esc_html($pending_page_type) : '&mdash;'; ?></td>
              <td><?php echo '' !== $pending_updated_label ? esc_html($pending_updated_label) : '&mdash;'; ?></td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    <?php endif; ?>

    <h3><?php esc_html_e('Active verification entries', 'eurostocks'); ?></h3>
    <?php if (empty($verification_entries)) : ?>
      <p><?php esc_html_e('No active verification entries at the moment.', 'eurostocks'); ?></p>
    <?php else : ?>
      <table class="widefat fixed striped eurostocks-verification-table">
        <thead>
          <tr>
            <th><?php esc_html_e('IP address', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Email', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Status', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Reason', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Page type', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Created / verified', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Actions', 'eurostocks'); ?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($verification_entries as $entry) :
            $entry_ip = isset($entry['ip']) ? $entry['ip'] : '';
            $entry_email = isset($entry['email']) ? $entry['email'] : '';
            $entry_status = isset($entry['status']) ? $entry['status'] : Eurostocks_Email_Verification::STATUS_NONE;
            $entry_page_type = isset($entry['page_type']) ? $entry['page_type'] : '';
            $created_at = isset($entry['created_at']) ? (int) $entry['created_at'] : 0;
            $verified_at = isset($entry['verified_at']) ? (int) $entry['verified_at'] : 0;
            $entry_reason_key = '';
            if (isset($entry['reason_key']) && $entry['reason_key'] !== '') {
              $entry_reason_key = $entry['reason_key'];
            } elseif ('' !== $entry_ip && isset($ip_metadata[$entry_ip]['reason_key'])) {
              $entry_reason_key = $ip_metadata[$entry_ip]['reason_key'];
            }
            $entry_reason_label = Eurostocks_Anti_Spam_Settings::get_reason_label($entry_reason_key);
            $date_format = get_option('date_format') . ' ' . get_option('time_format');
            $created_label = $created_at ? date_i18n($date_format, $created_at) : __('Unknown', 'eurostocks');
            $verified_label = $verified_at ? date_i18n($date_format, $verified_at) : '';
            ?>
            <tr>
              <td><?php echo '' !== $entry_ip ? esc_html($entry_ip) : '&mdash;'; ?></td>
              <td><?php echo '' !== $entry_email ? esc_html($entry_email) : '&mdash;'; ?></td>
              <td><?php echo esc_html(ucfirst($entry_status)); ?></td>
              <td><?php echo esc_html($entry_reason_label); ?></td>
              <td><?php echo '' !== $entry_page_type ? esc_html($entry_page_type) : '&mdash;'; ?></td>
              <td>
                <strong><?php esc_html_e('Created:', 'eurostocks'); ?></strong> <?php echo esc_html($created_label); ?>
                <?php if ($verified_label) : ?>
                  <br><strong><?php esc_html_e('Verified:', 'eurostocks'); ?></strong> <?php echo esc_html($verified_label); ?>
                <?php endif; ?>
              </td>
              <td>
                <?php if ('' !== $entry_ip) : ?>
                  <button type="button" class="button button-small eurostocks-remove-verification" data-hash="<?php echo esc_attr($entry['hash']); ?>" data-ip="<?php echo esc_attr($entry_ip); ?>" data-unblock="1">
                    <?php esc_html_e('Remove & unblock IP', 'eurostocks'); ?>
                  </button>
                <?php endif; ?>
              </td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    <?php endif; ?>

    

    <h3><?php esc_html_e('Blocked IP addresses', 'eurostocks'); ?></h3>
    <?php if (empty($blocked_ip_rows)) : ?>
      <p><?php esc_html_e('There are no blocked IP addresses.', 'eurostocks'); ?></p>
    <?php else : ?>
      <div class="eurostocks-blocked-ip-actions">
        <button type="button" class="button" id="eurostocks-unblock-selected" disabled>
          <?php esc_html_e('Unblock selected', 'eurostocks'); ?>
        </button>
      </div>
      <?php $blocked_date_format = get_option('date_format') . ' ' . get_option('time_format'); ?>
      <table class="widefat fixed striped eurostocks-blocked-ip-table">
        <thead>
          <tr>
            <th class="check-column">
              <input type="checkbox" id="eurostocks-blocked-ip-select-all" aria-label="<?php esc_attr_e('Select all blocked IPs', 'eurostocks'); ?>" />
            </th>
            <th><?php esc_html_e('IP address', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Reason', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Associated email', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Last updated', 'eurostocks'); ?></th>
            <th><?php esc_html_e('Actions', 'eurostocks'); ?></th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($blocked_ip_rows as $blocked_ip_row) :
            $blocked_ip = isset($blocked_ip_row['ip']) ? $blocked_ip_row['ip'] : '';
            $blocked_email = '';
            if ('' !== $blocked_ip && isset($verification_entries_by_ip[$blocked_ip])) {
              foreach ($verification_entries_by_ip[$blocked_ip] as $matching_entry) {
                if (!empty($matching_entry['email'])) {
                  $blocked_email = $matching_entry['email'];
                  break;
                }
              }
            }
            $blocked_reason = isset($blocked_ip_row['reason_label']) ? $blocked_ip_row['reason_label'] : Eurostocks_Anti_Spam_Settings::get_reason_label('');
            $blocked_updated_at = isset($blocked_ip_row['updated_at']) ? (int) $blocked_ip_row['updated_at'] : 0;
            $blocked_updated_label = $blocked_updated_at ? date_i18n($blocked_date_format, $blocked_updated_at) : '';
            ?>
            <tr>
              <th scope="row" class="check-column">
                <input type="checkbox" class="eurostocks-blocked-ip-checkbox" value="<?php echo esc_attr($blocked_ip); ?>" aria-label="<?php echo esc_attr(sprintf(__('Select %s', 'eurostocks'), $blocked_ip)); ?>">
              </th>
              <td><?php echo '' !== $blocked_ip ? esc_html($blocked_ip) : '&mdash;'; ?></td>
              <td><?php echo esc_html($blocked_reason); ?></td>
              <td><?php echo '' !== $blocked_email ? esc_html($blocked_email) : '&mdash;'; ?></td>
              <td><?php echo '' !== $blocked_updated_label ? esc_html($blocked_updated_label) : '&mdash;'; ?></td>
              <td>
                <button type="button" class="button button-small eurostocks-remove-blocked-ip" data-ip="<?php echo esc_attr($blocked_ip); ?>">
                  <?php esc_html_e('Unblock IP', 'eurostocks'); ?>
                </button>
              </td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    <?php endif; ?>

    <br>
     <p>
    <label for="eurostocks-autoban"><?php esc_html_e('Autoban IP after x amount of requests', 'eurostocks'); ?></label>
    <input type="number" class="regular-text input textinput" id="eurostocks-autoban" name="eurostocks-anti-spam[autoban]" value="<?php echo esc_html($autoban); ?>">
    </p>

    <p><?php esc_html_e('Add the IP address to the list below to block it. Using the IP log below, you can also add new IP addresses to the list.', 'eurostocks'); ?></p><br>
    <textarea rows="4" class="regular-text input textinput" type="text" id="eurostocks-iplist" name="eurostocks-anti-spam[iplist]"><?php echo esc_html($iplist); ?></textarea>

    <?php submit_button(__('Save all changes', 'eurostocks'), 'primary','submit', TRUE); ?>

  </form>
</div>
