<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<webapp:namedList name="Speciality" domain="EMDT" id="primarySiteList" filterName="carespellNLFilter"/>
<webapp:namedList name="KeyWorker" domain="EMDT" id="keyworkers"/>

	<webapp:namedList name="PrimarySite" domain="EMDT" id="primarySiteList" filterName="carespellNLFilter"/>
	<webapp:namedList name="KeyWorker" domain="EMDT" id="keyworkers"/>

	<script type="javascript" >
		function selectCarespell(id) {
			document.location = "selectCarespell.form?carespellId=" + id;
		}
	</script>
	<c:set var="activeUsers" value="${webapp.userNameNameMap}"/>
	<smvc:form action="editCarespells.form" method="post">
		<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<table width="100%">

					<tr>

						<td>&#160;</td>

					</tr>

	</table>
		<fieldset class="nobg" style="width: 70%">
			<legend>Care spells</legend>
			
								<c:if test="${empty command.patient.careSpells}">
						<table class="messageBox">
							<tr>
							<td>
							<img src="../images/16x16/information.png" alt="info"/>
							</td>
							<td>There are no care spells / episodes defined for this patient.<br/>Please create one below:
							</td>
							</tr>
						</table>
					</c:if>
	
			<c:if test="${!empty command.patient.careSpells}">
			<fieldset style="width: 100% text-align: left;">
			
				<table width="100%">
					<tr>
						<th>Site</th>
						<th>Primary Clinician</th>
						<th>Key Worker</th>
						<th>Status</th>
						<th>Select</th>
					</tr>
					<c:forEach var="cs" items="${command.patient.careSpells}" varStatus="s">

						<c:if test="${cs!=null}">
							<tr>
								<td>
									<smvc:select path="patient.careSpells[${s.index}].primarySite">
										<smvc:option value="${cs.primarySite}"><webapp:listValue  domain="EMDT" listName="Speciality" key="${cs.primarySite}"/></smvc:option>
										<smvc:option items="${primarySiteList.pairs}" value="first" label="second"/>
									</smvc:select>
								</td>
								<td>
									<smvc:select path="primaryClinicianName[${s.index}]">
									 <smvc:option value=""></smvc:option>
										<c:forEach var="user" items="${activeUsers}">
										   <smvc:option value="${user.key}"><c:out value="${user.value}"/></smvc:option>
										</c:forEach>
									</smvc:select>
								</td>
								<td>
									<smvc:select path="keyWorkerName[${s.index}]">
										<smvc:option value=""/>
										<!-- <options items="<c:out value="${keyworkers.pairs}"/>" itemValue="first" itemLabel="second"/> --> 
										<c:forEach var="user" items="${activeUsers}">
								   		<smvc:option value="${user.key}"><c:out value="${user.value}"/></smvc:option></c:forEach>
									</smvc:select>
								</td>
								<td>
									<smvc:select path="patient.careSpells[${s.index}].status">
										<smvc:option value="active" label="Active"/>
										<smvc:option value="suspended" label="Suspended"/>
										<smvc:option value="discharged" label="Discharged"/>
									</smvc:select>
								</td>
								<td title="Click to make this the current Care Spell" align="center">
									<c:choose>
										<c:when test="${emdt.currentCareSpell.primarySite == cs.primarySite}">
											<input type="radio" name="selectedCarespell" value="<c:out value="${cs.id }"/>" checked="checked"/> 
										</c:when>
										<c:otherwise>
											<input type="radio" name="selectedCarespell" onclick="selectCarespell(<c:out value="${cs.id }"/>)" />  
										</c:otherwise>
									</c:choose>
								</td>
							</tr>
							<tr>
								<td colspan="5" class="error"><smvc:errors path="patient.careSpells[${s.index}].primarySite"/></td>
							</tr>

						</c:if>
					</c:forEach>
				</table>
			</fieldset>
			</c:if>
			<fieldset style="text-align: left;">
				<b>Create new Care spell</b>
				<hr size="1" width="100%"/>
				<table width="100%">
					<tr>
						<th>Site</th>
						<th>Primary Clinician</th>
						<th>Key Worker</th>
						<th>Status</th>
					</tr>
					<tr>
						<td>
							<smvc:select path="newCarespell.primarySite">
								<smvc:option value=""/>
								<smvc:option items="${primarySiteList.pairs}" value="first" label="second"/>
							</smvc:select>
						</td>
						<td>
							<smvc:select path="newCSPrimaryClinicianName">
							    <smvc:option value=""></smvc:option>
								<c:forEach var="user" items="${activeUsers}">
                                	<smvc:option value="${user.key}"><c:out value="${user.value}"/></smvc:option>
                                 </c:forEach>
							</smvc:select>
						</td>
						<td>
							<smvc:select path="newCSKeyWorkerName">
								<smvc:option value=""/>
								<c:forEach var="user" items="${activeUsers}">
  							   		 <smvc:option value="${user.key}"><c:out value="${user.value}"/></smvc:option>
  							    </c:forEach>
							</smvc:select>
						</td>
						<td>
							<smvc:select path="newCarespell.status">
								<smvc:option value="active" label="Active"/>
								<smvc:option value="suspended" label="Suspended"/>
								<smvc:option value="discharged" label="Discharged"/>
							</smvc:select>
						</td>
					</tr>
					<tr>
						<td colspan="4" class="error"><smvc:errors path="newCarespell.primarySite"/></td>
					</tr>
				</table>
			</fieldset>
			<fieldset class="buttonFieldset">
				<input type="submit"  value="Save"/>
			</fieldset>
		</fieldset>
	</smvc:form>
