<%@ 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 basePath = request.getContextPath();
    String resourcePath = basePath + "/resource";
%>
<%
    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 = 5;
    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="ys0" id="<%=pagerId%>">
    <% String firstAndPrePageClass = pageNo > 1 && totalPages > 1 ? "" : " disabled"; %>
    <button class="ys <%=firstAndPrePageClass%>" page="<%=pageNo-1%>"></button>
    <c:choose>
        <c:when test="${totalPages <= 5}">
            <c:forEach var="i" begin="1" end="${totalPages}" step="1">
                <button class="ys1${pageNo == i ? " ys1-active" : ""}" page="${i}">${i}</button>
            </c:forEach>
        </c:when>
        <c:otherwise>
            <c:choose>
                <c:when test="${pageNo <= 3}">
                    <button class="ys1${pageNo == 1 ? " ys1-active disabled" : ""}" page="1">1</button>
                    <button class="ys1${pageNo == 2 ? " ys1-active disabled" : ""}" page="2">2</button>
                    <button class="ys1${pageNo == 3 ? " ys1-active disabled" : ""}" page="3">3</button>
                    <button class="ys1" page="4">4</button>
                    <button class="ys2 disabled">...</button>
                    <button class="ys1" page="<%=totalPages%>"><%=totalPages%></button>
                </c:when>
                <c:when test="${pageNo >= totalPages - 2}">
                    <button class="ys1" page="1">1</button>
                    <button class="ys2 disabled">...</button>
                    <button class="ys1" page="${totalPages - 3}">${totalPages - 3}</button>
                    <button class="ys1${pageNo == totalPages - 2 ? " ys1-active disabled" : ""}" page="${totalPages - 2}">${totalPages - 2}</button>
                    <button class="ys1${pageNo == totalPages - 1 ? " ys1-active disabled" : ""}" page="${totalPages - 1}">${totalPages - 1}</button>
                    <button class="ys1${pageNo == totalPages ? " ys1-active disabled" : ""}" page="${totalPages}">${totalPages}</button>
                </c:when>
                <c:otherwise>
                    <button class="ys1" page="1">1</button>
                    <button class="ys2 disabled">...</button>
                    <button class="ys1" page="${pageNo-1}">${pageNo-1}</button>
                    <button class="ys1 ys1-active disabled" page="${pageNo}">${pageNo}</button>
                    <button class="ys1" page="${pageNo+1}">${pageNo+1}</button>
                    <button class="ys2 disabled">...</button>
                    <button class="ys1" page="${totalPages}">${totalPages}</button>
                </c:otherwise>
            </c:choose>
        </c:otherwise>
    </c:choose>
    <button class="ys3${pageNo < totalPages && totalPages > 1 ? "" : " disabled"}" page="${pageNo + 1}"></button>
</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>--%>