<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<% request.setAttribute("title", "User List"); %>
<%@ include file="head.jsp" %>

<c:if test="${empty requestScope[Attributes.USERS]}">
    <p>There are no users registered (and you managed to login somehow???).</p>
</c:if>
<h2>User List (${requestScope[Attributes.COUNT]})</h2>

<c:if test="${empty requestScope[Attributes.USER].username}">
    <form:form method="post" modelAttribute="${Attributes.USER}" action="${pageContext.request.contextPath}${Routes.USERS}${Routes.ADD}">
        <form:input path="username" placeholder="username"/>
        <form:input path="email" placeholder="email"/>
        <form:input path="password" type="password" placeholder="password" />
        <br>
        <input type="submit" value="Add"/>
    </form:form>
</c:if>

<c:if test="${not empty requestScope[Attributes.USER].username}">
    <form:form method="post" modelAttribute="${Attributes.USER}" action="${pageContext.request.contextPath}${Routes.USERS}${Routes.EDIT}">
        <form:input path="username" value="${requestScope[Attributes.USER].username}"/>
        <form:input path="email" value="${requestScope[Attributes.USER].email}"/>
        <form:input path="password" type="password" value="******"/>
        <br>
        <input type="submit" value="Edit"/>
    </form:form>
</c:if>
<c:if test="${not empty requestScope[Attributes.USERS]}">
    <table>
        <thead>
            <th>Name</th>
            <th>Email</th>
        </thead>
        <tbody>
        <c:forEach var="user" items="${requestScope.get(Attributes.USERS)}">
            <tr>
                <td>${user.username}</td>
                <td>${user.email}</td>
                <td><a href="<c:url value="${Routes.USERS}${Routes.EDIT}/${user.username}"/>">Edit</a></td>
                <td><a href="<c:url value="${Routes.USERS}${Routes.DELETE}/${user.username}"/>">Delete</a></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</c:if>

<%@ include file="foot.jsp" %>

