<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<sec:authorize access="hasRole('ADMIN')">
	<smvc:form action="listAdmin.form?${_csrf.parameterName}=${_csrf.token}" method="post" enctype="multipart/form-data" modelAttribute="listAdminBean">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<jsp:directive.page contentType="text/html" />
	<c:set var="namedLists" value="${listAdminBean.namedLists}"/>
	<jsp:useBean id="listAdminPD" class="com.ardeo.util.PaginationData" scope="session"/>
	<c:if test="${param.pageSize != null}">
		<jsp:setProperty name="listAdminPD" property="pageSize" value="${param.pageSize}"/>
	</c:if>
	<jsp:setProperty name="listAdminPD" property="size" value="${fn:length(namedLists)}"/>
	<c:choose>
		<c:when test="${param.page != null}">
			<jsp:setProperty name="listAdminPD" property="currentPage" value="${param.page}"/>
		</c:when>
		<c:otherwise>
			<jsp:setProperty name="listAdminPD" property="currentPage" value="1"/>
		</c:otherwise>
	</c:choose>
	<script type="text/javascript">
			function update() {
				document.location="listAdmin.form";
			}
			
			function createNewList() {
				var val = document.getElementById("newListName").value;
				if(val && val.length > 0) {
					editList("EMDT", val);
				} else {
					window.alert("You must enter a list name");
				}
			}
			
			function confirmDelete() {
				return window.confirm("Are you sure you wish to delete the selected lists?");
			}
			
			function changePageSize(el) {
				document.location = 'listAdmin.form?pageSize=' + el.options[el.selectedIndex].value;
			}
	</script>
	<br/>

	
		<fieldset class="nobg" style="width: 50%; text-align: left">
			<legend>Lists</legend>
			<fieldset>
				<table width="100%" >
					<tr>
						<td colspan="3">
							<table width="100%">
								<tr>
									<td width="1">
										<c:choose>
											<c:when test="${listAdminPD.previousPageNum != null}">
												<a title="Previous Page" href="listAdmin.form?page=<c:out value="${listAdminPD.previousPageNum}"/>"><img src="../images/16x16/action_back.png"/></a>
											</c:when>
											<c:otherwise>
												<img src="../images/16x16/action_back_disabled.png"/>
											</c:otherwise>
										</c:choose>
									</td>
									<td align="center"><b>Page <c:out value="${listAdminPD.currentPage }"/> of <c:out value="${listAdminPD.numberOfPages }"/></b></td>
									<td align="right" width="1">
										<c:choose>
											<c:when test="${listAdminPD.nextPageNum != null}">
												<a title="Next Page" href="listAdmin.form?page=<c:out value="${listAdminPD.nextPageNum}"/>"><img src="../images/16x16/action_forward.png"/></a>
											</c:when>
											<c:otherwise>
												<img src="../images/16x16/action_forward_disabled.png"/>
											</c:otherwise>
										</c:choose>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<th width="1"/>
						<th align="left">Name</th>
						<th title="Number of values in list" width="1">Size</th>
					</tr>
					<c:forEach var="nl" items="${namedLists}" begin="${listAdminPD.startOfPageIndex}" end="${listAdminPD.endOfPageIndex}">
						<tr>
							<td width="1"><input class="allNonInvert" type="checkbox" value="<c:out value="${nl.id }"/>" name="selected"/></td>
							<td><a style="color: blue;" href="javascript:editList('<c:out value="${nl.domain }"/>', '<c:out value="${nl.name }"/>')"><c:out value="${nl.name }"/></a></td>
							<td title="Number of values in list" align="right"><c:out value="${fn:length(nl.pairs) }"/></td>
						</tr>
					</c:forEach> 
					<tr>
						<td colspan="3">
							<hr/>
							<table width="100%" cellpadding="0" cellspacing="0">
								<tr>
									<td>
										Select: <a href="javascript:select('all')">All</a> | <a href="javascript:select('none')">None</a> | <a href="javascript:select('invert')">Invert</a>
										&#160;&#160;Show 
										<select  name="selectPageSize" onchange="changePageSize(this)">
											<webapp:option value="10" currentValue="${listAdminPD.pageSize }">10</webapp:option>
											<webapp:option value="20" currentValue="${listAdminPD.pageSize }">20</webapp:option>
											<webapp:option value="30" currentValue="${listAdminPD.pageSize }">30</webapp:option>
											<webapp:option value="40" currentValue="${listAdminPD.pageSize }">40</webapp:option>
											<webapp:option value="50" currentValue="${listAdminPD.pageSize }">50</webapp:option>
											<webapp:option value="${listAdminPD.size }" currentValue="${listAdminPD.pageSize }">All</webapp:option>
										</select>
										items per page
									</td>
									<td align="right">
										<input type="submit" onclick="return confirmDelete()" name="delete" value="Delete" title="Delete selected items"/>&#160;<input title="Export selected items" name="export" type="submit" value="Export"/>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</fieldset>
			<fieldset>
				<div>
					<b>Create new list...</b><br/>
					Name: <input type="text" id="newListName"/> <input type="button" value="create" onclick="createNewList()"/>
				</div>
				<br/>
				<div>
					<b>Import list...</b><br/>
					File: <input type="file" name="document" onchange="setFileSize(this);"/> <input name="import" type="submit" value="Import"/>
					<c:if test="${importCount != null}">
						<div class="message">
							<c:out value="${importCount}"/> list(s) have been imported
						</div>
					</c:if>
				</div>
			</fieldset>
		</fieldset>
	</smvc:form></sec:authorize>