using System;
using System.Collections.Generic;
using System.Text;

namespace JuSNS.UI.Control
{
    /// <summary>
    /// 分页导航
    /// </summary>
    public class PageNavigator //: System.Web.UI.UserControl
    {
        static public string Pagination(int pageindex, int pagecount, string url)
        {
            if (url.IndexOf("?") >= 0)
                url += "&";
            else
                url += "?";
            int next = 0;
            int pre = 0;
            int startcount = 0;
            int endcount = 0;
            string retstr = "";

            if (pageindex < 1) pageindex = 1;
            if (pageindex > pagecount) pageindex = pagecount;
            next = pageindex + 1;
            pre = pageindex - 1;
            startcount = (pageindex + 5) > pagecount ? pagecount - 9 : pageindex - 4;//中间页起始序号
            //中间页终止序号
            endcount = pageindex < 5 ? 10 : pageindex + 5;
            if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数，设置如果小于1就从序号1开始
            if (pagecount < endcount) { endcount = pagecount; }//页码+5的可能性就会产生最终输出序号大于总页码，那么就要将其控制在页码数之内
            //currentpagestr = "共" + pagecount + "页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

            retstr += pageindex > 1 ? "<a href=\"" + url + "Page=1\" title=\"首页\">|<</a><a href=\"" + url + "Page=" + pre + "\" title=\"上一页\"><</a>" : "";
            //中间页处理，这个增加时间复杂度，减小空间复杂度
            for (int i = startcount; i <= endcount; i++)
            {
                retstr += pageindex == i ? "<a href=\"" + url + "Page=" + i + "\" class=\"current bold\" title=\"当前所在页\">" + i + "</a>" : "<a href=\"" + url + "Page=" + i + "\">" + i + "</a>";
            }
            retstr += pageindex != pagecount ? "<a href=\"" + url + "Page=" + next + "\"  title=\"下一页\">></a><a href=\"" + url + "Page=" + pagecount + "\"  title=\"最后一页\">>|</a>" : "";
            return retstr;
        }
    }
}
