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

namespace Store.Common
{
    public class Pager
    {
        #region 生成分页控件
        /// <summary>
        /// 获取分页码
        /// </summary>
        /// <param name="PageUrl">当前请求的原始url,有时候会用到传有参数的页,下一步开始开发适合所有的页面分页</param>
        /// <param name="TotalCountRecord">需要显示的总记录数</param>
        /// <param name="PageItem">每页需要显示的记录数</param>
        /// <param name="CurrentPage">当前页码</param>
        /// <param name="Pagenums">界面需要显示多少页码</param>
        /// <returns>返回显示在页面上的分页控件</returns>
        /// 说明:用户可以根据自己的需要为页码间添加分隔符,可以控制选中页的页码样式
        /// 创建者:蔡万国
        /// 修改时间;2007-9-30
        public StringBuilder MyPage(string PageUrl, int TotalCountRecord, int PageItem, int CurrentPage, int Pagenums)
        {

            //偏移量
            int LeftNum = (int)Math.Ceiling((double)(Pagenums) / 2);
            int Step = LeftNum + 1;
            int RightNum = Pagenums - LeftNum - 1;
            //string PageUrl = Request.FilePath;
            //一共有多少页
            int PageCount = (int)Math.Ceiling((double)(TotalCountRecord) / PageItem);
            StringBuilder OutPut = new StringBuilder();
            //选中的页大于1就显示上一页
            //if (CurrentPage > 1)
            //{
            //    OutPut.Append("<a href='");
            //    OutPut.Append(PageUrl);
            //    OutPut.Append("&&page=");
            //    OutPut.Append(CurrentPage - 1);
            //    OutPut.Append("'>上一页</a>");
            //}

            //如果页数小于10就只输出当前的10以内的标号
            if (PageCount < Pagenums)
            {
                for (int i = 1; i <= PageCount; i++)
                {
                    if (i == CurrentPage)
                    {
                        //OutPut.Append("<font>");//控制控制选中页的页码样式
                        //OutPut.Append("  ");//控制控制选中页的页码样式
                        OutPut.Append(i.ToString());
                        //OutPut.Append("  ");//控制控制选中页的页码样式
                        //OutPut.Append("</font>");//控制控制选中页的页码样式
                    }
                    else
                    {
                        OutPut.Append("<a href='");
                        OutPut.Append(PageUrl);
                        OutPut.Append("&page=");
                        OutPut.Append(i.ToString());
                        OutPut.Append("'>");
                        //OutPut.Append(" ");//添加页码间的分隔符
                        OutPut.Append(i.ToString());
                        //OutPut.Append(" ");//添加页码间的分隔符
                        OutPut.Append("</a>");
                    }
                }

            }
            //大于10就得加上省略号
            else
            {
                if (CurrentPage + RightNum < PageCount)//当不超过的时候
                {
                    if (CurrentPage - Step < 1)
                    {
                        for (int i = 1; i <= Pagenums; i++)
                        {
                            if (i == CurrentPage)
                            {
                                //OutPut.Append("<font>");//控制控制选中页的页码样式
                                //OutPut.Append("  ");//控制控制选中页的页码样式
                                OutPut.Append(i.ToString());
                                //OutPut.Append("  ");//控制控制选中页的页码样式
                                //OutPut.Append("</font>");//控制控制选中页的页码样式
                            }
                            else
                            {
                                OutPut.Append("<a href='");
                                OutPut.Append(PageUrl);
                                OutPut.Append("&page=");
                                OutPut.Append(i.ToString());
                                OutPut.Append("'>");
                                //OutPut.Append(" ");//添加页码间的分隔符
                                OutPut.Append(i.ToString());
                                //OutPut.Append(" ");//添加页码间的分隔符
                                OutPut.Append("</a>");
                            }
                        }
                        if (PageCount > Pagenums)
                        {
                            OutPut.Append("...");
                        }
                    }

                    else
                    {
                        for (int i = CurrentPage - LeftNum; i <= CurrentPage + RightNum; i++)
                        {
                            if (i == CurrentPage)
                            {
                                //OutPut.Append("<font>");//控制控制选中页的页码样式
                                //OutPut.Append("  ");//控制控制选中页的页码样式
                                OutPut.Append(i.ToString());
                                //OutPut.Append("  ");//控制控制选中页的页码样式
                                //OutPut.Append("</font>");//控制控制选中页的页码样式
                            }
                            else
                            {
                                OutPut.Append("<a href='");
                                OutPut.Append(PageUrl);
                                OutPut.Append("&page=");
                                OutPut.Append(i.ToString());
                                OutPut.Append("'>");
                                //OutPut.Append(" ");//添加页码间的分隔符
                                OutPut.Append(i.ToString());
                                //OutPut.Append(" ");//添加页码间的分隔符
                                OutPut.Append("</a>");
                            }
                        }
                        if (CurrentPage + RightNum < PageCount)
                        {
                            OutPut.Append("...");
                        }
                    }
                }
                else//当超过的时候
                {
                    for (int i = PageCount - Pagenums + 1; i <= PageCount; i++)
                    {
                        if (i == CurrentPage)
                        {
                            //OutPut.Append("<font>");//控制控制选中页的页码样式
                            //OutPut.Append("  ");//控制控制选中页的页码样式
                            OutPut.Append(i.ToString());
                            //OutPut.Append("  ");//控制控制选中页的页码样式
                            //OutPut.Append("</font>");//控制控制选中页的页码样式
                        }
                        else
                        {
                            OutPut.Append("<a href='");
                            OutPut.Append(PageUrl);
                            OutPut.Append("&page=");
                            OutPut.Append(i.ToString());
                            OutPut.Append("'>");
                            OutPut.Append(" ");//添加页码间的分隔符
                            OutPut.Append(i.ToString());
                            OutPut.Append(" ");//添加页码间的分隔符
                            OutPut.Append("</a>");
                        }

                    }
                }
            }
            //if (CurrentPage < PageCount)
            //{
            //    OutPut.Append("<a href='");
            //    OutPut.Append(PageUrl);
            //    OutPut.Append("&&page=");
            //    OutPut.Append(CurrentPage + 1);
            //    OutPut.Append("'>下一页</a>");
            //}

            return OutPut;

        }
        #endregion
    }
}