<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form" %>
<script language="javascript">
	var nIntervId;
	function queryfn(w){
		document.getElementById("radiovalue").value = w;
		if(dispatch("ownerChange")) {
			document.getElementById("form").submit();
		}	
	}
	function exportQuery(form){				
		if( !isChecked(form.selected) ){
			alert("Please select at least one query to export.");
			return false;
		}
		var isConfirmed = confirm('Are you sure you wish to export the selected queries?');
		if(!isConfirmed){
			return false;
		}
		var previousFormAction = form.action;
		form.action = "exportQueries.form";
		form.submit();
		form.action = previousFormAction;
		deselectCheckBox(form.selected);
	}
	function showProgress(queryId)	{
		$("#run_" + queryId).hide();
		$("#csv_" + queryId).hide();
		$("#wait_run_" + queryId).show();
	}
	function hideProgress(queryId)	{
		$("#run_" + queryId).show();
		$("#csv_" + queryId).show();
		$("#wait_run_"+queryId).hide();
	}

	function runQBReport(queryId, view){
		showProgress(queryId);
		$.ajax({
			type : "GET",
			url : "../emdt/runAsynchronousQBQuery.form",
			data : "queryId="+queryId+"&view="+view,
			success : function(msg) {
				if(msg.status){
					$("#messageSpanId").html(msg.status);
					$("#messageId").show()
					nIntervId=setInterval(checkReportResult, 3000, queryId, view)
				}
				if(msg.errorMsg){
					$("#errorSpanId").html(msg.errorMsg);
					$("#errorMsgId").show()
				}
			
			}
		});
	}
	
	function checkReportResult(queryId, view){
		$.ajax({
			type : "GET",
			url : "../emdt/checkQBQueryResult.form",
			data : "queryId="+queryId+"&view="+view,
			success : function(msg) {
				$("#messageSpanId").html(msg.status);
				if(msg.status=='completed'){
					hideProgress(queryId);
					clearInterval(nIntervId);
					if(view=='html')
						window.open("../emdt/runQueryHtmlView.jsp?queryId="+queryId+"&view="+view, '_blank');
					else if(view=='csv')
						window.open("../emdt/downloadCSVView.form?queryId="+queryId+"&view="+view, '_blank');
				}
			}
		});
	}
</script>
<c:if test="${qbFilter == null }">
	<c:set var="qbFilter" value="my" scope="session"/>
</c:if>
<c:if test="${param.qbFilter != null }">
	<c:set var="qbFilter" value="${param.qbFilter }" scope="session"/>
</c:if>
<c:set var="queries" value="${emdt.queriesList }" />

<div class="breadcrumb">
You are here: <a href="../emdt/emdtHome.form"><img src="../emdt/images/16x16/icon.png" style="vertical-align: top;"/> eMDT</a> &gt; <a href="../emdt/reportMenu.jsp">Reports</a> &gt; <a class="selected" href="../emdt/queries.jsp">Query Builder</a>
</div>

<br/>

<smvc:form action="deleteQueries.form" method="POST">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<c:if test="${not empty message }">
		<div class="message"><img src="images/16x16/information.png" alt="i" /><c:out value="${message}"/></div>
	</c:if>
	<div class="message" id="messageId" style="display: none;"><img src="../images/16x16/information.png" /><span id="messageSpanId"></span></div>
	<div class="error" id="errorMsgId" style="display: none;"><img src="../images/16x16/error.png"/><span id="errorSpanId"></span></div>
