<%@ page contentType="text/html; charset=euc-kr"%>
<jsp:directive.page import="java.util.List" />
<jsp:directive.page
	import="org.kldp.ebmsh.moniter.persistant.SearchResult" />
<jsp:directive.page import="java.util.ArrayList" />
<jsp:directive.page import="java.util.Calendar" />
<jsp:directive.page
	import="org.kldp.ebmsh.moniter.persistant.EbmsMoniterManager" />

<%
	List searchResultList = new ArrayList();
	long searchResultCount = 0;
	int currentPage = 1;
	int perPageContentCount = 10;
	String parameter = "";
	
	String searchType = request.getParameter("searchType");
	
	if( searchType != null ) {
		if( searchType.equals("documentId") ) {
	// DocumentId로 검색
	String documentId = request.getParameter("documentId");
	
	String pageStr = request.getParameter("page");
	
	if( pageStr != null )
		currentPage = Integer.parseInt(pageStr);
	
		parameter = "searchType=" + searchType + "&documentId=" + documentId;
		
		EbmsMoniterManager bizboxMoniterManager = new EbmsMoniterManager();
		
		searchResultList = bizboxMoniterManager.searchReceiveMessagesByDocumentId(documentId, currentPage);
		searchResultCount = bizboxMoniterManager.searchReceiveMessageCountByDocumentId(documentId);
	}
	else if( searchType.equals("messageId") ) {
		// MessageId로 검색
		String messageId = request.getParameter("messageId");
		
		String pageStr = request.getParameter("page");
		
		if( pageStr != null )
			currentPage = Integer.parseInt(pageStr);
			
			parameter = "searchType=" + searchType + "&messageId=" + messageId;
			
			EbmsMoniterManager bizboxMoniterManager = new EbmsMoniterManager();
			
			SearchResult searchResult = null;
			
			searchResult = bizboxMoniterManager.searchReceiveMessageByMessageId(messageId);
			
			if( searchResult != null ) {
				searchResultList.add(searchResult);
				
				searchResultCount = 1;
			}
		}
		else if( searchType.equals("period") ) {
			// Period로 검색
			int startYear = Integer.parseInt(request.getParameter("startYear"));
			int startMonth = Integer.parseInt(request.getParameter("startMonth"));
			int startDay = Integer.parseInt(request.getParameter("startDay"));
			
			int endYear = Integer.parseInt(request.getParameter("endYear"));
			int endMonth = Integer.parseInt(request.getParameter("endMonth"));
			int endDay = Integer.parseInt(request.getParameter("endDay"));
				
			String pageStr = request.getParameter("page");
			
			if( pageStr != null )
				currentPage = Integer.parseInt(pageStr);
				
			parameter = "searchType=" + searchType + "&startYear=" + startYear + "&startMonth=" + startMonth + "&startDay=" + startDay + "&endYear=" + endYear + "&endMonth=" + endMonth + "&endDay=" + endDay;		
				
			Calendar startCalendar = Calendar.getInstance();
			Calendar endCalendar = Calendar.getInstance();
			
			startCalendar.set(startYear, startMonth-1, startDay);
			endCalendar.set(endYear, endMonth-1, endDay);
			
			EbmsMoniterManager bizboxMoniterDAO = new EbmsMoniterManager();
			searchResultList = bizboxMoniterDAO.searchReceiveMessagesByPeriod(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis(), currentPage);
			searchResultCount = bizboxMoniterDAO.searchReceiveMessageCountByPeriod(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());
		}
	}
%>


<!--검색-->
<table width="100%" height="45" border="0" cellpadding="0"
	cellspacing="1" bgcolor="CECECF">
	<tr>
		<td bgcolor="F8F8F8">
			<form action="messageSearch.jsp" style="margin:0px;padding:0px;"
				name="searchForm">
				<select class="selectBox" name="searchType"
					OnChange="conditionChanged(this);" style="margin-left:10px">
					<option value="documentId" selected>
						문서 ID
					</option>

					<option value="messageId">
						메시지 ID
					</option>

					<option value="period">
						기간
					</option>
				</select>

				<span id="condition_detail"
					style="text-align:center; vertical-align:middle; width:620px">
					<input name="documentId" type="text" size="100" maxlength="255" />
				</span>

				<span align="right" style="vertical-align:middle; margin-left:10px">
					<img src="<%= request.getContextPath() %>/image/search/btn_search.gif" width="79"
						height="24" onClick="searchForm.submit();"> </span>
			</form>
		</td>
	</tr>
</table>
<!--/검색-->

<br>
<table width="100%" height="39" border="0" cellpadding="0"
	cellspacing="0">
	<tr>
		<td background="sub/search_bg.gif"
			style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px;">
			검색어가
			<span class="style1"><%=searchResultCount%> </span>개 검색되었습니다.
		</td>
	</tr>
</table>

