<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<script>
//<!--
	function update() {
		if(dispatch("reload")) {
			document.getElementById("cancel").value="Cancel";
			document.getElementById("form").submit();
		}
	}
	
	function addMatch() {
	
		if(dispatch("addMatch")) {
			document.getElementById("validationCheck").value = "addMatch";
			var f = document.getElementById("form");
			if(validateNotificationForm(f)) {
			    f.submit();
			}
		}
	}
	
	function addMethod() {
		if(dispatch("addMethod")) {
			document.getElementById("validationCheck").value = "addMethod";
			document.getElementById("form").submit();
		}
	}
	
	function removeMatch(id) {
		document.getElementById("removeMatchId").value = id;
		if(dispatch("removeMatch")) {
			document.getElementById("form").submit();
		}
	}
	
	function removeMethod(id) {
		document.getElementById("removeMethodId").value = id;
		if(dispatch("removeMethod")) {
			document.getElementById("form").submit();
		}
	}
//-->
</script>
<br/>
<sec:authorize access="hasRole('ADMIN')">
<smvc:form action="../emdt/editNotification.form" onsubmit="return validateNotificationForm(this);"  id="form" modelAttribute="notificationForm">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<input type="hidden" name="method" value="save" id="dispatchMethod"/>
	<input type="hidden" name="removeMatchId" id="removeMatchId"  value=""/>
	<input type="hidden" name="removeMethodId" id="removeMethodId" value=""/>
	<input type="hidden" name="org.apache.struts.taglib.html.CANCEL" id="cancel" value=""/>
	<smvc:hidden path="validationCheck" id="validationCheck"/>
	<c:if test="${message ne null}">
		<span class="info"><c:out value="${message}"/></span>
	</c:if>
	<c:if test="${errorMsg ne null}">
		<span class="error"><c:out value="${errorMsg}"/></span>
	</c:if>
	<table>
		<tr>
			<td>
			<div class="dialogBox">
			<div class="dialogTitle">Edit Notification</div>
				<div class="dialogContent">
			
					<table cellpadding="4">
						<tr>
							<td><br/><b>Description</b></td>
							<td><br/>
								<smvc:input path="notification.description" size="60"/>
							</td>
						</tr>
						<tr>
							<td colspan="2">
								<hr/>
								<b>Rule</b>
							</td>
						</tr>
						<tr>
							<td>When form</td>
							<td><smvc:select path="notification.formName" onchange="update()">
								<smvc:option value="">-- please select a form --</smvc:option>
									<c:forEach var="grp" items="${emdt.formDomain.children }">
										<optgroup label="<c:out value="${grp.label}"/>"> 
										<c:forEach var="form" items="${grp.children }">
												<smvc:option value="${form.path}"><c:out value="${form.label}"/></smvc:option>
											</c:forEach>
										</optgroup>
									</c:forEach>
								</smvc:select>
								 field(s):
						</tr>
						<tr>
							<td colspan=2>
								 <table width="100%" cellpadding="4" cellspacing="0" style="padding: 3 3 3 3; border:1px solid #B4B4B4;">
								 	<tr class="dialogTitle">
								 		<td>Field Name</td>
								 		<td>Condition</td>
								 		<td>Value</td>
								 		<td>&nbsp;</td>
								 	</tr>
								 	<c:choose>
								 		<c:when test="${empty notificationForm.notification.matches}">
									 		<tr>
									 			<td colspan="3">-- No fields selected --</td>
									 		</tr>
								 		</c:when>
								 		<c:otherwise>
								 			<c:forEach var="match" items="${notificationForm.notification.matches}" varStatus="i">
												<tr>
													<td width="1">
														<b><c:out value="${match.fieldName}"/></b>
													</td>
													<td width="1">
														<b>
															<c:choose>
																<c:when test="${match.op eq 0}">is equal to</c:when>
																<c:when test="${match.op eq 1}">does not equal</c:when>
																<c:when test="${match.op eq 2}">contains</c:when>
																<c:when test="${match.op eq 3}">does not contain</c:when>
																<c:when test="${match.op eq 4}">is empty</c:when>
																<c:when test="${match.op eq 5}">is not empty</c:when>
																<c:when test="${match.op eq 6}">is greater than</c:when>
																<c:when test="${match.op eq 7}">is less than</c:when>
															</c:choose>
														</b>
													</td>
													<td width="1">
														<b><c:out value="${match.value}"/></b>
													</td>
													<td>
														<input type="image" src="../images/delete.png" title="Remove field match condition"  value="Remove" onclick="removeMatch('<c:out value="${i.count-1}"/>');return false;"/>
													</td>
												</tr>
											</c:forEach>
								 		</c:otherwise>
								 	</c:choose>
								 
									
						
									<tr>
										<td width="1">
											<smvc:select path="newMatch.fieldName">
												<option value=""></option>
												<c:forEach var="fld" items="${notificationForm.formFields}">
													<smvc:option value="${fld.name}"><c:out value="${fld.label}"/></smvc:option>
												</c:forEach>
											</smvc:select>
										</td>
										<td width="1">
											<smvc:select path="newMatch.op">
												<smvc:option value="0">is equal to</smvc:option>
												<smvc:option value="1">does not equal</smvc:option>
												<smvc:option value="2">contains</smvc:option>
												<smvc:option value="3">does not contain</smvc:option>
												<smvc:option value="4">is empty</smvc:option>
												<smvc:option value="5">is not empty</smvc:option>
												<smvc:option value="6">is greater than</smvc:option>
												<smvc:option value="7">is less than</smvc:option> 
											</smvc:select>
										</td>
										<td width="1">
											<smvc:input path="newMatch.value" size="20"/>
										</td>
										<td>
											<input type="image" src="../images/add.png" title="Add field match condition"  value="Add" onclick="addMatch();return false;"/>
										</td>
									</tr>
								</table>
								<br/>
								
								<table width="100%" cellpadding="4" cellspacing="0" style="padding: 3 3 3 3; border:1px solid #B4B4B4; ">
									<tr class="dialogTitle">
										<td colspan="3">Send notification to:</td>
									</tr>
									<c:choose>
								 		<c:when test="${empty notificationForm.notification.methods}">
									 		<tr>
									 			<td colspan="3">-- No recipients selected --</td>
									 		</tr>
								 		</c:when>
								 		<c:otherwise>
								 			<c:forEach var="m" items="${notificationForm.notification.methods}" varStatus="i">
												<tr>
													<td>
														<b>
															<c:choose>
																<c:when test="${m.type eq 0}">E-Mail address</c:when>
																<c:when test="${m.type eq 1}">Fax number</c:when>
																<c:when test="${m.type eq 2}">SMS Number</c:when>
															</c:choose>
														</b>
													</td>
													<td width="1"><b><c:out value="${m.address}"/></b></td>
													<td>&nbsp;
														<input type="image" src="../images/delete.png" title="Remove message recipient" value="Remove" onclick="removeMethod('<c:out value="${m.address}"/>');return false;"/>
													</td>
												</tr>
											</c:forEach>
										</c:otherwise>
									</c:choose>
									<tr>
										<td width="1">
											<smvc:select path="newMethod.type">
												<smvc:option value="0">E-Mail address</smvc:option>
											</smvc:select>
										</td>
										<td width="1"><smvc:input path="newMethod.address" size="50"/></td>
										<td>&nbsp;<input type="image" src="../images/add.png" title="Add message recipient"  value="Add" onclick="addMethod();return false;"/></td>
									</tr>
								</table>
							</td>
						</tr>
						<tr>
							<td colspan="2">
								<hr/>
								<b>Message</b>
							</td>
						</tr>
						<tr>
							<td>
								Subject: 
							</td>
							<td>
								<smvc:input size="60" path="notification.message.subject"/>
							</td>
						</tr>
						<tr>
							<td valign="top">
								Body: 
							</td>
							<td>
								<smvc:textarea rows="6" cols="60" path="notification.message.body"/>
							</td>
						</tr>
						<tr>
                            <td valign="top">
                                Include Patient Summary: 
                            </td>
                            <td>
                            <smvc:select path="notification.patientSummaryId">
							<smvc:option value=""> -- No summary selected --</smvc:option>
							<c:forEach var="summary" items="${notificationForm.patientSummaries}">
								<smvc:option value="${summary.id}"><c:out value="${summary.title}"/></smvc:option>
							</c:forEach>
						  </smvc:select>
						</td>
						
						</tr>
						
					</table>
				</div>
				</div>
			</td>
		</tr>
		<tr>
			<td align="right"><br/>
			<input type="reset"/>&nbsp;<input type="submit" value="Save" onclick="return dispatch('save')"/>
			</td>
		</tr>
	</table>
</smvc:form></sec:authorize>

