package com.ssj.framework.basic.common.bean;

import java.io.Serializable;
import java.util.Map;

public class SplitPage implements Serializable{
    /**
	 * 
	 */
	private static final long serialVersionUID = -2823281440247472516L;
	private int pageNo = 1;
    private int pageSize = 10;
    private int rowCnt = 0;
    private int pages = 0; // 总页数
    private String action = null;
    private Map<String, Object> params = null;

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getRowCnt() {
        return rowCnt;
    }

    public void setRowCnt(int rowCnt) {
        this.rowCnt = rowCnt;
    }

    public String getAction() {
        return action;
    }

    public int getStartNum() {
        return (this.pageNo - 1) * this.pageSize + 1;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getPages() {
        if ((rowCnt % pageSize) == 0) {
            pages = rowCnt / pageSize;
        } else {
            pages = (rowCnt / pageSize) + 1;
        }
        return pages;
    }

    /**
     * 生成分页导航条
     * @return
     */
    public String getSplitHtml() {
        if (rowCnt <= 0) {
            return "";
        }
        StringBuffer html = new StringBuffer();
        int pageCnt = 0;// :一共有多少页
        if ((rowCnt % pageSize) == 0) {
            pageCnt = rowCnt / pageSize;
        } else {
            pageCnt = (rowCnt / pageSize) + 1;
        }

        int prePageNo = 1; //上一页页码
        if (pageNo > 1) {
            prePageNo = pageNo - 1;
        }

        int nextPageNo = pageCnt; //下一页页码
        if (pageNo < pageCnt) {
            nextPageNo = pageNo + 1;
        }

        html.append("<form id=\"splitFrom\" name=\"splitFrom\" method=\"post\" action=\"" + action + "\">\n");

        //页面参数封装
        html.append("<input type='hidden' id='pageNo' name='pageNo' value='").append(pageNo).append("'>\n");
        html.append("<input type='hidden' id='pageSize' name='pageSize' value='").append(pageSize).append("'>\n");

        if (params != null) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (!("pageNo".equals(entry.getKey()) || "pageSize".equals(entry.getKey()))) {
                    html.append("<input type=\"hidden\" name=\"" + entry.getKey() + "\" value=\"").append(entry.getValue()).append("\">\n");
                }
            }
        }

        html.append("<ul class='pagination'>\n");

        //添加首页 --- bryan add 2016-01-21
        if (pageCnt > 0) {
            if (pageNo == 1) {
                html.append("<li><a href='javascript:;' onclick='listData(1); return false;' aria-label='First'><span aria-hidden='true'>|&laquo;</span></a></li>\n");
            } else {
                html.append("<li><a href='javascript:;' onclick='listData(1);' aria-label='First'><span aria-hidden='true'>|&laquo;</span></a></li>\n");
            }
        }

        html.append("<li><a href='javascript:;' onclick='listData(").append(prePageNo).append(");' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>\n");

        if (pageCnt > 7) { //大于7页时才采取计算[左右为首尾页，中间5页]
            //取得中间显示的那页
            int centerPage = pageNo;
            if (pageNo < 4) { //如果当前页小于4，就一直以4为中心   
                centerPage = 4;
            } else if (pageNo + 3 > pageCnt) { //如果当前页快接近尾页，则以尾页减3为中心
                centerPage = pageCnt - 3;
            }

            //中间5页中的左2页
            if (centerPage > 4) { //如果中心页大于4，则表示前面与首页分开了
                //先添加首页
                html.append(generatePagerNum(1, 1, pageNo));

                html.append("<li><a href=\"#\" onclick=\"javascript:return false;\">...</a></li>\n");
                html.append(generatePagerNum(centerPage - 2, centerPage - 1, pageNo));
            } else {
                html.append(generatePagerNum(1, centerPage - 1, pageNo));
            }

            //产生中心页
            html.append(generatePagerNum(centerPage, centerPage, pageNo));

            //中间5页中的右2页
            if (centerPage + 3 < pageCnt) { //如果中心页离尾页超过3页，则表示与尾页分开了
                html.append(generatePagerNum(centerPage + 1, centerPage + 2, pageNo));
                html.append("<li><a href=\"#\" onclick=\"javascript:return false;\">...</a></li>\n");

                //添加尾页
                html.append(generatePagerNum(pageCnt, pageCnt, pageNo));
            } else {
                html.append(generatePagerNum(centerPage + 1, pageCnt, pageNo));
            }
        } else {
            html.append(generatePagerNum(1, pageCnt, pageNo));
        }

