<?php
namespace myclass;


class Pages
{
    public $PageNo;     // 当前页
    public $PageSize;   // 每页多少数据
    public $TotalPage;  // 总共多少页
    public $TotalCount; // 总共多少条数据
    public $FirstPage;  // 第一页
    public $LastPage;   // 最后一页
    public $Url;        // 链接

    public function __construct($PageNo, $PageSize, $TotalCount, $Url)
    {
        $this->PageNo = $PageNo;
        $this->PageSize = $PageSize;
        $this->TotalCount = $TotalCount;
        $this->Url = $Url;
    }

    // 计算总页数
    private function getPageCount()
    {
        if ($this->TotalCount == 0) {
            $this->TotalPage = 1;
        } else {
            $this->TotalPage = ($this->TotalCount + $this->PageSize - 1) / $this->PageSize;
            $this->TotalPage = intval($this->TotalPage);
        }
        $this->LastPage = $this->TotalPage;
        $this->FirstPage = 1;
        $this->execUrl();
    }

    // 格式化URL地址
    private function execUrl()
    {
        if (strpos($this->Url, '?') !== false) {
            $this->Url = $this->Url . '&page=';
        } else {
            $this->Url = $this->Url . '?page=';
        }
    }

    // 获取URL
    private function getUrl($page)
    {
        return sprintf("%s%d", $this->Url, $page);
    }

    // 获取页码范围
    private function getNumRange()
    {
        $maxVisiblePages = 3; // 最多显示的页码数

        if ($this->TotalPage <= $maxVisiblePages) {
            $k = 1;
            $v = $this->TotalPage;
        } else {
            $k = $this->PageNo - intval(($maxVisiblePages-1)/2);
            $v = $this->PageNo + intval($maxVisiblePages/2);

            if ($k < 1) {
                $k = 1;
                $v = $maxVisiblePages;
            } else if ($v > $this->TotalPage) {
                $k = $this->TotalPage - $maxVisiblePages + 1;
                $v = $this->TotalPage;
            }
        }

        return array($k, $v);
    }

    // 获取分页起始位置
    public function LimitStart()
    {
        return ($this->PageNo - 1) * $this->PageSize;
    }

    // 生成响应式分页HTML
public function Show()
{
    $this->getPageCount();

    $style = '
        <link href="/static/css/font-awesome.css" rel="stylesheet">
        <style>
            /* 分页容器样式 */
            .pagination-container {
                display: flex;
                justify-content: center;
                margin: 1.5rem 0 1rem;
            }
            
            /* 分页导航样式 */
            .pagination {
                display: inline-flex;
                border-radius: 0.375rem;
                box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
                padding: 1rem;
                background-color: white;
                border: 1px solid #e5e7eb;
                font-size: 1.25rem; /* 字体大小调整为20px (1.25rem) */
            }
            
            /* 分页项通用样式 */
            .pagination-item {
                padding: 0.375rem 0.75rem;
                border-radius: 0.375rem;
                border: 1px solid #d1d5db;
                color: #4b5563;
                text-decoration: none;
                transition: all 0.2s;
                margin-left: 0.25rem;
            }
            
            /* 分页项悬停效果 */
            .pagination-item:hover:not(.pagination-item-disabled) {
                background-color: #f3f4f6;
            }
            
            /* 当前激活页样式 */
            .pagination-active {
                background-color: #3b82f6; /* 主色调蓝色 */
                color: white;
                border-color: #3b82f6;
            }
            
            /* 禁用状态样式 */
            .pagination-item-disabled {
                opacity: 0.5;
                cursor: not-allowed;
            }
            
            /* 禁用状态悬停效果 */
            .pagination-item-disabled:hover {
                background-color: transparent;
            }
            
            /* 屏幕阅读器隐藏文本 */
            .sr-only {
                position: absolute;
                width: 1px;
                height: 1px;
                padding: 0;
                margin: -1px;
                overflow: hidden;
                clip: rect(0, 0, 0, 0);
                white-space: nowrap;
                border-width: 0;
            }
        </style>
    ';

    $buf = '
        <div class="pagination-container">
            <nav class="pagination" aria-label="Pagination">
    ';

    // 首页和上一页
    if ($this->PageNo > 1) {
        $buf .= sprintf('
            <a href="%s" class="pagination-item">
                <span class="sr-only">首页</span>
                <i class="fa fa-angle-double-left"></i>
            </a>
            <a href="%s" class="pagination-item">
                <span class="sr-only">上一页</span>
                <i class="fa fa-angle-left"></i>
            </a>
        ', $this->getUrl(1), $this->getUrl($this->PageNo-1));
    } else {
        $buf .= '
            <span class="pagination-item pagination-item-disabled">
                <span class="sr-only">首页</span>
                <i class="fa fa-angle-double-left"></i>
            </span>
            <span class="pagination-item pagination-item-disabled">
                <span class="sr-only">上一页</span>
                <i class="fa fa-angle-left"></i>
            </span>
        ';
    }

    // 页码
    list($k, $v) = $this->getNumRange();

    // 中间页码
    for ($i = $k; $i <= $v; $i++) {
        if ($i == $this->PageNo) {
            $buf .= sprintf('
                <span class="pagination-item pagination-active">
                    %s
                </span>
            ', sprintf("%d", $i));
        } else {
            $buf .= sprintf('
                <a href="%s" class="pagination-item">
                    %s
                </a>
            ', $this->getUrl($i), sprintf("%d", $i));
        }
    }

    // 下一页和尾页
    if ($this->PageNo < $this->TotalPage) {
        $buf .= sprintf('
            <a href="%s" class="pagination-item">
                <span class="sr-only">下一页</span>
                <i class="fa fa-angle-right"></i>
            </a>
            <a href="%s" class="pagination-item">
                <span class="sr-only">尾页</span>
                <i class="fa fa-angle-double-right"></i>
            </a>
        ', $this->getUrl($this->PageNo+1), $this->getUrl($this->TotalPage));
    } else {
        $buf .= '
            <span class="pagination-item pagination-item-disabled">
                <span class="sr-only">下一页</span>
                <i class="fa fa-angle-right"></i>
            </span>
            <span class="pagination-item pagination-item-disabled">
                <span class="sr-only">尾页</span>
                <i class="fa fa-angle-double-right"></i>
            </span>
        ';
    }

    $buf .= '
            </nav>
        </div>
    ';

    if ($this->TotalPage <= 1) {
        return "";
    }

    return $style . $buf;
}
}

//$pages = new \myclass\Pages($_GET["page"],$pageSise,$lenArr,'?page={page}');
 $pages->Show()