<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<br />
<script type="text/javascript">
function submitForm(action,fileName){
	document.getElementById("actionRequired").value=action;
	document.getElementById("fileName").value=fileName;
	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>
<sec:authorize access="hasRole('ADMIN')">
<smvc:form action="webShareFiles.form?${_csrf.parameterName}=${_csrf.token}" method="post"	enctype="multipart/form-data" modelAttribute="fileUploadForm" id="patientfilesform">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<input type="hidden" name="actionRequired" id="actionRequired"></input>
	<input type="hidden" name="fileName" id="fileName"/>
	<c:if test="${not empty(successMsg)}">
		<span class="info">
			<c:out value="${successMsg}"/>
		</span>
	</c:if>
	<c:if test="${not empty(errorMsg)}">
		<span class="error">
			<c:out value="${errorMsg}"/>
		</span>
	</c:if>
	<fieldset style="width: 50%; text-align: left">
            <legend>File Sharing</legend>
           		<table>
           			<tr>
					<td colspan="2"><br/>File: <input type="file" name="file" size="45" onchange="setFileSize(this);"/>&#160;<input
						type="button" name="startPatientImport" value="Attach" onclick="submitForm('','');"/><br />
					<br /></td>
				</tr>
				<c:if test="${fn:length(files)>0 }">
				<tr>
				<td width="30px">&#160;</td>
				<td><table>
					<tr>
						<td colspan="4">Attached documents:</td>
					</tr>
				<c:forEach items="${files}" var="entry">
					<tr style="height:25px">
						<td>&#160;&#160;&#160;</td>
						<td>
							<c:set var="actualfileName" value="${entry.key.name}"></c:set>
							<a href="javascript:submitForm('fileView','<c:out value="${entry.key.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(entry.key.name, 0, dotIndex)}${fn:substring(entry.key.name, dotIndex, fn:length(entry.key.name))}"/>
							</a>
						&#160;&#160;
						</td>
						<td>
							<c:out value="${entry.value}"></c:out>
 						</td>
						<td>&#160;&#160;
							<a href="javascript:fileDeleteAction('<c:out value="${entry.key.name}"/>');" title="Delete file..."><img
								src="../images/delete.png" alt="Delete" />Delete</a>
						</td>
					</tr>
				</c:forEach>
				</table> </td></tr></c:if>
          	</table>
     </fieldset>
</smvc:form></sec:authorize>
