<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<script type="text/javascript">
function addPatientToMeeting(){
    fullScreenPleaseWait();
    document.getElementById('action').value="addToSelectedMeeting";
	document.getElementById('patientsToNextMeeting').submit();
}
function goBack(){
	location.href="viewPatient.form";
}
function getFutureMeetings(){
	var selectedspeciality=document.getElementById('selectedspeciality').value;
	var selectedHospital=document.getElementById('selectedHospital').value;
	var addingCurrentPatient=document.getElementById('addingCurrentPatient').value;
	var locationString="../nationalMDT/addCurrentPatientToNextMeeting.form?task=selectNextMeeting&hospId="+selectedHospital+"&selectedspeciality="+selectedspeciality;
	if(addingCurrentPatient == 'false'){
		var selected=[];
		var oArr = {};
		$("*[id]").each(function() {
		var id = $(this).attr('id');
	      	if (!oArr[id]) oArr[id] = true;
		});
	 	for (var key in oArr){
	 		if(key.indexOf('selectedPatientList')!=-1){
	 			var prop=document.getElementById(key).value;
				if(typeof(prop)!='undefined'){
					selected.push(document.getElementById(key).value);
	     		}
	  		}
		}
		locationString=locationString+"&isMultipleSelect=true&selected="+selected;
	}
	location.href=locationString
}
</script>
<!--  start -->
<tiles:insertTemplate template ="referralsDetailsTopBar.jsp" ></tiles:insertTemplate>
<br/>
<br/>
<div class="container">
	<sec:authorize access="hasRole('NMDT_COORD')">
			<tiles:insertTemplate template ="/nationalMDT/tiles/nationalMDTFormMenu.jsp" ></tiles:insertTemplate>
	</sec:authorize>
<br />
<c:if test="${emdt.patient.deathIndicator }"><span class="patientDeceasedMessage warning">Patient is Deceased</span></c:if>
<br />
<br />
<br />
<table class="dialogBox" width="100%" align="center">
	<tr>
		<td align="center">Hospital: <select id="selectedHospital" onchange="getFutureMeetings()">
				<option value="">Any</option>
				<c:forEach var="hospital" items="${webapp.hospMap}">
					<option value="<c:out value="${hospital.key}"/>" <c:if test="${hospital.key eq  patientsToNextMeetingForm.selectedHospital}">selected="selected"</c:if> > <c:out value="${hospital.value}"/></option>
				</c:forEach>
		</select>
		</td>
		<td align="center">Speciality:
			<webapp:namedList name="speciality" domain="EMDT" id="nl"/>
				<select id="selectedspeciality"  onchange="getFutureMeetings()">
					<option value="allSpeciality">Any</option>
					<c:forEach var="pair" items="${nl.pairIterator }">
					<option value="<c:out value="${pair.first}" />"   <c:if test="${pair.first eq  patientsToNextMeetingForm.selectedspeciality}">selected="selected"</c:if>  ><c:out value="${pair.second}" /></option>
					</c:forEach>
				</select>
		</td>
		<td><input type="button" value="filter" onclick="getFutureMeetings()"></td>
	</tr>
</table>
<br/><br/>
<webapp:namedList name="speciality" domain="EMDT" id="specialityNL"/>
<webapp:listToMap var="specialityMap" list="${specialityNL}"/>
<smvc:form name="patientsToNextMeeting" id="patientsToNextMeeting" modelAttribute="patientsToNextMeetingForm" method="post"  action="../nationalMDT/addCurrentPatientToNextMeeting.form">
<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
<div style="display: none;">
<smvc:hidden path="action" id="action"/>
<smvc:hidden path="addingCurrentPatient" id="addingCurrentPatient"/>
<smvc:hidden path="speciality"/>
<smvc:hidden path="selectedspeciality"/>
<smvc:hidden path="selectedHospital"/>
<c:forEach var="id" items="${patientsToNextMeetingForm.selected}" varStatus="s">
	<smvc:checkbox path="selected" value="${id}" id="selectedPatientList${s.count}"/>
</c:forEach>
</div>
<c:choose>
<c:when  test="${noSpecialtyErrMsg ne null }">
	<span class="error"><c:out value="${noSpecialtyErrMsg}"/><br/><br/>
		<input type="button" value="Back" onclick="goBack()">
	</span>
</c:when>
<c:otherwise>
<table id="dialogBox" class="dialogBox" width="100%" align="center" >
		<tr>
			<td>&nbsp;<br/></td>
		</tr>
	<c:if test="${actionResponse eq 'PatientAddedToMeeting'}">
		<tr>
			<td align="center"><span class="info"><c:out value="${PatientAddedToMeetingMessage}"/></span></td>
		</tr>
	</c:if>
	<tr ><td>&nbsp;<br/></td></tr>
	<tr align="center">
		<td><b>Add patient to meeting:</b>
			<smvc:select path="meetingId">
			<c:forEach var="meetingMap" items="${patientsToNextMeetingForm.futureMeetingMap}">
				<c:choose>
					<c:when test="${meetingMap.key.id ne null}">
						<c:set var="optionValue" value="${meetingMap.key.id}" />
					</c:when>
					<c:otherwise>
						<fmt:formatDate pattern="dd MMM yyyy" value="${meetingMap.key.date}" var="theDate"/>
						<c:set var="optionValue" value='scheduleId=${meetingMap.value}, date=${theDate}' />
					</c:otherwise>

				</c:choose>
				<smvc:option value="${optionValue}">
					<c:out value="${specialityMap[meetingMap.key.tumourGroup]}"/>&nbsp;meeting on
					<fmt:formatDate pattern="E dd MMM yyyy" value="${meetingMap.key.date}" />
					<fmt:formatDate pattern="HH:mm" value="${meetingMap.key.date}" />
					<c:if test="${meetingMap.key.hospital ne null }"> in <c:out value="${meetingMap.key.hospital.hospitalName}"/></c:if>
					<c:if test="${not empty meetingMap.key.location}">(<c:out value="${meetingMap.key.location}"/>)</c:if>
				</smvc:option>
			</c:forEach>
			</smvc:select>
			<input type="button" value="Add.." onclick="addPatientToMeeting();">
		</td>
	</tr>
	<tr ><td>&nbsp;<br/><br/></td></tr>
	

	<tr align="center">
		<td>
			<input type="button" value="Back" onclick="goBack()">
		</td>
	</tr>
	</table>
</c:otherwise>
</c:choose>
</smvc:form>
</div>