<%@page import="org.springframework.ui.ModelMap"%>
<%@ include file="/includes.jsp" %>
<c:set var="resultVariable" value="qbResult${param.queryId}"/>
<% String resultVariable=  (String) pageContext.getAttribute("resultVariable");%>
<% ModelMap model=  (ModelMap)session.getAttribute(resultVariable);%>
<c:set var="qbResult" value="<%=session.getAttribute(resultVariable)%>"/>'
<% session.removeAttribute(resultVariable); %>
<script language="javascript">
	function runcvsQBReport(queryId, view){
		location.href="../emdt/downloadCSVView.form?queryId="+queryId+"&view="+view;
		<%session.setAttribute(resultVariable,model); %>
	}
</script>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=Edge">
		<meta http-equiv="Pragma" content="no-cache"/>
		<title>eMDT - Query Results</title>
		<link rel="icon" href="../images/emdt.ico" type="image/x-icon" />
		<link rel="shortcut icon" href="../images/emdt.ico" type="image/x-icon" />
		<link rel="stylesheet" href="../css/tables.css" type="text/css" />
		<link rel="stylesheet" href="../css/webapp.css" type="text/css" />
	</head>
	<body>
			<br/>
			<div class="table">
			<div class="tableTitleBar">Query Results - <c:out value="${qbResult.queryName}"/></div>
			<div class="tableContent">
				<table class="">
				     <c:forEach var="row" items="${qbResult.results}" varStatus="resultsStatus">
						<c:choose>
							<c:when test="${resultsStatus.count == 1}">
								<tr>
									<c:forEach var="item" items="${row}">
										<th><c:out value="${item }"/></th>
									</c:forEach>
								</tr>
							</c:when>
							<c:otherwise>
								<tr>
									<c:forEach var="item" items="${row}">
										<td><c:out value="${item }"/></td>
									</c:forEach>
								</tr>
							</c:otherwise>
						</c:choose>
					</c:forEach>
				</table>
			
			</div>
			</div>
		<c:if test="${qbResult.results != null }" >
		<br/>
		&#160;Results: <c:out value="${fn:length(qbResult.results) -1}"/>
		<br/>
		<br/>
		&#160;<a href="javascript:runcvsQBReport('<c:out value="${param['queryId']}"/>', 'csv')" title="Download CSV">Download CSV <img src="../images/16x16/database_save.png" alt="Download"/></a>
		<br/>
		</c:if>
	</body>
</html>
