<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<script>
	function getNote(o,filterId) {
        fullScreenPleaseWait();
        document.location = "consultationNote.form?conNoteId=" + o +"&filterFormId="+filterId;
	}
	
	function confirmDelete() {
		return window.confirm("Are you sure you wish to delete the selected notes?");
	}
	
	function unlock(o) {
		document.location = "unlockNote.do?id=" + o;
	}

	function deletePatientHistory(){

		if(confirmAndDispatch('delete', 'Are you sure you wish to delete the selected notes?')){
			document.forms[0].action = 'patientHistoryActions.form';
            fullScreenPleaseWait();
            document.getElementById('dispatchMethod').value="delete";
		}
	}
	function getPathologyFeeds() {
	    fullScreenPleaseWait();
		document.location = "../emdt/pathologyFeeds.form";
	}
</script>
<smvc:form action="/emdt/patientHistoryActions.form" id="form" modelAttribute="patientHistoryForm">
<c:set var="patientHistoryItems" value="${patientHistoryForm.patientHistoryItems }"/>
    <input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<input type="hidden" name="method" value="list" id="dispatchMethod"/>
	<c:if test="${patientHistoryForm.filterFormDefinition != null}">
		<input type="hidden" name="filterFormId" value="<c:out value="${patientHistoryForm.filterFormDefinition.id}"/>"/>
	</c:if>
	<input type="hidden" name="basePage" id="basePage" value='<c:out value="${basePage}"/>'/>


	<br/>
	<table  align="center" width="70%" border="0"><tr><td align="center" >
		<table width="75%">
			<tr>
				<td>
				<div class="table">
					<div class="tableTitleBar">Patient History</div>
					<div class="tableContent">
						<table class="table" cellpadding="4">
							<tr>
								<th colspan="2">Form Name</th>
								<th>Speciality</th>
								<th>Created</th>
								<th>Updated</th>
							    <th><sec:authorize access="hasAnyRole('ADMIN')">Select</sec:authorize></th>
							</tr>

							<c:set var="count" value="1"></c:set>
							<c:forEach var="item" items="${patientHistoryItems }" varStatus="s1">
								<c:if test="${item.formDefinition eq null or item.formDefinition.liveStatus != 0 or (item.formDefinition.liveStatus == 0 and item.formDefinition.owner eq emdt.currentUser.username)}">
									<tr class="<c:out value="${count % 2 == 0 ? 'evenRow' : 'oddRow'}"/>">
										<td onmouseover="this.style.cursor = cursorPointer" onclick="getNote('<c:out value="${item.note.guid}"/>','<c:out value="${item.formDefinition.id}"/>')">
											<c:out value="${item.formLabel}"/>
											<c:if test="${item.lockData != null }">
												<c:set var="since"><fmt:formatDate value="${item.lockData.since}" pattern="HH:mm"/></c:set>
												&nbsp;<img src="images/16x16/locked.png" title="Locked by <c:out value="${item.lockData.user.username}"/> since <c:out value="${since}"/>"/>
												<sec:authorize access="hasRole('ADMIN')">
													<a href="javascript:unlock(<c:out value="${item.note.id}"/>)">&nbsp;(Unlock)</a>
												</sec:authorize>
											</c:if>
										</td>
										<td align="right">
											<c:if test="${item.note.locked}">
												<img src="../images/16x16/lock.png" title="Form has been locked" />
											</c:if>
											<a onclick="getNote('<c:out value="${item.note.guid}"/>',<c:out value="${item.formDefinition.id}"/>)" title="Click to view or edit form" style="cursor: pointer;">
												<img src="../images/16x16/edit.png" />
											</a>
										</td>
										<td>
											<webapp:listValue domain="EMDT" listName="Speciality" key="${item.carespell.primarySite}" />
										</td>
										<td><fmt:formatDate value="${item.note.creationDate}" pattern="dd MMM yyyy HH:mm"/></td>
										<td><fmt:formatDate value="${item.note.modifiedDate}" pattern="dd MMM yyyy HH:mm"/></td>
									<td align="center"><sec:authorize access="hasAnyRole('ADMIN')">
											<c:if test="${item.lockData == null }">
												<smvc:checkbox path="selected" value="${item.note.guid}" title="Check box if you want to delete this form" />
											</c:if>
										</sec:authorize></td>
									</tr>

									<c:set var="count" value="${count+1}"></c:set>

								</c:if>
							</c:forEach>
						<c:if test="${empty patientHistoryItems }">
							<tr>
								<td colspan="6">There are no forms to display.</td>
							</tr>
						</c:if>
						<c:if test="${not empty patientHistoryItems }">
							<tr>
								<td colspan="6"><c:out value="${patientHistoryItems.size() }"/> items</td>
							</tr>
						</c:if>
							<tr>
							<td colspan="5" align="center">
								<c:if test="${patientHistoryForm.filterFormDefinition != null }">
									<c:if test="${patientHistoryForm.filterFormDefinition.liveStatus != 2}">
										<!-- not required for spire. hence, hidden -->
										<input type="button" name="createForm"
											value='Create new <c:out value="${patientHistoryForm.filterFormDefinition.label}"/> form'
											onclick="document.location='consultationNote.form?filterFormId=<c:out value="${patientHistoryForm.filterFormDefinition.id}"/>&mode=CREATE'" />
											&nbsp;
									</c:if>
								</c:if>
							</td>
							<td>
								<c:if test="${not empty patientHistoryItems }">
									<sec:authorize access="hasAnyRole('ADMIN')">
										<input type="submit" name="delete" value="Delete"
											onclick="deletePatientHistory()"
											title="Delete selected forms" />
									</sec:authorize>
								</c:if>
							</td>
						</tr>
					</table>

					<br/>

					<table width="100%" style="background-color: #EBF5FF; border: 1px lightgrey solid;">
						<tr>
							<td align="center"  height="50pt">
								Select Pathology results for this patient: <input type="button" name="Pathology Results" value="Pathology Results" onclick="getPathologyFeeds();" title="Pathology Results"/>
								</td>
							</tr>
						</table>
				</div>
				</div>
			</td>
		</tr>

	</table></td></tr>
</table>
</smvc:form>
