<%@ include file="/includes.jsp" %>
<jsp:useBean id="helper" scope="page" class="com.ardeo.autoforms.FileUploadDispatcher"/>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<link rel="stylesheet" href="../css/tables.css" type="text/css" />
<link rel="stylesheet" href="../js/jquery/datatables.min.css" type="text/css" />
<script src="../js/jquery/datatables.min.js"> </script>
<script src="../js/jquery/dataTablesPlugins/file-size.js"> </script>
<script src="../js/jquery/dataTablesPlugins/date-euro.js"> </script>
<br />
<script type="text/javascript">
function submitForm(action,fileName){
	document.getElementById("actionRequired").value=action;
	document.getElementById("fileName").value=fileName;
	if (action == '' && document.getElementById("my_file").value == '')		{
		alert("Please choose a file before continuing");
		return false;
	}
	document.getElementById("patientfilesform").submit();
}
function fileDeleteAction(fileNameSel) {
	if(confirm("Are you sure that you want to delete the file?")){
		document.getElementById("actionRequired").value = "fileDelete";
		document.getElementById("fileName").value=fileNameSel;
		document.getElementById("patientfilesform").submit();
	}
}

</script>
<script type="text/javascript">
// init datatable, set sorting order for column 5 to use facility provided by file-size.js plugin
	$(document).ready(function(){
		$('#attachedDocumentsTblId').DataTable( {
			columnDefs: [
			       { type: 'file-size', targets: 5 },
			       { type: 'date-euro', targets: 2 }
			     ],
			     order: [[ 2, "desc" ]]
			
		});
	});
 </script >
<smvc:form action="patientfiles.form?${_csrf.parameterName}=${_csrf.token}" method="post"	enctype="multipart/form-data" modelAttribute="patientFileUploadDto" id="patientfilesform" >
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
<%@ include file="attachFilePopup.jsp" %>
	
	<fieldset style="width: 80%; text-align: left">
		<legend>Patient Files</legend>
		
			<input type="hidden" name="actionRequired" id="actionRequired" />
			<input type="hidden" name="fileName" id="fileName" />
			
			<c:if test="${successMsg != null}">
				<div class="message">
					<img src="../images/16x16/information.png" alt="i" /> <c:out value="${successMsg}"/>
				</div>
			</c:if>
			<c:if test="${errorMsg != null}">
				<div class="error">
					<img src="../images/16x16/error.png" alt="i" /> <c:out value="${errorMsg}"/>
				</div>
			</c:if>
			<div id="fileAddDiv" style="margin-bottom: 10px; margin-top: 10px;">
				<button type="button" onclick="openAttachFilePopup();" style="padding: 10px;"><img src="../images/add.png"/> Add a file...</button>
			</div>
			
		
		<table style="width: 100%;" id="attachedDocumentsTblId" class="display">
			<thead>
			<tr>
				<th>Title</th>
				<th>Category</th>
				<th>Date</th>
				<th>Added By</th>
				<th>File Name</th>
				<th>Size</th>
				<th>Delete</th>
			</tr>
			</thead>
			<c:if test="${fn:length(patientFileUploadDto.patientDocumentMetaDataList) gt 0 }">
			<tbody>
			<c:forEach var="patientDocumentMetaData" items="${patientFileUploadDto.patientDocumentMetaDataList}">
				<tr style="height:25px">
					<td><c:out value="${patientDocumentMetaData.title}"/></td>
					<td><c:out value="${patientDocumentMetaData.category}"/></td>
					<td><fmt:formatDate value="${patientDocumentMetaData.uploadedOn}" pattern="dd/MM/yyyy HH:mm:ss"/></td>
					<td><c:out value="${patientDocumentMetaData.uploadedBy.name}"/></td>
					<td>
					<c:choose>
					<c:when test="${! empty patientDocumentMetaData.attachedDocument}">
						<c:set var="actualfileName" value="${patientDocumentMetaData.attachedDocument.name}"></c:set>
						<a href="javascript:submitForm('fileView','${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%>"/>
							<%int underScoreIndex =  ((String)pageContext.getAttribute("actualfileName")).indexOf("_Cnid");%>
							<c:set var="underScoreIndex" value="<%=underScoreIndex%>" />
							<c:if test="${underScoreIndex == -1}">
								<c:set var="underScoreIndex" value="${dotIndex-21}" />
							</c:if>
							<c:out value="${fn:substring(patientDocumentMetaData.attachedDocument.name, 0, underScoreIndex)}${fn:substring(patientDocumentMetaData.attachedDocument.name, dotIndex, fn:length(patientDocumentMetaData.attachedDocument.name))}"/>
						</a>
					</c:when>
					<c:otherwise>
						<span class="warning">File not found</span>
					</c:otherwise>
					</c:choose>
					&#160;&#160;
					</td>
					<td>
						<c:out value="${patientDocumentMetaData.fileSize}"></c:out>
						</td>
					<td>&#160;&#160;
					<c:if test="${! empty patientDocumentMetaData.attachedDocument}">
						<%-- escape any embedded apostrophes --%>
						<a href="javascript:fileDeleteAction('${fn:replace(patientDocumentMetaData.attachedDocument.name, "\'", "\\\'" ) }');" title="Delete file..."><img
							src="../images/delete.png" alt="Delete" />Delete</a>
					</c:if>
					</td>
				</tr>
			</c:forEach>
			</tbody>
			</c:if>
		</table>
	</fieldset>
	
	
</smvc:form>
