<%@page import="com.ardeo.emdt.EMDTApplication"%>
<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<jsp:useBean id="helper" scope="page" class="com.ardeo.autoforms.FileUploadDispatcher" />
<script src="../js/jquery/jquery-3.7.1.min.js"></script>
<script src="../js/AJAXSupport.js" type="text/javascript"></script>


<script language="javascript">	
   $(document).ready(function(){
		 loadPacsOnLoad();
	});
	function loadPacsOnLoad(){
		var loadImage = GetURLParameter('loadImage');
		if(loadImage!=null && loadImage=='imgStatusTracking'){
			var dateOfScan = GetURLParameter('dateOfScan');
			var hospitalNumber = GetURLParameter('hospitalNumber');
			var accessionNo = GetURLParameter('accessionNo');
			callPacsRequestDirect(loadImage,dateOfScan,hospitalNumber,accessionNo);
		}
	}
	function openReport(id){		
		window.open("../referrals/imgStatusTracking.form?fileName=Report&noteId="+id,"Browse","width=740,height=650,toolbar=0,resizable=1,scrollbars=1");
	}
</script>
<c:set var="imageFormName"/>
<c:if test="${emdt.currentCareSpell.referralsDefinition ne null}">
<c:forEach var="formSummaryMapOrder"
	items="${emdt.currentCareSpell.referralsDefinition.formSummaryMapOrder}">
	<c:if
		test="${formSummaryMapOrder.groupName eq 'Images and Reports' }">
		<c:if test="${imageFormName eq null or imageFormName eq ''}">
			<c:set var="imageFormName"  value="${formSummaryMapOrder.formOrSummaryName}"/>
		</c:if>
	</c:if>
</c:forEach>
</c:if>
	
<tiles:insertTemplate template ="referralsDetailsTopBar.jsp" ></tiles:insertTemplate>
<c:choose>
	<c:when
		test="${((webapp.roleManagement)  and ('ReferAPatient' ne parentScreen)) or  ('FindExisting' eq parentScreen)}">
		<div class="menuSubHolder"><br />
		<div class="menuButtonRect green">
			<a href="../referrals/redirectToParentScreen.form">Display Search<br />Listing</a>
		</div>
		<div class="menuButtonRect green">
			 <a href="../referrals/viewPatient.form">Click Here For<br />Patient Details</a>
		</div>
		<div class="menuButtonRect green">
		 <a href="../referrals/consultationNote.form?formName=<c:out value="${imageFormName}"/>&filterFormId=<c:out value="${consultationNoteForm.view.form.id}"/>">
		Edit Images and Reports
		</a>
		</div>
		
		<div class="menuButtonRect green">
			<a href="#" onclick="openPacsParameterWindow();">Find in PACS</a>
		</div>
		
		<div class="menuButtonRect green">
			 <a href="../referrals/consultationNote.form?formName=Response">
		
		
			<c:if
				test="${(0 eq emdt.currentCareSpell.referralDetails.referralStatus)or (1 eq emdt.currentCareSpell.referralDetails.referralStatus)}">
				<div class="menuButtonRect green">
					<span>Create/Edit Response<br />
				To Referrer</span>
				</div>
			</c:if> 
			<c:if
				test="${(2 eq emdt.currentCareSpell.referralDetails.referralStatus) or (3 eq emdt.currentCareSpell.referralDetails.referralStatus)}">
				<span>View Response<br />
				To Referrer</span>
			</c:if> <c:if
				test="${emdt.currentCareSpell.referralDetails.referralStatus eq null}">
				<span>View Response<br />
				To Referrer</span>
			</c:if>
			</a>
		</div>
	</c:when>
	<c:otherwise>
	</c:otherwise>
</c:choose>

<smvc:form name="imgStatusTracking" modelAttribute="imagingStatusListDTO"
	method="post" onsubmit="return submitTheForm();" action="../referrals/imgStatusTracking.form">
