<%@page import="java.util.List"%>
<%@ include file="/includes.jsp" %>
<tiles:importAttribute />
<jsp:useBean id="helper" scope="page" class="com.ardeo.autoforms.FileUploadDispatcher" />
<script>
	$(document).ready( function() {

		// toggle form display by clicking show/hide link
		$(".formDisplayToggler").click(	function() {
			$(this).parent().parent().children(".summaryPagesContainer").toggle(200);
		});

		// toggle form display by clicking title
		$(".summaryFormTitle").click( function() {
			$(this).parent().parent().children(".summaryPagesContainer").toggle(200);
		});
  
		// toggle all form display
  		$(".showAllToggler").click( function() {
	    	$(".summaryPagesContainer").show(200);
			return false;
	  	});
	});
</script>

<table width="100%">
	<tr>
		<td>
			<div style="width: 100%;" align="right" class="noprint">
				<div class="showAllToggler">
				<a href="#" >Show all <img src="../images/add.png" alt="+" title="Toggle all forms display"/></a>
				</div>
			</div>
			
			<c:forEach var="formMap" items="${summary.formsMap}">
			<div class="allFormsContainer">
				<div class="summaryFormContainer">
					<div class="summaryFormHeader">
						<span class="summaryFormType">&nbsp;Form</span>
							<span class="summaryFormTitle"><c:out value="${formMap.key }"/></span>
					</div>
					<c:forEach var="form" items="${formMap.value}" varStatus="s">
						<c:set var="formHasData" value="false" />
						<c:set var="hrefToAppend"/>
						<c:choose>
							<c:when test="${fn:contains(pageContext.request.servletPath, 'referrals')}">
								<c:set var="hrefToAppend" value="../referrals/"/>
							</c:when>
							<c:otherwise>
								<c:set var="hrefToAppend" value="../emdt/"/>
							</c:otherwise>
						</c:choose>
						<div class="summaryPagesContainer">
						<c:if test='${param["readonly"] ne "1" }'>
						
							<c:if test="${form.consultationNoteId == null }">
								<span class="tools">
										&nbsp;<a href="<c:out value="${hrefToAppend}"/>consultationNote.form?filterFormId=<c:out value="${form.formId }"/>&amp;mode=CREATE&amp;details=<c:out value="${cnDetails }"/>&amp;forwardOnError=<c:out value="${forwardOnError}"/>"> <img src="../images/16x16/page_white_add.png" alt=""/> Create new form</a>
								</span>
							</c:if>
						
							<!--  -->
							<c:if	test="${form.consultationNoteId != null }">
								<span class="summaryFormCount">Form <c:out value="${s.count-1}"/>:</span>
								<span class="tools">
										&nbsp;<a href="<c:out value="${hrefToAppend}"/>consultationNote.form?conNoteId=<c:out value="${form.consultationNoteGuid }"/>"> <img src="../images/16x16/page_white_edit.png" alt=""/> Edit</a>&nbsp;
								</span>
							</c:if>
						</c:if> <%-- read only --%>
						<c:forEach var="page" items="${form.pages}">
							<div class="summaryPageContainer">
								<div class="summaryPageHeader">
									<span class="summaryPageType">Page</span>
									<span class="summaryPageTitle"><c:out value="${page.label }"/></span>
								</div>
								<c:forEach var="section" items="${page.sections }">
									<div class="summarySectionContainer">
										<div class="summarySectionHeader">
											<span class="summarySectionType">Section</span>
											<span class="summarySectionTitle"><c:out value="${section.label }"/></span>
										</div>
										<table class="summaryFieldTable">
											<c:forEach var="field" items="${section.fields }">

												<%-- check if any of the field values have a value and display if so --%>
												<c:set var="fieldHasData" value="false"/>
												<c:forEach var="value" items="${field.values }">
													<c:if test="${! empty value.code}">
														<c:set var="fieldHasData" value="true" />
													</c:if>
												</c:forEach>

												<c:if test="${ fieldHasData }">
													<tr class="summaryRow">
														<c:if test="${!empty field.values}">
															<c:set var="formHasData" value="true"/>
															<td class="summaryFieldTitle" ><c:out value="${field.label }"/></td>
															<td class="summaryFieldValue"><c:set var="displayAttach" value="false" />
																<c:forEach var="value" items="${field.values }">
																<c:choose>
																<c:when test="${field.type eq 9}">
																	<c:set var="noteId" value="${form.consultationNoteId}" />
																	<c:set var="fieldName" value="${field.name}" />
																	<c:set var="patientGuid" value="${emdt.patient.guid}"/>
																	<%
																		String fieldName = (String) pageContext.getAttribute("fieldName");
																		Integer noteId = (Integer) pageContext.getAttribute("noteId");
																		String patientGuid = (String) pageContext.getAttribute("patientGuid");
																		List<com.ardeo.entities.PatientDocumentMetaData> patientDocumentMetaDataList=helper.getFileNamesList(noteId,fieldName,patientGuid);
																		String  hasPatientDocument="false";
																		if(patientDocumentMetaDataList!=null && patientDocumentMetaDataList.size()>0)
																			hasPatientDocument="true";
																	%>
																	<c:set var="hasPatientDocument" value="<%=hasPatientDocument%>"/>
																	<c:if test="${hasPatientDocument eq 'true'}">
																	<c:forEach var="patientDocumentMetaData" items="<%=patientDocumentMetaDataList%>">
																		<c:set var="actualfileName" value="${patientDocumentMetaData.attachedDocument.name}" />
																		<c:set var="source" value="emdt"/>
																			<c:if test="${(null ne formView.sourceModule) and ('' ne formView.sourceModule)}">
																				<c:set var="source" value="${formView.sourceModule}"/>
																			</c:if>
																			<a onclick="window.open('../emdt/viewNoteFile.form?patientGuid=<c:out value="${patientGuid}"/>&amp;fileName=${fn:replace(actualfileName, ' ', '+')}', '_blank');" title="Click to View or download file">
																			<img src="../images/16x16/page.png" alt="Document" />
																			<%int dotIndex =  ((String)pageContext.getAttribute("actualfileName")).lastIndexOf(".");%>
																			<c:set var="dotIndex" value="<%=dotIndex%>"/>
																			<c:out value="${fn:substring(patientDocumentMetaData.attachedDocument.name, 0, dotIndex-21)}${fn:substring(patientDocumentMetaData.attachedDocument.name, dotIndex, fn:length(patientDocumentMetaData.attachedDocument.name))}"/>
																		</a>
																	</c:forEach>
																	</c:if>
																</c:when>
																<c:otherwise>
																	<%-- this hack is to avoid an issue with vertical alignment when PRE tags are rendered in PDFs.  I'm using a DIV instead and searching and replacing newlines with BR tags --%>
																	<c:set var="newline" value="<%='\n'%>" />
																	<div style="vertical-align: top;"><pre><c:out value="${value.code }"/></pre></div></c:otherwise>
																</c:choose>
																</c:forEach>
															</td>
														</c:if>
													</tr>
												</c:if> <%-- fieldHasData --%>

											</c:forEach>
										</table>
									</div>
								</c:forEach>
							</div>
						</c:forEach> <%-- form page --%>
					</div><%-- summaryPagesContainer --%>
					<div class="summaryFormisEmptyMessage">
						<c:if test="${!formHasData and s.last}">
							(this form contains no information)
						</c:if>
						<!-- empty comment -->
					</div>
					</c:forEach> <%-- form --%>
					<c:if test="${formHasData}">
						<div style="width: 100%;" align="right">
							<a  class="formDisplayToggler" >show / hide </a>
						</div>
					</c:if>
				</div>
			</div>
			
			</c:forEach> <%-- formMap --%>
		</td>
	</tr>
</table>