        html.append("<li><a href='javascript:;' onclick='listData(").append(nextPageNo).append(");' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>\n");

        //添加尾页 --- bryan add 2016-01-21
        if (pageCnt > 0) {
            if (pageNo == pageCnt) {
                html.append("<li><a href='javascript:;' onclick='listData(" + pageCnt + "); return false;' aria-label='Last'><span aria-hidden='true'>&raquo;|</span></a></li>\n");
            } else {
                html.append("<li><a href='javascript:;' onclick='listData(" + pageCnt + ");' aria-label='Last'><span aria-hidden='true'>&raquo;|</span></a></li>\n");
            }
        }

        html.append("</ul>\n");
        html.append("</form>");
        return html.toString();
    }

    /*
     * 循环产生分页的标签
     */
    private String generatePagerNum(int startNum, int endNum, int currentpage) {
        StringBuffer htmlStr = new StringBuffer();
        for (int i = startNum; i <= endNum; i++) {
            if (i == currentpage) {
                htmlStr.append("<li class=\"active\"><a href='javascript:;' onclick='listData(" + i + "); return false;'>" + i + "</a></li>\n");
            } else {
                htmlStr.append("<li><a href='javascript:;' onclick='listData(" + i + ");'>" + i + "</a></li>\n");
            }
        }
        return htmlStr.toString();
    }

    /**
     * 生成后台分页导航条
     * @return
     */
    public String getSysPaginHtml() {
        if (rowCnt <= 0) {
            return "";
        }
        StringBuffer html = new StringBuffer();
        int pageCnt = 0;// :一共有多少页
        if ((rowCnt % pageSize) == 0) {
            pageCnt = rowCnt / pageSize;
        } else {
            pageCnt = (rowCnt / pageSize) + 1;
        }

        int prePageNo = 1; //上一页页码
        if (pageNo > 1) {
            prePageNo = pageNo - 1;
        }

        int nextPageNo = pageCnt; //下一页页码
        if (pageNo < pageCnt) {
            nextPageNo = pageNo + 1;
        }

        html.append("<form id=\"splitFrom\" name=\"splitFrom\" method=\"post\" action=\"" + action + "\">\n");

        //页面参数封装
        html.append("<input type='hidden' id='pageNo' name='pageNo' value='").append(pageNo).append("'>\n");
        html.append("<input type='hidden' id='pageSize' name='pageSize' value='").append(pageSize).append("'>\n");
        html.append("<input type='hidden' id='attr1' name='attr1' value=''>\n");
        html.append("<input type='hidden' id='attr2' name='attr2' value=''>\n");

        if (params != null) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (!("pageNo".equals(entry.getKey()) || "pageSize".equals(entry.getKey()))) {
                    html.append("<input type=\"hidden\" name=\"" + entry.getKey() + "\" value=\"").append(entry.getValue()).append("\">\n");
                }
            }
        }

        html.append("<ul class='paginList'>\n");
        if (pageCnt > 1) {
            html.append("<li class='paginItem'><a href='javascript:;' onclick='listData(").append(prePageNo).append(");'><span class='pagepre'></span></a></li>\n");
        }
        if (pageCnt > 7) { //大于7页时才采取计算[左右为首尾页，中间5页]
            //取得中间显示的那页
            int centerPage = pageNo;
            if (pageNo < 4) { //如果当前页小于4，就一直以4为中心   
                centerPage = 4;
            } else if (pageNo + 3 > pageCnt) { //如果当前页快接近尾页，则以尾页减3为中心
                centerPage = pageCnt - 3;
            }

            //中间5页中的左2页
            if (centerPage > 4) { //如果中心页大于4，则表示前面与首页分开了
                //先添加首页
                html.append(generateSysPaginNum(1, 1, pageNo));

                html.append("<li class='paginItem more'><a href='javascript:;'>...</a></li>\n");
                html.append(generateSysPaginNum(centerPage - 2, centerPage - 1, pageNo));
            } else {
                html.append(generateSysPaginNum(1, centerPage - 1, pageNo));
            }

            //产生中心页
            html.append(generateSysPaginNum(centerPage, centerPage, pageNo));

            //中间5页中的右2页
            if (centerPage + 3 < pageCnt) { //如果中心页离尾页超过3页，则表示与尾页分开了
                html.append(generateSysPaginNum(centerPage + 1, centerPage + 2, pageNo));
                html.append("<li class='paginItem more'><a href='javascript:;'>...</a></li>\n");

                //添加尾页
                html.append(generateSysPaginNum(pageCnt, pageCnt, pageNo));
            } else {
                html.append(generateSysPaginNum(centerPage + 1, pageCnt, pageNo));
            }
        } else {
            html.append(generateSysPaginNum(1, pageCnt, pageNo));
        }
        if (pageCnt > 1) {
            html.append("<li class='paginItem'><a href='javascript:;' onclick='listData(").append(nextPageNo).append(");'><span class='pagenxt'></span></a></li>\n");
        }
        html.append("</ul>\n");
        html.append("</form>");
        return html.toString();
    }
    
    /*
     * 循环产生后台分页的标签
     */
    private String generateSysPaginNum(int startNum, int endNum, int currentpage) {
        StringBuffer htmlStr = new StringBuffer();
        for (int i = startNum; i <= endNum; i++) {
            if (i == currentpage) {
                htmlStr.append("<li class='paginItem current'><a href='javascript:;'>" + i + "</a></li>\n");
            } else {
                htmlStr.append("<li class='paginItem'><a href='javascript:;' onclick=\"javascript:listData('" + i + "');return false;\">" + i + "</a></li>\n");
            }
        }
        return htmlStr.toString();
    }
    
    /**
     * 获取人间分页html
     * @return
     */
    public String getJobPageHtml() {
        if (rowCnt <= 0) {
            return "";
        }
        StringBuffer html = new StringBuffer();
        int pageCnt = 0;// :一共有多少页
        if ((rowCnt % pageSize) == 0) {
            pageCnt = rowCnt / pageSize;
        } else {
            pageCnt = (rowCnt / pageSize) + 1;
        }

        int prePageNo = 1; //上一页页码
        if (pageNo > 1) {
            prePageNo = pageNo - 1;
        }

        int nextPageNo = pageCnt; //下一页页码
        if (pageNo < pageCnt) {
            nextPageNo = pageNo + 1;
        }
        html.append("<form id=\"jobSpFrom\" name=\"jobSpFrom\" method=\"get\" action=\"" + action + "\">\n");

        //页面参数封装
        html.append("<input type=\"hidden\" id=\"pageNo\" name=\"p\" value=\"").append(pageNo).append("\">\n");

        if (params != null) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (!("p".equals(entry.getKey()) || "pageSize".equals(entry.getKey()))) {
                    html.append("<input type=\"hidden\" name=\"" + entry.getKey() + "\" value=\"").append(entry.getValue()).append("\">\n");
                }
            }
        }
        
        if (pageNo == 1) {
            html.append("<a id=\"rtPrev\" class=\"dicon\" href=\"javascript:;\"></a>\n");
        } else {
            html.append("<a id=\"rtPrev\" class=\"dicon on\" href=\"javascript:;\" onclick=\"javascript:jobListData(").append(prePageNo).append(");\"></a>\n");
        }
        
        html.append("<span class=\"se_c_orange\">").append(pageNo).append("</span>/").append(pageCnt).append("\n");
        
        if (pageNo == pageCnt) {
            html.append("<a id=\"rtNext\" class=\"dicon off\" href=\"javascript:;\"></a>\n");
        } else {
            html.append("<a id=\"rtNext\" class=\"dicon on\" href=\"javascript:;\" onclick=\"javascript:jobListData(").append(nextPageNo).append(");\"></a>\n");
        }
        html.append("</form>");
        return html.toString();
    }

    /**
     * 生成分页导航条
     * @return
     */
    public String getCourseHtml() {
        if (rowCnt <= 0) {
            return "";
        }
        StringBuffer html = new StringBuffer();
        int pageCnt = 0;// :一共有多少页
        if ((rowCnt % pageSize) == 0) {
            pageCnt = rowCnt / pageSize;
        } else {
            pageCnt = (rowCnt / pageSize) + 1;
        }

        int prePageNo = 1; //上一页页码
        if (pageNo > 1) {
            prePageNo = pageNo - 1;
        }

        int nextPageNo = pageCnt; //下一页页码
        if (pageNo < pageCnt) {
            nextPageNo = pageNo + 1;
        }

        html.append("<form id=\"splitFrom\" name=\"splitFrom\" method=\"post\" action=\"" + action + "\">\n");

        //页面参数封装
        html.append("<input type='hidden' id='pageNo' name='pageNo' value='").append(pageNo).append("'>\n");
        html.append("<input type='hidden' id='pageSize' name='pageSize' value='").append(pageSize).append("'>\n");

        if (params != null) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (!("pageNo".equals(entry.getKey()) || "pageSize".equals(entry.getKey()))) {
                    html.append("<input type=\"hidden\" name=\"" + entry.getKey() + "\" value=\"").append(entry.getValue()).append("\">\n");
                }
            }
        }

        html.append("<ul class='pagination pull-right'>\n");

        //添加首页 --- bryan add 2016-01-21
        if (pageCnt > 0) {
            if (pageNo == 1) {
            	html.append("<li><a href='javascript:;' onclick='listData(1);' aria-label='Previous' class='previous'><span aria-hidden='true'>上一页</span></a></li>\n");
            } else {
            	html.append("<li><a href='javascript:;' onclick='listData(").append(prePageNo).append(");' aria-label='Previous' class='previous'><span aria-hidden='true'>上一页</span></a></li>\n");
            }
        }

        

        if (pageCnt > 7) { //大于7页时才采取计算[左右为首尾页，中间5页]
            //取得中间显示的那页
            int centerPage = pageNo;
            if (pageNo < 4) { //如果当前页小于4，就一直以4为中心   
                centerPage = 4;
            } else if (pageNo + 3 > pageCnt) { //如果当前页快接近尾页，则以尾页减3为中心
                centerPage = pageCnt - 3;
            }

            //中间5页中的左2页
            if (centerPage > 4) { //如果中心页大于4，则表示前面与首页分开了
                //先添加首页
                html.append(generatePagerNum(1, 1, pageNo));

                html.append("<li><a href=\"#\" onclick=\"javascript:return false;\">...</a></li>\n");
                html.append(generatePagerNum(centerPage - 2, centerPage - 1, pageNo));
            } else {
                html.append(generatePagerNum(1, centerPage - 1, pageNo));
            }

            //产生中心页
            html.append(generatePagerNum(centerPage, centerPage, pageNo));

            //中间5页中的右2页
            if (centerPage + 3 < pageCnt) { //如果中心页离尾页超过3页，则表示与尾页分开了
                html.append(generatePagerNum(centerPage + 1, centerPage + 2, pageNo));
                html.append("<li><a href=\"#\" onclick=\"javascript:return false;\">...</a></li>\n");

                //添加尾页
                html.append(generatePagerNum(pageCnt, pageCnt, pageNo));
            } else {
                html.append(generatePagerNum(centerPage + 1, pageCnt, pageNo));
            }
        } else {
            html.append(generatePagerNum(1, pageCnt, pageNo));
        }

       

        //添加尾页 --- bryan add 2016-01-21
        if (pageCnt > 0) {
            if (pageNo == pageCnt) {
                html.append("<li><a href='javascript:;' onclick='listData(").append(pageCnt).append(");' aria-label='Next'  class='next'><span aria-hidden='true'>下一页</span></a></li>\n");
            } else {
                html.append("<li><a href='javascript:;' onclick='listData(").append(nextPageNo).append(");' aria-label='Next'  class='next'><span aria-hidden='true'>下一页</span></a></li>\n");
            }
        }

        html.append("</ul>\n");
        html.append("</form>");
        return html.toString();
    }
    
}