<table width="70%" >
	<tr>
		<td>
			Show :
			<input type="radio" name="qbFilter" value="my" <c:out value="${qbFilter == 'my' ? 'checked' : '' }"/> onclick="document.location='queries.jsp?qbFilter=my'"/>
			My Queries
			<input type="radio" name="qbFilter" value="all" <c:out value="${qbFilter == 'all' ? 'checked' : '' }"/> onclick="document.location='queries.jsp?qbFilter=all'"/>
			All Queries
		</td>
	</tr>
	<tr>
		<td>
			<tags:table title="Queries">
				<table cellspacing="0" cellpadding="4" width="100%" class="table">
					<tr>
						<th>Description</th>
						<th width="1%">View</th>
						<th width="1%">Download</th>
						<th width="1%">Edit</th>
						<th>Owner</th>
						<th width="1%">Select</th>
					</tr>
					<c:if test="${empty queries}">
						<tr>
							<td colspan="6" align="center">-- No queries defined --</td>
						</tr>
					</c:if>
					<c:forEach var="n" items="${queries}" varStatus="s">
						<c:if test="${qbFilter == 'all' or (qbFilter =='my' and n.owner.username == webapp.currentUser.username)}">
							<tr class="<c:out value="${s.count % 2 == 0 ? 'even' : 'odd'}"/>Row">
								<td><c:out value="${n.description}"/>&nbsp;
								</td>
								<td align="center">
									<div id="wait_run_${n.id}" style="display: none;"><img src="../images/ajax-loader-bl.gif" alt="Loading"></div>
									<div id="run_${n.id}">
										<a href="javascript:runQBReport('<c:out value="${n.id}"/>', 'html')" title="View">
											<img src="../images/16x16/application_view_columns.png" border="0" alt="View"/>
										</a>
									</div>
								</td>
								<td align="center">
									<div id="wait_csv_${n.id}" style="display: none;"><img src="../images/ajax-loader-bl.gif" alt="Loading"></div>
									<div id="csv_${n.id}">
										<a href="javascript:runQBReport('<c:out value="${n.id}"/>', 'csv')" title="Download CSV file">
										<img src="../images/16x16/database_save.png" border="0" alt="CSV"/>
									</a>
									</div>
								</td>
								<td align="center">
									<c:if test="${n.owner.username == webapp.currentUser.username }">
										<a href="editQueryBuilder.form?queryBuilderId=<c:out value="${n.id}"/>" title="Edit query">
											<img src="../images/page_white_edit.png" border="0" alt="Edit"/>
										</a>
									</c:if>
									<c:if test="${n.owner.username != webapp.currentUser.username }">
										<sec:authorize access="hasRole('ADMIN')">
										<a href="editQueryBuilder.form?queryBuilderId=<c:out value="${n.id}"/>" title="Edit query">
											<img src="../images/page_white_edit.png" border="0" alt="Edit"/>
										</a>
										</sec:authorize>
									</c:if>									
								</td>
								<td width="1%"><c:out value="${n.owner.username}"/></td>
								<td width="1%" align="center">
									<c:if test="${n.owner.username == webapp.currentUser.username }">
										<input type="checkbox" name="selected" value="<c:out value="${n.id }"/>"/>
									</c:if>
									<c:if test="${n.owner.username != webapp.currentUser.username }">
										<sec:authorize access="hasRole('ADMIN')">
											<input type="checkbox" name="selected" value="<c:out value="${n.id }"/>"/>
										</sec:authorize>
									</c:if>
								</td>
							</tr>
						</c:if>
					</c:forEach>
					<tr>
						<td colspan="5">
						<!--
                        <input type="button" value="Generate Letter" onclick="document.location='letterGenerate.form'"/>
                        -->
                        </td>
						<td align="right"><input type="submit" value="Delete" onclick="return confirm('Are you sure you wish to delete the selected items?')"/></td>
						
					</tr>
					
					<tr>
						<td colspan="5">&nbsp;</td>
						<td align="right"><input type="button" value="Export" onclick="exportQuery(this.form);"/></td>
					</tr>
				</table>
			</tags:table>
			<br>
			<table> 
				<tr>
					<td>
						<input type="button" value="New Query..." onclick="document.location='editQueryBuilder.form'"/>
					</td>
					<td>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					</td>
					<td>
						<input type="button" value="Import Query..." onclick="document.location='importQueryBuilder.form'"/>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</smvc:form>