<%@page import="com.ardeo.entities.autoforms.ConsultationNote"%>
<%@page import="java.util.List"%>
<%@ include file="/includes.jsp" %>
<tiles:importAttribute/>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<jsp:useBean id="helper" scope="page" class="com.ardeo.autoforms.FileUploadDispatcher"/>
<table>
	<tr>
		<td>
			<c:if test="${'true' ne disabled and not consultationNoteForm.view.note.locked}">
				<c:set var="propertyName" value="fileValue(${fieldName})"></c:set>
				<input type="button" value="Add File..." onclick="openAttachFilePopupStruts('${propertyName}DivId'); document.getElementById('patientDocumentMetaDataselectedUser').value=${webapp.currentUser.id};" name="attachBtn" id="attachBtn"/><br /><br />
				
				<div id="<c:out value="${propertyName}"/>DivId" style="display: none;">
					File:
					<c:choose>
						<c:when test="${'true' ne disabled and consultationNoteForm.view.note.locked ne true }">
							<input type="file"  name="fileUpload[<c:out value="${fieldName}"/>]" size='<c:out value="${fieldWidth}"/>'  onchange="setFileSize(this);setUsedInSave();setTitle();"  id="my_file"/>
							<input type="button" name="attachBtn" value="Attach File and Finish" onclick="javascript:notefileUploader('<c:out value="${fieldName}"/>','<c:out value="${propertyName}"/>DivId');"/><br />					
						</c:when>
						<c:otherwise>
							<input type="file"  name="fileUpload[<c:out value="${fieldName}"/>]" size='<c:out value="${fieldWidth}"/>' onchange="setFileSize(this);setUsedInSave()" disabled="disabled"  id="my_file"/>
							<input type="button" name="attachBtn" id="attachBtn" value="Attach" />
						</c:otherwise>
					</c:choose>
					<input type="hidden" id="usedInSave" value="" />
					<br />
	
				</div>
			</c:if>
		</td>
	</tr>
	<tr>
		<td>
			<c:if test="${consultationNoteForm.view ne null}">
				<c:set var="note" value="${consultationNoteForm.view.note}"/>
				<c:set var="patientGuid" value="${emdt.patient.guid}"/>
				<%
				 	ConsultationNote note = (ConsultationNote)pageContext.getAttribute("note");
					Integer id =note.getId();
					String fieldName = (String) pageContext.getAttribute("fieldName");
					String patientGuid=  (String) pageContext.getAttribute("patientGuid");
					List<com.ardeo.entities.PatientDocumentMetaData> patientDocumentMetaDataList=helper.getFileNamesList(id,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'}"> 
					<table style="width: 100%;" id="attachedDocumentsTblId">
						<tbody>
							<tr><td colspan="3">Attached documents:</td></tr>
						<c:forEach var="patientDocumentMetaData" items="<%=patientDocumentMetaDataList%>">
							<tr style="height:25px">
								<td>
									<c:set var="actualfileName" value="${patientDocumentMetaData.attachedDocument.name}"></c:set>
									<a href="javascript:fileViewAction('<c:out value="${fieldName}"/>','${fn:replace(patientDocumentMetaData.attachedDocument.name, "\'", "\\\'" ) }');"  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>
								&#160;&#160;
								</td>
								<td>&#160;&#160;
									<c:out value="${patientDocumentMetaData.fileSize}"></c:out>&#160;&#160;
								</td>
								<td>&#160;&#160;
									<c:if test="${not consultationNoteForm.view.note.locked }">
									<%-- escape any embedded apostrophes --%>
									<a href="javascript:fileDeleteAction('<c:out value="${fieldName}"/>','${fn:replace(patientDocumentMetaData.attachedDocument.name, "\'", "\\\'" ) }');" title="Delete file..."><img
										src="../images/delete.png" alt="Delete" />Delete</a>
									</c:if>
								</td>
							</tr>
						</c:forEach>
						</tbody>
					</table>
				</c:if>
			</c:if>
			
			<smvc:hidden path="value[${fieldName}]"/> 
			<input type="hidden" name="fileName" id="fileName" value=""/> <!-- TO STORE PATIENT FILE NAME -->
		</td>
	</tr>
</table>