<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly 

use iwbvel\classes\helpers\Sanitizer;

if (!empty($history_count)) {
    $iwbvel_per_page = (!empty($per_page)) ? $per_page : 10;
    $iwbvel_current_page = (!empty($current_page)) ? $current_page : 1;
    $iwbvel_max_num_pages = ($history_count > $iwbvel_per_page) ? ceil($history_count / $iwbvel_per_page) : 1;
    $iwbvel_prev = max(1, $iwbvel_current_page - 1);
    $iwbvel_next = min($iwbvel_max_num_pages, $iwbvel_current_page + 1);
    $iwbvel_max_display = 3;

    $iwbvel_pagination = '<div style="float: right;">';
    if (isset($iwbvel_max_num_pages)) {
        $iwbvel_pagination .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_prev) . "' class='iwbvel-history-pagination-item'><</a>";
        if ($iwbvel_current_page < $iwbvel_max_display) {
            for ($iwbvel_i = 1; $iwbvel_i <= min($iwbvel_max_display, $iwbvel_max_num_pages); $iwbvel_i++) {
                $iwbvel_current = ($iwbvel_i == $iwbvel_current_page) ? 'current' : '';
                $iwbvel_pagination .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_i) . "' class='iwbvel-history-pagination-item " . esc_attr($iwbvel_current) . "'>" . esc_html($iwbvel_i) . "</a>";
            }
            if ($iwbvel_max_num_pages > $iwbvel_max_display) {
                $iwbvel_pagination .= "<span>...</span>";
                $iwbvel_pagination .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_max_num_pages) . "' class='iwbvel-history-pagination-item'>" . esc_html($iwbvel_max_num_pages) . "</a>";
            }
        } elseif ($iwbvel_current_page == $iwbvel_max_display) {
            $iwbvel_max_num = ($iwbvel_max_display < $iwbvel_max_num_pages) ? $iwbvel_max_display + 1 : $iwbvel_max_display;
            for ($iwbvel_i = 1; $iwbvel_i <= $iwbvel_max_num; $iwbvel_i++) {
                $iwbvel_current = ($iwbvel_i == $iwbvel_current_page) ? 'current' : '';
                $iwbvel_pagination .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_i) . "' class='iwbvel-history-pagination-item " . esc_attr($iwbvel_current) . "'>" . esc_html($iwbvel_i) . "</a>";
            }
            if ($iwbvel_max_num_pages > $iwbvel_current_page) {
                $iwbvel_pagination .= "<span>...</span>";
                $iwbvel_pagination .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_max_num_pages) . "' class='iwbvel-history-pagination-item'>" . esc_html($iwbvel_max_num_pages) . "</a>";
            }
        } else {
            $iwbvel_pagination .= "<a href='javascript:;' data-index='1' class='iwbvel-history-pagination-item'>1</a>";
            $iwbvel_pagination .= "<span>...</span>";
            for ($iwbvel_i = $iwbvel_current_page - 2; $iwbvel_i <= min($iwbvel_current_page + 2, $iwbvel_max_num_pages); $iwbvel_i++) {
                $iwbvel_current = ($iwbvel_i == $iwbvel_current_page) ? 'current' : '';
                $iwbvel_pagination .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_i) . "' class='iwbvel-history-pagination-item " . esc_attr($iwbvel_current) . "'>" . esc_html($iwbvel_i) . "</a>";
            }
            if ($iwbvel_current_page + 2 < $iwbvel_max_num_pages) {
                $iwbvel_pagination .= "<span>...</span>";
                $iwbvel_pagination .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_max_num_pages) . "' class='iwbvel-history-pagination-item'>" . esc_html($iwbvel_max_num_pages) . "</a>";
            }
        }
        $iwbvel_pagination .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_next) . "' class='iwbvel-history-pagination-item'>></a>";
    }
    $iwbvel_pagination .= "</div>";
    $iwbvel_pagination .= "<div class='iwbvel-history-pagination-loading'><img src=" . esc_url(IWBVEL_IMAGES_URL . 'loading-2.gif') . " width='20' height='20'></div>";

    if (!empty($iwbvel_pagination)) {
        echo wp_kses($iwbvel_pagination, Sanitizer::allowed_html());
    }
}
