<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String pagerId = "Pager";
    String paramPagerIdStr = request.getParameter("pagerId");
    if (StringUtils.isNotEmpty(paramPagerIdStr)){
        pagerId = paramPagerIdStr;
    }
    int pageNo = 1;
    String paramPageNoStr = request.getParameter("pageNo");
    if (StringUtils.isNotEmpty(paramPageNoStr) && StringUtils.isNumeric(paramPageNoStr)){
        pageNo = Integer.parseInt(paramPageNoStr);
    }
    pageContext.setAttribute("pageNo", pageNo);
    int pageSize = 10;
    String paramPageSizeStr = request.getParameter("pageSize");
    if (StringUtils.isNotEmpty(paramPageSizeStr) && StringUtils.isNumeric(paramPageSizeStr)){
        pageSize = Integer.parseInt(paramPageSizeStr);
    }
    pageContext.setAttribute("pageSize", pageSize);
    int total = 0;
    String paramTotalStr = request.getParameter("total");
    if (StringUtils.isNotEmpty(paramTotalStr) && StringUtils.isNumeric(paramTotalStr)){
        total = Integer.parseInt(paramTotalStr);
    }
    pageContext.setAttribute("total", total);
    final int totalPages = total <= 0 ? 1 :(int) Math.ceil((double) total/pageSize);
    pageContext.setAttribute("totalPages", totalPages);
    final int itemNoStart = total == 0 ? 0 : (pageNo - 1) * pageSize + 1;
    final int itemNoEnd = pageNo < totalPages ? pageNo * pageSize : total;

    String searchFunction = "search();";
    String paramSearchFunctionStr = request.getParameter("searchFunction");
    if (StringUtils.isNotEmpty(paramSearchFunctionStr)){
        searchFunction = paramSearchFunctionStr;
    }
    String pageHidenId = null;
    String paramPageHidenIdStr = request.getParameter("pageHidenId");
    if (StringUtils.isNotEmpty(paramPageHidenIdStr)){
        pageHidenId = paramPageHidenIdStr;
    }
%>
<div class="form-pagination" id="<%=pagerId%>">
    <span><%=itemNoStart%>-<%=itemNoEnd%>项，共<%=total%>项</span>
    <ul class="pagination paginate-btn">
        <% String firstAndPrePageClass = pageNo > 1 && totalPages > 1 ? "" : "disabled"; %>
        <li ><a href="javascript:void(0);" page="1" class="<%=firstAndPrePageClass%>">首页</a></li>
        <li><a href="javascript:void(0);" page="<%=pageNo-1%>" class="<%=firstAndPrePageClass%>">上一页</a></li>
        <c:choose>
            <c:when test="${totalPages <= 5}">
                <c:forEach var="i" begin="1" end="${totalPages}" step="1">
                    <li class="${pageNo == i ? "active" : ""}"><a href="javascript:void(0);" class="${pageNo == i ? "disabled" : ""}" page="${i}">${i}</a></li>
                </c:forEach>
            </c:when>
            <c:otherwise>
                <c:choose>
                    <c:when test="${pageNo <= 3}">
                        <li class="${pageNo == 1 ? "active" : ""}"><a href="javascript:void(0);" class="${pageNo == 1 ? "disabled" : ""}" page="1">1</a></li>
                        <li class="${pageNo == 2 ? "active" : ""}"><a href="javascript:void(0);" class="${pageNo == 2 ? "disabled" : ""}" page="2">2</a></li>
                        <li class="${pageNo == 3 ? "active" : ""}"><a href="javascript:void(0);" class="${pageNo == 3 ? "disabled" : ""}" page="3">3</a></li>
                        <li><a href="javascript:void(0);" page="4">4</a></li>
                        <li><a href="javascript:void(0);" class="disabled">...</a></li>
                        <li><a href="javascript:void(0);" page="<%=totalPages%>"><%=totalPages%></a></li>
                    </c:when>
                    <c:when test="${pageNo >= totalPages - 2}">
                        <li><a href="javascript:void(0);" page="1">1</a></li>
                        <li><a href="javascript:void(0);" class="disabled">...</a></li>
                        <li><a href="javascript:void(0);" page="@(Model.TotalPages - 3)">@(Model.TotalPages - 3)</a></li>
                        <li class="${pageNo == totalPages - 2 ? "active" : ""}"><a href="javascript:void(0);" class="${pageNo == totalPages - 2 ? "disabled" : ""}" page="${totalPages - 2}">${totalPages - 2}</a></li>
                        <li class="${pageNo == totalPages - 1 ? "active" : ""}"><a href="javascript:void(0);" class="${pageNo == totalPages - 1 ? "disabled" : ""}" page="${totalPages - 1}">${totalPages - 1}</a></li>
                        <li class="${pageNo == totalPages ? "active" : ""}"><a href="javascript:void(0);" class="${pageNo == totalPages ? "disabled" : ""}" page="${totalPages}">${totalPages}</a></li>
                    </c:when>
                    <c:otherwise>
                        <li><a href="javascript:void(0);" page="1">1</a></li>
                        <li><a href="javascript:void(0);" class="disabled">...</a></li>
                        <li><a href="javascript:void(0);" page="<%=pageNo-1%>"><%=pageNo-1%></a></li>
                        <li class="active"><a href="javascript:void(0);" class="disabled" page="<%=pageNo%>"><%=pageNo%></a></li>
                        <li><a href="javascript:void(0);" page="<%=pageNo+1%>"><%=pageNo+1%></a></li>
                        <li><a href="javascript:void(0);" class="disabled">...</a></li>
                        <li><a href="javascript:void(0);" page="<%=totalPages%>"><%=totalPages%></a></li>
                    </c:otherwise>
                </c:choose>
            </c:otherwise>
        </c:choose>
        <li><a href="javascript:void(0);" page="${pageNo + 1}" class="${pageNo < totalPages && totalPages > 1 ? "" : "disabled"}">下一页</a></li>
        <li><a href="javascript:void(0);" page="${totalPages}" class="${pageNo < totalPages && totalPages > 1 ? "" : "disabled"}">尾页</a></li>
    </ul>
</div>
<script type="text/javascript">
    $("#" + "<%=pagerId%>" + " ul.pagination li a").not(".disabled").click(function () {
        var pageNo = $(this).attr("page");
        <%
            if (pageHidenId == null){
                out.println("$(\"input[type='hidden'][name='pageNo']\").val(pageNo);");
            }else {
                out.println("$(\"input[type='hidden'][id='\" + \""+ pageHidenId + "\" + \"'][name='pageNo']\").val(pageNo);");
            }
        %>
        <%=searchFunction%>
    });
</script>