<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<smvc:errors></smvc:errors>&nbsp;
</smvc:form>

	<div class="rowSpacer">&nbsp;</div>
	
	
	<div id="noDataFoundID" align="center" style="display: none; width: 100%;">
		<span class="warning">No matching image studies were found in PACS</span>
	</div>
	
	
	<div class="rowSpacer">&nbsp;</div>

	<div class="largeContainer" style="clear: both;">
	<c:choose>
		<c:when
			test="${requestScope.successMsg != null || requestScope.errorMsg != null}">
			<c:if test="${requestScope.successMsg != null}">
				<div class="message"><img
					src="../images/16x16/information.png" alt="i" />
				<c:out value="${requestScope.successMsg}"/></div>
			</c:if>
			<c:if test="${requestScope.errorMsg != null}">
				<span class="error"><c:out value="${requestScope.errorMsg}"/></span>
			</c:if>
		</c:when>
		<c:otherwise>
			<c:set var="msg" value='<%=request.getParameter("successMsg")%>' />
			<c:if test="${(null ne msg) and ('null' ne msg)}">
				<div class="message"><img
					src="../images/16x16/information.png" alt="i" /> <c:out value="${msg}"/></div>
			</c:if>
		</c:otherwise>
	</c:choose>

	<div class="error"><spring:bind path="imagingStatusListDTO.*">
		<c:if test="${not empty status.errorMessages}">
			<c:forEach items="${status.errorMessages}" var="error">
				<img src="../images/16x16/error_white_bg.png" alt="e" />
				<c:out value="${error}" escapeXml="false" />
				<br />
			</c:forEach>
		</c:if>
	</spring:bind></div>


	<div class="rowDiv">
	<h3>Requests for Image Studies</h3>
	<table border="1" width="100%" height="100%" class="table" cellpadding="2">
		<tr class="tableHeader" height="100%">
			<th class="tableColHeader">Description</th>
			<th class="tableColHeader">Study Date</th>
			<th class="tableColHeader">Original Accession No.</th>
			<th class="tableColHeader">View Reports</th>
			<th class="tableColHeader">Image Request Status</th>
			<c:if test="${webapp.centralHubHospital}">
				<th class="tableColHeader">Change<br/>Status</th>
			</c:if>
			<th class="tableColHeader">View<br/>Log</th>			
			<c:if test="${webapp.centralHubHospital}">
				<th class="tableColHeader">Show in PACS</th>
			</c:if>
		</tr>

		<c:forEach items="${imagingStatusListDTO.listImagingStatus}"
			var="imgStatus" varStatus="s1">
			<c:set var="rowClass"
				value="${s1.count % 2 == 0 ? 'evenRow' : 'oddRow'}" />
			<tr align="center" class="<c:out value="${rowClass}"/>" style="height: 40px">
				<td align="left"><c:out value="${imgStatus.description}"/></td>
				<td><fmt:formatDate value="${imgStatus.dateOfScan}"
					pattern="dd MMM yyyy" /></td>
				<td><c:out value="${imgStatus.accessionNum}"/></td>
					<td align="left"><c:forEach items="${imgStatus.reportNames}"
							var="reports">

							<%
								int dotIndex = ((String) pageContext.getAttribute("reports")).lastIndexOf(".");
							%>
							<c:set var="dotIndex" value="<%=dotIndex%>" />
							<c:set var="displayFile"
								value="${fn:substring(reports, 0, dotIndex-21)}" />
							<c:set var="displayExt"
								value="${fn:substring(reports, dotIndex, fn:length(reports))}" />
							<%
								int slashIndex = ((String) pageContext.getAttribute("displayFile"))
												.lastIndexOf(helper.FILE_PATH_SEPERATOR);
							%>
							<c:set var="slashIndex" value="<%=slashIndex%>" />
							<c:set var="displayFile"
								value='<c:out value="${fn:substring(displayFile, slashIndex+1, fn:length(displayFile))}"/>' />
							<img src="../images/16x16/report.png" alt="Report" />&nbsp;<a
								href="?fileName=<c:out value="${reports}"/>"> <c:out
									value="${displayFile}"></c:out> <c:out value="${displayExt}"></c:out></a>
							<br />

						</c:forEach> <a
						href="javascript:openReport('<c:out value="${imgStatus.noteGuid}"/>');"><img
							src="../images/16x16/report.png" alt="Report" />&nbsp;Report</a></td>


					<c:if test="${0 eq imgStatus.status}">	<!--  image requested -->
					<c:choose>
						<c:when
							test="${(imgStatus.maxHours ne null and imgStatus.elapsedTime ne null) and (imgStatus.maxHours le imgStatus.elapsedTime)}">
							<td style="background-color: #FFFFCC" align="left">
								<b><c:out value="${imgStatus.statusDescription}"/>&nbsp;&nbsp;<fmt:formatDate
								value="${imgStatus.requestedDate}" pattern="dd MMM yyyy" /> 
								</b>
								<br/>
								<br/>							
								<img src="../images/16x16/error.png" alt="Warning" />&nbsp; Warning: This study was expected to have arrived by now.<br/>This may indicate a problem with the image request.
							</td>
						</c:when>
						<c:otherwise>
							<td align="left"><b><c:out value="${imgStatus.statusDescription}"/>&nbsp;&nbsp;<fmt:formatDate
								value="${imgStatus.requestedDate}" pattern="dd MMM yyyy" /> </b></td>
						</c:otherwise>
					</c:choose>
				</c:if>

				<c:if test="${1 eq imgStatus.status}">	<!-- image request in progress -->
					<c:choose>
						<c:when
							test="${(imgStatus.maxHours ne null and imgStatus.elapsedTime ne null) and (imgStatus.maxHours le imgStatus.elapsedTime)}">
							<td style="background-color: #FFFFCC" align="left"><b><c:out value="${imgStatus.statusDescription}"/>&nbsp;&nbsp;<fmt:formatDate
								value="${imgStatus.requestedDate}" pattern="dd MMM yyyy" /> </b>
								<br/>
								<br/>							
								<img src="../images/16x16/error.png" alt="Warning" />&nbsp; Warning: This study was expected to have arrived by now.<br/>This may indicate a problem with the image request.
								</td>
						</c:when>
						<c:otherwise>
							<td align="left"><b><c:out value="${imgStatus.statusDescription}"/>&nbsp;&nbsp;<fmt:formatDate
								value="${imgStatus.requestedDate}" pattern="dd MMM yyyy" /></b></td>
						</c:otherwise>
					</c:choose>
				</c:if>

				<c:if test="${2 eq imgStatus.status}">	<!-- image request completed -->
					<td style="background-color: #99FF99" align="left"><img
						src="../images/16x16/tick.png" alt="Error" />&nbsp;<b><c:out value="${imgStatus.statusDescription}"/></b>
					</td>
				</c:if>

				<c:if test="${(3 eq imgStatus.status) || (4 eq imgStatus.status) }"> <!-- image request failed or cancelled -->
					<td style="background-color: #FFCCCC" align="left"><img
						src="../images/16x16/cross.png" alt="Error" />&nbsp;<b><c:out value="${imgStatus.statusDescription}"/></b>
					<c:if test="${webapp.roleReferrals}">
						<a style="text-decoration: underline"
							href="../referrals/requestResendImages.form?noteId=<c:out value="${imgStatus.noteId}"/>">Resend
						Images</a>
					</c:if></td>
				</c:if>
				<c:choose>
					<c:when
						test="${(null eq imgStatus.status) and (webapp.roleReferrals)}">
						<td align="left"><img src="../images/16x16/error.png"
							alt="Warning" />&nbsp;<b><c:out value="${imgStatus.statusDescription}"/></b><br />
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
							style="text-decoration: underline"
							href="../referrals/requestResendImages.form?noteId=<c:out value="${imgStatus.noteId}"/>">Send
						Images</a></td>
					</c:when>
					<c:otherwise>
						<c:if test="${(null eq imgStatus.status)}">
							<td align="left"><img src="../images/16x16/error.png"
								alt="Warning" />&nbsp;<b><c:out value="${imgStatus.statusDescription}"/></b></td>
						</c:if>
					</c:otherwise>
				</c:choose>

				<!--  show link to modify status if Hub hospital -->

				<c:if test="${webapp.centralHubHospital}">
					<td>
						<a href="../referrals/imgStatusUpdate.form?conNote=<c:out value="${imgStatus.noteGuid}"/>" title="Modify image status"><img src="../images/16x16/cog.png" border="0" alt="settings icon"/></a>
					</td>
				</c:if>
				
				<td align="center"><a
					title="Click here to view image transfer log of <c:out value="${imgStatus.accessionNum}"/>"
					href="../referrals/imageTransferLog.form?noteId=<c:out value="${imgStatus.noteId}"/>&accNum=<c:out value="${imgStatus.accessionNum}"/>"><img
					src="../images/16x16/information.png" alt="Error" /></a>
				</td>
				<c:if test="${webapp.centralHubHospital}">
					<td><c:choose>
						<c:when test="${2 eq imgStatus.status}">
							<div id="Div<c:out value="${imgStatus.accessionNum}"/>ID1" style="display: block">
									<%-- output a link which will show the series info from PACS via an Ajax call --%>
									<a href="#" id="A<c:out value="${imgStatus.accessionNum}"/>"/>ID1"
										onclick="callPacsRequestDirect('imgStatusTracking', '<fmt:formatDate value="${imgStatus.dateOfScan}" pattern="dd/MM/yyyy" />', '<%=((EMDTApplication)session.getAttribute("emdt")).getPatient().getHospitalNumber()%>', '<c:out value="${imgStatus.accessionNum}"/>"/>', false);"><img
										src="../referrals/images/viewIcon.png"
										title="Show Image Study details in PACS" /></a>
							</div>
						</c:when>
						<c:otherwise>
								n/a
							</c:otherwise>
					</c:choose></td>
				</c:if>

			</tr>
		</c:forEach>
		<c:if test="${imagingStatusListDTO.listImagingStatus ne null && imagingStatusListDTO.listImagingStatus.size()>0}">
			<tr>
			<td>
			<a href="../referrals/imgStatusTracking.form?CheckPacs=true" title="Check any in-progress image requests against PACS">
			   Check PACS 
			  </a></td>
			</tr>	
		</c:if>
	</table>
	</div>

	<br>
	<div class="rowDiv" id="studyDetails" style="display: none;">
		<h3>Image Studies in PACS</h3>
		<table border="1" bordercolor="#b4b4b4" width="100%"
			id="imgStatusTrackingTable">
			<tr class="tableHeader">
				<th class="tableColHeader" width="20%">Patient Name</th>
				<th class="tableColHeader" width="8%">Date of Birth</th>
				<th class="tableColHeader" width="8%">Study Date</th>
				<th class="tableColHeader" width="8%">Accession No.</th>
				<th class="tableColHeader" width="6%">Modality</th>
				<th class="tableColHeader" width="30%">Study Description</th>
				<th class="tableColHeader" width="6%">No. of Images</th>
				<th class="tableColHeader" width="15%">Series</th>
			</tr>
	
		</table>
	</div>

	<div class="rowDiv" id="seriesDetails" style="display: none;">
		<h3>Image Series</h3>
		<table border="1" bordercolor="#b4b4b4" width="100%" id="seriesTable">
		<tr class="tableHeader">
			<th class="tableColHeader" width="20%">Patient Name</th>
			<th class="tableColHeader" width="8%">Date of Birth</th>
			<th class="tableColHeader" width="8%">Study Date</th>
			<th class="tableColHeader" width="8%">Accession No.</th>
			<th class="tableColHeader" width="6%">Modality</th>
			<th class="tableColHeader" width="30%">Series Description</th>
			<th class="tableColHeader" width="6%">No. of Images</th>
		</tr>

	</table>
	</div>

	</div>
	<%@ include file="pacsPopup.jsp"%>
	<%@ include file="progressPopup.jsp"%>
