<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
  <head profile="<c:url value="/profile.txt"/>">
    <title><fmt:message key="lists.title"/></title>
    <link media="screen" href="<c:url value="/css/global.css"/>" type="text/css" rel="stylesheet" />
  	<script type="text/javascript">
	<!--
		function changeList(select) {
			select.form.submit();
		}
		
	// -->
	</script>
  </head>
  <body>
	<jsp:include page="nav-header.jsp"/>
    <h1><fmt:message key="lists.title"/></h1>

    <spring:hasBindErrors name="command">
    	<div class="error-intro">
			<spring:bind path="command">
				<c:choose>
					<c:when test="${status.errors.errorCount > 1}">
						<ul>
							<c:forEach items="${status.errors.allErrors}" var="error">
								<li>
									<spring:message code="${error.code}" arguments="${error.arguments}"/><br />
								</li>
							</c:forEach>
						</ul>
					</c:when>
					<c:when test="${status.errors.errorCount == 1}">
						<c:set var="error" value="${status.errors.allErrors[0]}"/>
						<spring:message code="${error.code}" arguments="${error.arguments}"/><br />
					</c:when>
				</c:choose>
			</spring:bind>
    	</div>
    </spring:hasBindErrors>

	<c:choose>
		<c:when test="${updateCount > 0}">
			<div class="error-intro">
				<fmt:message key="lists.updated">
					<fmt:param value="${updateCount}"/>
				</fmt:message>
			</div>
		</c:when>
		<c:otherwise>
    		<p><fmt:message key="lists.intro"/></p>
		</c:otherwise>
	</c:choose>

	<%-- Domain object drop-down menu --%>
	<form method="get" action="<c:url value="/setup/lists.do"/>">

		<table class="form">
			<tr>
				<th><fmt:message key="lists.list.displayName"/></th>
				<td>
		
		<spring:bind path="command.beanClass">
			<select name="<c:out value="${status.expression}"/>"
				onchange="changeList(this)">
				<option value=""/>
				<option value="magoffin.matt.ieat.domain.impl.BaseImpl" <c:if 
						test="${status.value.name == 
						'magoffin.matt.ieat.domain.impl.BaseImpl'}">selected="selected"</c:if>
					><fmt:message key="recipe.base.displayName"/></option>
				<option value="magoffin.matt.ieat.domain.impl.CourseImpl" <c:if 
						test="${status.value.name == 
						'magoffin.matt.ieat.domain.impl.CourseImpl'}">selected="selected"</c:if>
					><fmt:message key="recipe.course.displayName"/></option>
				<option value="magoffin.matt.ieat.domain.impl.DifficultyImpl" <c:if 
						test="${status.value.name == 
						'magoffin.matt.ieat.domain.impl.DifficultyImpl'}">selected="selected"</c:if>
					><fmt:message key="recipe.difficulty.displayName"/></option>
				<option value="magoffin.matt.ieat.domain.impl.EthnicityImpl" <c:if 
						test="${status.value.name == 
						'magoffin.matt.ieat.domain.impl.EthnicityImpl'}">selected="selected"</c:if>
					><fmt:message key="recipe.ethnicity.displayName"/></option>
				<option value="magoffin.matt.ieat.domain.impl.IngredientImpl" <c:if 
						test="${status.value.name == 
						'magoffin.matt.ieat.domain.impl.IngredientImpl'}">selected="selected"</c:if>
					><fmt:message key="recipe.ingredient.ingredient.displayName"/></option>
				<option value="magoffin.matt.ieat.domain.impl.PrepTimeImpl" <c:if 
						test="${status.value.name == 
						'magoffin.matt.ieat.domain.impl.PrepTimeImpl'}">selected="selected"</c:if>
					><fmt:message key="recipe.prepTime.displayName"/></option>
			</select> <br />
		</spring:bind>

				</td>
			</tr>
			<tr>
				<th><fmt:message key="language.displayName"/></th>
				<td class="readonly">${pageContext.request.locale.language}</td>
			</tr>
		</table>
	</form>

	<c:if test="${fn:length(command.beans) > 0}">

	<hr />

	<form method="post" action="<c:url value="/setup/lists.do"/>">
		
		<table class="form">
			<tr>
				<c:forEach var="prop" items="${meta[command.beanClass.name].properties}" varStatus="status">
				<th>${prop.displayName}</th>
				</c:forEach>
				<th><fmt:message key="lists.remove.displayName"/></th>
				<th><fmt:message key="lists.reassign.displayName"/></th>
			</tr>
			
			<c:forEach var="item" items="${command.beans}" varStatus="idxStatus">
			<tr<c:if test="${(idxStatus.index % 2) == 1}"> class="even"</c:if>>
				<spring:bind path="command.beans[${idxStatus.index}]">
				<c:forEach var="prop" items="${meta[command.beanClass.name].properties}">
				<td>
					<c:choose>
						<c:when test="${prop.primaryKey}">
							${item[prop.name]}
							<input type="hidden" name="${status.expression}['${prop.name}']" 
								value="${item[prop.name]}"/>
							<c:set var="primaryKeyPropName" value="${prop.name}" scope="page"/>
						</c:when>
						<c:when test="${prop.type == 3}">
						</c:when>
						<c:otherwise>
							<input type="text" name="${status.expression}['${prop.name}']" value="${item[prop.name]}"
								<c:if test="${prop.length > 0}">
									maxlength="${prop.length}"
								</c:if>
								/>
							<c:set var="origName">_orig_${prop.name}</c:set>
							<input type="hidden" name="${status.expression}['_orig_${prop.name}']" 
								value="${item[origName]}"/>
						</c:otherwise>
					</c:choose>
				</td>
				</c:forEach>
				<td>
					<input type="checkbox" name="${status.expression}['_delete_']"
						value="true"/>
				</td>
				<td>
					<input type="radio" name="reassignPrimaryKey"
						value="${command.beans[idxStatus.index][primaryKeyPropName]}"/>
				</td>					
				</spring:bind>
			</tr>
			</c:forEach>

			<tr>
				<spring:bind path="command.newBean">
				<c:forEach var="prop" items="${meta[command.beanClass.name].properties}">
				<td>
					<c:choose>
						<c:when test="${prop.primaryKey}">
							<fmt:message key="lists.new.displayName"/>
						</c:when>
						<c:when test="${prop.type == 3}">
						</c:when>
						<c:otherwise>
							<input type="text" name="${status.expression}['${prop.name}']" value="${item[prop.name]}"
								<c:if test="${prop.length > 0}">
									maxlength="${prop.length}"
								</c:if>
								/>
						</c:otherwise>
					</c:choose>
				</td>
				</c:forEach>
				<td></td>
				<td></td>					
				</spring:bind>
			</tr>
			
		</table>

		<div>
			<input type="hidden" name="beanClass" value="${command.beanClass.name}"/>
			<input type="submit" name="_finish" value="<fmt:message key="save.displayName"/>"/>
		</div>

	</form>	

	</c:if>

  </body>
</html>
