<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%
     System.out.println("\n\nEntering AppointmentsAdm.jsp\n\n");
     java.util.List appointments = (java.util.List)request.getSession().getAttribute("appointments");
     java.util.List userList = (java.util.List)request.getSession().getAttribute("users");
%>


<html>
  <head>
   <title>Club Administration V0.1 -- Appointment Administration</title>
   <script type="text/javascript">
      function enterValue1(AppointmentId)
      {
        document.forms[1].elements[1].value = AppointmentId;
        document.forms[2].elements[0].value = AppointmentId;
      }
      
      function updateApp()
      {
        userId = document.all.UserSelect.value;
        document.forms[1].elements[0].value = userId;  
        document.forms[1].submit();
      }
    </script>
  </head>
<body>
 <center>
 <h1>Appointment Administration</h1><br><br>
 <html:errors/></p>
 <h2>Current Appointments</h2>
 <table border="1">
   <tr><th>Select:</th><th>Subject:</th><th>Leader:</th><th>Users:</th><th>Date:</th><th>Duration:</th><th>Category</th><th>Place:</th></tr>
   <%
       if (appointments != null) {
          for (java.util.Iterator itor = appointments.iterator(); itor.hasNext();) {
              com.ferienclub.custadmin.beans.Appointment a = (com.ferienclub.custadmin.beans.Appointment)itor.next();
              java.lang.String sub = a.getSubject();
              java.lang.String leadingUser = a.getLeadingUser().getName();
              java.util.List users = a.getUsers();
              if (users == null) users = new java.util.ArrayList();
              int duration = a.getDuration();
              java.lang.String category = a.getCategory();
              java.lang.String place = a.getPlace().getName();
              java.lang.String usersString = "";
              java.util.Date date = a.getDatetime();
              java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
	      String dateString = sdf.format(date);

              for (java.util.Iterator itor2= users.iterator(); itor2.hasNext();) {
                 usersString += ((com.ferienclub.custadmin.beans.User)itor2.next()).getName() + "<br>";
              }
              %>
                 <tr>
                    <td><input type="radio" name="AppointmentChoice" value="<%= a.getAppointmentId() %>" onClick="enterValue1('<%= a.getAppointmentId() %>')"/></td>
                    <td><%= sub %></td>
                    <td><%= leadingUser %></td>
                    <td><%= usersString %></td>
                    <td><%= dateString %></td>
                    <td><%= duration %></td>
                    <td><%= category %></td> 
                    <td><%= place %></td>
                 </tr> 
              <%
          }
       }
   %>
 </table>
 
<br>
<br>

 <!-- The UserList -->

 <select name="UserSelect"> 
 <% 
     for (java.util.Iterator itor3 = userList.iterator(); itor3.hasNext();)
     {
        com.ferienclub.custadmin.beans.User u = (com.ferienclub.custadmin.beans.User)itor3.next();
        String userId = u.getUserId();
        String userName = u.getFirstName() + " " + u.getName();
        %><option value="<%= userId %>"><%= userName %></option><%
     }
  %>   
 </select> 

<!-- Formulare -->
<html:form action="CreateAppointment.do?mode=init">
</html:form>

<html:form action="UpdateAppointment.do">
  <html:hidden property="userId" name="appointmentForm"/>
  <html:hidden property="appointmentId" name="appointmentForm"/>
</html:form>

<html:form action="DeleteAppointment.do">
  <html:hidden property="appointmentId" name="appointmentForm"/>
</html:form>

 <input type="button" value="Create Appointment" onClick="document.forms[0].submit()"/> 
 <input type="button" value="Delete Appointment" onClick="document.forms[2].submit()"/> 
 <input type="button" value="Update Appointment" onClick="updateApp()"/> 
 <html:link href="/ClubAdministration">Back to Start</html:link>

</center>
</body>
</html>
