<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<br/>
<%
    com.ardeo.emdt.MeetingPatientListHelper mplHelper = new com.ardeo.emdt.MeetingPatientListHelper();
    mplHelper.setPage(pageContext);
    mplHelper.setSortProperty(request.getParameter("sortCol"));
    mplHelper.setSortOrder(request.getParameter("sortOrder"));
    String resultPartParam = request.getParameter("resultPart");
    mplHelper.setResultPart(resultPartParam == null ? 1 : Integer.parseInt(resultPartParam));
    pageContext.setAttribute("mplHelper", mplHelper);
%>
<c:set var="resultPart" value="${mplHelper.resultPart}"/>
<c:set var="resultInToParts" value="${mplHelper.resultInToParts}"/>
<c:set var="patients" value="${mplHelper.patientList}"/>
<script>
	function changePageSize(el) {
		document.location = 'addMeetingPatients.form?pageSize=' + el.options[el.selectedIndex].value;
	}
	function deletePatient(patientid){
		var r = confirm('Are you sure you want to remove patient from meeting?');
		if(r){
			setPatientAction('removePatientFromMeeting');
			document.getElementById('patientToDelete').value=patientid;
			document.getElementById('meetingPatientForm').submit();
		}
	}
	function setPatientAction(actionRequired){
		document.getElementById('patientAction').value=actionRequired;
	}
</script>
<br/>