<br>
<table width="100%" border="0" align="center" cellpadding="0"
	cellspacing="0">
	<tbody>
		<tr>
			<td width="15%" height="30" align="center"
				background="<%= request.getContextPath() %>/image/search/td_bg.gif">
				문서ID
			</td>
			<td width="27%" align="center"
				background="<%= request.getContextPath() %>/image/search/td_bg.gif">
				메세지ID
			</td>
			<td width="16%" align="center"
				background="<%= request.getContextPath() %>/image/search/td_bg.gif">
				받은시간
			</td>
			<td width="22%" align="center"
				background="<%= request.getContextPath() %>/image/search/td_bg.gif">
				메세지 처리상태
			</td>
			<td width="20%" align="center"
				background="<%= request.getContextPath() %>/image/search/td_bg.gif">
				어플리케이션 처리상태
			</td>
		</tr>

		<%
		if (searchResultList == null || searchResultList.isEmpty()) {
		%>
		<tr>
			<td colspan="5" style="text-align:center">
				검색 결과가 없습니다.
			</td>
		</tr>

		<tr>
			<td colspan="5" align="center" bgcolor="#f0f0f0"></td>
		</tr>
		<%
				} else {
				int searchResultSize = searchResultList.size();

				for (int i = 0; i < searchResultSize; i++) {
					SearchResult searchResult = (SearchResult) searchResultList
					.get(i);
		%>
		<tr>
			<td height="20" align="center">
				<%
							String documentId = searchResult.getDocumentId();

							if (documentId == null) {
				%>
				<a
					href="messageInfo.jsp?ocumentId=&messageId=<%=searchResult.getMessageId()%>" />-</a>
				<%
				} else {
				%>
				<a
					href="messageInfo.jsp?documentId=<%=searchResult.getDocumentId()%>&messageId=<%=searchResult.getMessageId()%>" /><%=documentId%>
				</a>
				<%
				}
				%>
			</td>
			<td width="27%" align="center">
				<%=searchResult.getMessageId()%>
			</td>
			<td width="16%" align="center">
				<%=searchResult.getReceiveTime()%>
			</td>
			<td width="22%" align="center">
				<%= searchResult.getEbxmlMessageStatus() %>
			</td>
			<td width="20%" align="center">
				<%= searchResult.getBsiMessageStatus() %>
			</td>
		</tr>

		<tr>
			<td colspan="5" align="center" bgcolor="#f0f0f0"></td>
		</tr>
		<%
			}
			}
		%>

		<!-- <tr>
			<td height="20" align="center">
				ㅇㅇ
			</td>
			<td width="27%" align="center">
				&nbsp;
			</td>
			<td width="16%" align="center">
				&nbsp;
			</td>
			<td width="22%" align="center">
				&nbsp;
			</td>
			<td width="20%" align="center">
				&nbsp;
			</td>
		</tr>

		<tr>
			<td colspan="5" align="center" bgcolor="#f0f0f0"></td>
		</tr>

		<tr>
			<td height="20" align="center">
				ㅇㅇ
			</td>
			<td width="27%" align="center">
				&nbsp;
			</td>
			<td width="16%" align="center">
				&nbsp;
			</td>
			<td width="22%" align="center">
				&nbsp;
			</td>
			<td width="20%" align="center">
				&nbsp;
			</td>
		</tr> 

		<tr>
			<td height="1" colspan="5"></td>
		</tr>-->
		<tr>
			<!-- start of : 리스트 -->
			<td colspan="5" valign="top">
				<table cellspacing="0" cellpadding="0" width="100%" border="0">
					<tr>
						<td bgcolor="#f0f0f0" height="1"></td>
					</tr>
				</table>
			</td>
			<!-- end of : 리스트 -->
		</tr>
		<tr>
			<!-- start of : 리스트  보기-->
			<td height="50" colspan="5" align="middle">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td>
							<div align="center">
								<%
									int totalPageCount = (int) (searchResultCount / perPageContentCount) + 1;
									int startPage = ((currentPage - 1) / 5) * 5 + 1;
									int endPage = startPage + 4;

									if (endPage > totalPageCount)
										endPage = totalPageCount;

									// Prev button
									if (currentPage <= 5) {
								%>
								<%
										} else {
										int prePage = startPage - 5;
										String preLinkPath = "messageSearch.jsp?" + parameter
										+ "&page=" + prePage;
								%>
								<a href="<%=preLinkPath%>"><img
										src="<%= request.getContextPath() %>/image/search/page_list_butn04_01.gif"
										width="16" height="12" border="0"> </a>
								<%
									}

									// Page button
									for (int i = startPage; i <= endPage; i++) {
										String numberLinkPath = "messageSearch.jsp?" + parameter
										+ "&page=" + i;

										if (i != startPage)
											out.println(" | ");

										if (i == currentPage) {
								%>
								<b><%=i%> </b>
								<%
								} else {
								%>
								<a href="<%=numberLinkPath%>"><%=i%> </a>
								<%
									}
									}

									// Next button
									if (startPage > totalPageCount - 5) {
								%>
								<%
										} else {
										int nextPage = startPage + 5;
										String nextLinkPath = "messageSearch.jsp?" + "page=" + nextPage
										+ "&" + parameter;
								%>
								<a href="<%=nextLinkPath%>"><img
										src="<%= request.getContextPath() %>/image/search/page_list_butn04_04.gif"
										width="16" height="12" border="0"> </a>
								<%
								}
								%>
							</div>
						</td>
					</tr>
				</table>
			</td>
			<!-- end of : 리스트  보기-->
		</tr>
	</tbody>
</table>
<p>
	&nbsp;
</p>
