<?php

namespace GriffinForms\Admin\Html\Pages\Lists;

trait ListPagination
{   
    protected $called = 0;
    
    protected function listPagination()
    {
        $one_page = '';
        
        if ($this->total_pages == 1) {
            $one_page = ' one-page';
            // WP uses this class to hide paginations when results can be displayed on single page.
        }
        
        $this->called++;
        echo '<div class="tablenav-pages' . esc_attr($one_page) . '">';
        $this->itemCount();
        $this->paginationLinks();
        echo '</div>';
    }
    
    protected function paginationLinks()
    {
        echo '<span class="pagination-links">';
        $this->firstPageBtn();
        $this->prevPageBtn(); 
        $this->pagingInput();
        $this->nextPageBtn();
        $this->lastPageBtn();
        echo '</span>';
    }
    
    protected function itemCount()
    {
        echo '<span class="displaying-num">' . esc_html($this->lang->itemCountPagination($this->result_count)) . '</span>';
    }
    
    protected function firstPageBtn()
    {
        if ($this->paged < 2) {
            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">«</span>';
        } else {
            echo '<a class="first-page button" href="' . esc_url($this->pagedUrl(1)) . '">';
            echo '<span class="screen-reader-text">First page</span><span aria-hidden="true">«</span></a>';
        }
        echo ' ';
    }
    
    protected function prevPageBtn()
    {
        if ($this->paged < 2) {
            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">‹</span>';
        } else {
            $url = $this->pagedUrl($this->paged - 1);
            echo '<a class="prev-page button" href="' . esc_url($url) . '">';
            echo '<span class="screen-reader-text">Previous page</span><span aria-hidden="true">‹</span></a>';
        }
        echo ' ';
    }
    
    protected function pagingInput()
    {
        if ($this->called === 2) {
            echo '<span id="table-paging" class="paging-input">';
            echo '<span class="tablenav-paging-text">';
            echo absint($this->paged);
            echo ' of <span class="total-pages">';
            echo absint($this->total_pages);
            echo ' </span></span></span>';
        } else {
            echo '<span class="paging-input"><label for="current-page-selector" class="screen-reader-text">Current Page</label>';
            echo '<input class="current-page" id="current-page-selector" type="text" name="paged" value="' . absint($this->paged) . '" size="' . esc_attr(strlen($this->paged)) . '" aria-describedby="table-paging"><span class="tablenav-paging-text"> of <span class="total-pages">' . absint($this->total_pages) . '</span></span></span>';
            echo ' ';
        }
    }
    
    
    protected function nextPageBtn()
    {
        if ($this->total_pages > $this->paged) {
            $url = $this->pagedUrl($this->paged + 1);
            echo '<a class="next-page button" href="' . esc_url($url) . '"><span class="screen-reader-text">Next page</span>';
            echo '<span aria-hidden="true">›</span></a> ';
        } else {
            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">›</span>';
        }
        echo ' ';
    }
    
    protected function lastPageBtn()
    {
        if (($this->total_pages - $this->paged) > 1) {
            $url = $this->pagedUrl($this->total_pages);
            echo '<a class="last-page button" href="' . esc_url($url) . '">';
            echo '<span class="screen-reader-text">Last page</span><span aria-hidden="true">»</span></a>';
        } else {
            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">»</span>';
        }
        echo ' ';
    }
    
    protected function pagedUrl($page_no)
    {
        $page_no = absint($page_no); // Sanitize the page number
        $url_attrs = array_map('sanitize_text_field', $_GET); // Sanitize other $_GET values
        $url_attrs['paged'] = $page_no; // Add the 'paged' parameter
        $url = get_admin_url() . 'admin.php?' . http_build_query($url_attrs);
        return $url;
    }
}