<table width="100%">
	<tr>
		<td align="center" valign="top">
			<smvc:form modelAttribute="meetingPatientForm" id="meetingPatientForm" action="../meetings/addMeetingPatients.form">
			<input type="hidden" name="page"  value="Patients">
			<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
			 <c:choose>
				<c:when test="${param.resultPart eq null}">
					<input type="hidden" name="resultPart"  value="1">
				</c:when>
				<c:otherwise>
					<input type="hidden" name="resultPart" value="<c:out value="${param.resultPart}"/>">
				</c:otherwise>
			</c:choose>
			<smvc:hidden path="action" id="actionRequired"/>
			<smvc:hidden path="patientAction" id="patientAction"/>
			<table width="60%"><tr><td><tags:table title="Patients in Meeting">
				<table class="table">
					<tr>
						<th>Patient Name</th>
						<th>Date Of Birth</th>
						<th>Hospital Number</th>
						<th>Priority</th>
						<th>Delete</th>
						
					</tr>
					<smvc:hidden path="patientToDelete" id="patientToDelete"/>
					<c:forEach var="patient" items="${emdt.currentMeeting.patientsInMeeting}" varStatus="s">
						<tr>
							<td><c:out value="${patient.surname }"/>, <c:out value="${patient.forename }"/></td>
							<td align="center"><fmt:formatDate value="${patient.dateOfBirth}" pattern="dd MMM yyyy"/></td>
							<td align="center"><c:out value="${patient.hospitalNumber}"/></td>
							<td><webapp:listValue domain="EMDT" listName="PatientPriority" key="${patient.priority}" /></td>
							<td align="center"><img src="../images/delete.png" border="0" alt="Delete" onmouseover="this.style.cursor = cursorPointer;"  <c:if test='${mplHelper.locked eq false }'> onclick="deletePatient('<c:out value="${patient.guid}"/>')"  </c:if>/>
							<c:if test="${patient.deathIndicator }"><img src="../images/16x16/warning.png" title="Patient is Deceased"/></c:if>
							</td>
						</tr>
					</c:forEach>
				</table>
			</tags:table></td></tr></table>
			
			<br/>
			
			<table width="500px">
				<tr>
					<td></td>
					<td>
					 	<tiles:insertTemplate template ="/referrals/tiles/meetingPatientSearchBox.jsp"/>
					</td>
				</tr>
			</table>

		<c:if test="${fn:length(patients) >0}" >
			<c:if test="${mplHelper.sortProperty eq 'selectedPatients'}">
				<c:set var="sortOrderBySelectedPatients" value="&sortCol=selectedPatients"></c:set>
			</c:if>
			
			<%--
			<c:if test="${resultInToParts>1}">
				<div class="message"><c:out value="${patientsInMeetingMaxMessage}&nbsp;
					<c:if test="${resultPart ne 1}">
						<img src="../images/backwardbutton.png" title="click to fetch previous set of patients" onclick="location.href='addMeetingPatients.form?resultPart=<c:out value="${resultPart-1}&action=paginationAction'" />&nbsp;
					</c:if>
					<c:if test="${resultPart ne resultInToParts}">
						<img src="../images/forwardbutton.png" title="click to fetch next set of patients" onclick="location.href='addMeetingPatients.form?resultPart=<c:out value="${resultPart+1}&action=paginationAction'" />
					</c:if>
				</div>
			</c:if>
			 --%>
			<table width="90%">
				<tr>
					<td>
						<tags:table title="Select Patients for Discussion">
							<c:if test="${requestScope.successMsg ne null}">
								<span class="info">	<c:out value="${requestScope.successMsg}"/></span>
							</c:if>
							
							<%@ include file="meetingPatientsPagination.jsp"%>
							<table class="table">
								<tr>
									<th><a href="addMeetingPatients.form?sortCol=selectedPatients&sortOrder=desc&action=paginationAction">Select</a></th>
									<th>
										<c:choose>
											<c:when test="${param.sortCol == 'surname' && param.sortOrder != 'desc' }">
												<a href="addMeetingPatients.form?sortCol=surname&sortOrder=desc&action=paginationAction">Surname</a>
											</c:when>
											<c:otherwise>
												<a href="addMeetingPatients.form?sortCol=surname&sortOrder=asc&action=paginationAction">Surname</a>
											</c:otherwise>
										</c:choose>
									</th>
									<th>
										<c:choose>
											<c:when test="${param.sortCol == 'forename' && param.sortOrder != 'desc' }">
												<a href="addMeetingPatients.form?sortCol=forename&sortOrder=desc&action=paginationAction">Forename</a>
											</c:when>
											<c:otherwise>
												<a href="addMeetingPatients.form?sortCol=forename&sortOrder=asc&action=paginationAction">Forename</a>
											</c:otherwise>
										</c:choose>
									</th>
									<th>
										<c:choose>
											<c:when test="${param.sortCol == 'dateOfBirth' && param.sortOrder != 'desc' }">
												<a href="addMeetingPatients.form?sortCol=dateOfBirth&sortOrder=desc&action=paginationAction">Age</a>
											</c:when>
											<c:otherwise>
												<a href="addMeetingPatients.form?sortCol=dateOfBirth&sortOrder=asc&action=paginationAction">Age</a>
											</c:otherwise>
										</c:choose>
									</th>
									<th>
										<c:choose>
											<c:when test="${param.sortCol == 'nhsNumber' && param.sortOrder != 'desc' }">
												<a href="addMeetingPatients.form?sortCol=nhsNumber&sortOrder=desc&action=paginationAction">NHS Number</a>
											</c:when>
											<c:otherwise>
												<a href="addMeetingPatients.form?sortCol=nhsNumber&sortOrder=asc&action=paginationAction">NHS Number</a>
											</c:otherwise>
										</c:choose>
									</th>
									<th>
										<c:choose>
											<c:when test="${param.sortCol == 'hospitalNumber' && param.sortOrder != 'desc' }">
												<a href="addMeetingPatients.form?sortCol=hospitalNumber&sortOrder=desc&action=paginationAction">Hospital Number</a>
											</c:when>
											<c:otherwise>
												<a href="addMeetingPatients.form?sortCol=hospitalNumber&sortOrder=asc&action=paginationAction">Hospital Number</a>
											</c:otherwise>
										</c:choose>
									</th>
									<th><div class="textofhyperlinkcolor" >Last MDT </div></th>
									<th><div class="textofhyperlinkcolor" >Primary Clinician </div></th>
									<th>
										<c:choose>
											<c:when test="${param.sortCol == 'priority' && param.sortOrder != 'desc' }">
												<a href="addMeetingPatients.form?sortCol=priority&sortOrder=desc&action=paginationAction">Priority</a>
											</c:when>
											<c:otherwise>
												<a href="addMeetingPatients.form?sortCol=priority&sortOrder=asc&action=paginationAction">Priority</a>
											</c:otherwise>
										</c:choose>
									</th>
								</tr>
								<c:forEach var="patient" items="${patients}" varStatus="s" begin="${changePatientsPaginationData.startOfPageIndex}" end="${changePatientsPaginationData.endOfPageIndex}">
									<c:set var="deceasedClass" value="" />
									<c:if test="${patient.patient_deathIndicator}">
										<c:set var="deceasedClass" value="patientDeceasedSearchResult" />
									</c:if>
									<tr class='<c:out value="${s.count%2 == 0 ? 'odd' : 'even'}"/>Row <c:out value="${deceasedClass}"/>'>
										<td align="center">
											<input type="hidden" name="patientIdsDisplayed" value="<c:out value="${patient.patient_guid }"/>"/>
											<c:if test="${mplHelper.locked eq false}">
												<smvc:checkbox path="selected" value="${patient.patient_guid }" title="Check box to include in meeting" />
											</c:if>
											<c:if test="${mplHelper.locked eq false}">
												<!-- should say Yes or No here :-) -->
											</c:if>
										</td>
										<td>
											<a href='../emdt/viewPatient.form?patientId=<c:out value="${patient.patient_guid }"/>' title="Click to view Patient"><c:out value="${patient.patient_surname }"/></a>
											<c:if test="${patient.patient_deathIndicator}">
												<img src="../images/16x16/warning.png" title="Patient is Deceased">
											</c:if>
										</td>
										<td>
											<a href='../emdt/viewPatient.form?patientId=<c:out value="${patient.patient_guid }"/>' title="Click to view Patient"><c:out value="${patient.patient_forename }"/></a>
										</td>
										<td align="right"><c:out value="${patient.currentAge}"/></td>
										<td align="right"><c:out value="${patient.patient_nhsNumber}"/></td>
										<td align="right"><c:out value="${patient.patient_hospitalNumber}"/></td>
										<td align="center">
											<fmt:formatDate value="${patient.lastDiscussedAtMeeting}" pattern="dd MMM yyyy"/>&nbsp;
										</td>
										<td ><c:out value="${webapp.userNameNameMap[patient.primaryClinicianInCSMatchesMeetingTG]}"/></td>
										<td><webapp:listValue domain="EMDT" listName="PatientPriority" key="${patient.patient_priority}" /></td>
									</tr>
								</c:forEach>
								<tr>
									<td colspan="3">
										<c:if test="${mplHelper.locked ne true}">
											<input type="submit" value="Add Patients" onclick="setPatientAction('addPatientsToMeeting')"/>
										</c:if>
										<c:if test="${mplHelper.locked eq true}">
											<span class="info">This meeting is locked: changes are not permitted.</span>
										</c:if>
									</td>
								</tr>
							</table>
						</tags:table>
			
					</td>
				</tr>
			</table>
			<br/>
		</c:if>
		</smvc:form>
		<c:if test="${fn:length(patients) == 0}" >
			<span class="info">Your search returned no results.</span>
		</c:if>
	
		<br/>
		
		<div class="meetingNavigationPanel" style="width:300px;">
		
			<c:if test="${emdt.currentMeeting != null}">
				<a href="resumeMeeting.form">Return to Current Meeting <img src="../images/16x16/arrow_undo.png" /> </a> <br/>
			</c:if>
			 <br/>
			<a href="setMeetingFilter.form?filterId=0">Select Another Meeting <img src="../images/16x16/bullet_go.png" /> </a> 
			<br/>
			
		</div>
		<br/>
	
	</td>
	</tr>
</table>
