package com.palmnest.application.core.util;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * xiemin
 * 分页标签辅助类
 */
public class PageBean<T> extends TagSupport implements Serializable {

	private static final long serialVersionUID = 1L;

	public String  theme        = "url"; //跳转类型
	public String  url;//地址
	public Integer curPage      = 1;
	public Integer totalPage;// 总页数
	public Integer pageSize     = 10;
	public Integer totalCount   = 0;
	public Integer showPage     = 5;
	public String  formID       = "";
	public String  pageListShow = "";//页面显示div的id(ajax用到)

	public String getFormID() {
		return formID;
	}

	public void setFormID(String formID) {
		this.formID = formID;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getCurPage() {
		return curPage;
	}

	public void setCurPage(Integer curPage) {
		this.curPage = curPage;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

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

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getShowPage() {
		return showPage;
	}

	public void setShowPage(Integer showPage) {
		this.showPage = showPage;
	}

	public String getPageListShow() {
		return pageListShow;
	}

	public void setPageListShow(String pageListShow) {
		this.pageListShow = pageListShow;
	}

	public int doStartTag() throws JspException {

		JspWriter out = pageContext.getOut();

		int pageNumber = 0;
		if (totalPage % pageSize == 0) {
			pageNumber = totalPage / pageSize;
		} else {
			pageNumber = (totalPage / pageSize) + 1;
		}
		if (curPage < 1) {
			curPage = 1;
		}

		try {
			if (pageNumber > 0) {
//                out.print("<script type='text/javascript'>" +   
//                                "function go(pageNum){" +   
//                                    "var f = document.getElementById('" + formID + "');"+  
//                                    "f.action = f.action + '?pageNum=' + pageNum + '&pageSize="+pageSize+"';"+  
//                                    "f.submit();"+  
//                                "}" +   
//                          "</script>");  
				out.print(Jump());
				out.print("<div  class='l-btn-pages'>");
				out.print("<p class='wymy-invest-page l-btn-p'>");
				int start = 1;
				int end = totalPage;
				for (int i = showPage / 2; i >= 1; i--) {
					if ((curPage - i) >= 1) {
						start = curPage - i;
						break;
					}
				}
				for (int i = showPage / 2; i >= 1; i--) {
					if ((curPage + i) <= totalPage) {
						end = curPage + i;
						break;
					}
				}
				//如果小于9则右侧补齐
				if (end - start + 1 <= showPage) {
					Integer padLen = showPage - (end - start + 1);
					for (int i = padLen; i >= 1; i--) {
						if ((end + i) <= totalPage) {
							end = end + i;
							break;
						}
					}
				}

				//如果还小于9左侧补齐
				if (end - start + 1 <= showPage) {
					Integer padLen = showPage - (end - start + 1);
					for (int i = padLen; i >= 1; i--) {
						if ((start - i) >= 1) {
							start = start - i;
							break;
						}
					}
				}

//                if(curPage>1){  
//                    if(start>1){  
//                        out.print("<a href='javascript:go(1)' class='page-first'>首页</a>&nbsp;&nbsp;");  
//                    }else{
//                        out.print("<a href='javascript:go("+(curPage-1)+")' class='page-down'>上一页</a>&nbsp;&nbsp;");   
//                    }
//                }  

				for (int i = start; i <= end; i++) {
					if (i == curPage) {
						out.print("<a href='#'><span  class='current'>" + i + "</span></a>");
					} else {
						out.print("<a href='javascript:go(" + i + ")'><span  class='current' style='background-color:#ddd;' >" + i + "</span></a>&nbsp;&nbsp;");
					}
				}
//                if(curPage<totalPage){  
//                    out.print("<a href='javascript:go("+(curPage+1)+")' class='page-down'>下一页</a>&nbsp;&nbsp;");  
//                    if(end<totalPage){  
//                        out.print("<a href='javascript:go("+totalPage+")' class='page-first'>尾页</a>&nbsp;&nbsp;");  
//                    }  
//                }
				out.print("<b>共" + this.totalCount + "条</b>");
				out.print("<strong>跳转到第</strong>");
				out.print("<input type='text' id ='jumpPage'   size='4' value='" + curPage + "' class='current-input'><strong>页</strong>");
				out.print("<a href='javascript:jump()' class='' id='btnOk'>确定</a>");
				out.print("</p>");
				out.print("</div>");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();

	}

	public String Jump() {
		StringBuffer sb = new StringBuffer();
		sb.append("<script type='text/javascript'>");
		sb.append("function go(pageNum){");
		sb.append("$(\"#pageNo\").val(pageNum);");
		String script = "var jumpPage = $(\"#jumpPage\").val();"
				+ "if(isNaN(jumpPage)){"
				+ "alert(\"输入格式不正确，请重新输入!\");"
				+ "return;"
				+ "}"
				+ "if(jumpPage > " + this.totalPage + "){"
				+ "alert(\"输入页数大于当前最大页!\");"
				+ "$(\"#jumpPage\").val(" + this.totalPage + ");"
				+ "return;"
				+ "}"
				+ "$(\"#jumpPage\").val(jumpPage);";
		sb.append(script);
		if ("url".equals(this.theme)) {
			sb.append("var s = $('#'+'" + formID + "').serialize();");
//    		sb.append("var url = '"+this.url+"/'+pageNum+'?'+s;");
			sb.append("var url = '" + this.url + "?'+s;");
			sb.append("window.location.href=url;");
		} else if ("ajax".equals(this.theme)) {
			sb.append("$.ajax({");
			sb.append("type:'POST',");
			sb.append("url:'" + this.url + "',");
			sb.append("data:$('#'+'" + formID + "').serialize(),");
			sb.append("dataType: 'html',");
			sb.append("success: function (data) ");
			if (this.pageListShow == null || this.pageListShow.equals("")) {
				sb.append("{$('#pageList').html(data);}});");
			} else {
				sb.append("{$('#").append(this.pageListShow).append("').html(data);}});");
			}

		}
		sb.append("}");
		sb.append("function jump(){");
		sb.append(" var jumpP = document.getElementById('jumpPage').value;go(jumpP);");
		sb.append("}");
		sb.append("</script>");
		return sb.toString();
	}

	public static Integer getStartIndex(Integer pageNum, Integer pageSize) {
		Integer res = 0;
		if (pageNum > 0) {
			res = (pageNum - 1) * pageSize;
		}
		return res;
	}
}
