<%@ include file="SQL.jsp" %>
<%@ page import="java.sql.*" %>
<%@ include file="header.jsp"%>

<!-- javascript for highlighting the table's rows -->
<script type="text/javascript">
    function ChangeColor(tableRow, highLight)
    {
    if (highLight)
    {
      tableRow.style.backgroundColor = '#dcfac9';
    }
    else
    {
      tableRow.style.backgroundColor = 'white';
    }
  }

  function DoNav(theUrl)
  {
  	document.location.href = theUrl;
  }
</script>


<%
String self = request.getParameter("self");
%>

<body>

<!-- main navigation -->
<div id="b3k_contentarea_header">
	 <div id="b3k_contentarea_header_nav">
                  <ul <% if(self!=null && self.equals("true")) {
					out.println("class=\"b3k_menumode\">");
				}else{
					out.println(">");
				}
			%>
                    
                    <li class="b3k_firstchild">
                      <a href="myReservations.jsp" title="Meine Reservierungen">Meine Reservierungen</a>
                     </li>
                    
                    
                    <li class="b3k_lastchild  b3k_active">
                      <a href="reservationDateTimeChooser.jsp?self=
				<% if(self!=null && self.equals("true")) {
						out.print("false");
					}else{
						out.print("true");
					}
				%>
			" title="Neue Reservierung">Neue Reservierung</a>
                     </li>

		    <li class="b3k_lastchild">
                      <a href="logout.jsp" title="Logout">Logout</a>
                   </li>
                    
                  </ul>
       </div>
</div>
<h2>
<!-- main navigation end-->

<%
//retrieving the parameters transmitted in the URL

String user = (String) session.getAttribute("user");
String day = request.getParameter("day");
String time = request.getParameter("time");
String duration = request.getParameter("duration");

if(user!=null){
	Integer t = new Integer(time) + new Integer(duration);	//change the time values into integer values
	if(t%100 == 60) t = t + 40 ;				//if the time+duration contains a 60 at the end, the hour is rounded up using +40
	String endTime = null;
	if(t<1000){
		endTime = "0"+t.toString();		//adding 0 to get the time format 00:00 if it was in 0:00
	}else{
		endTime = t.toString();
	}
	//change endTime to match xx:yy
	endTime = endTime.substring(0, 2)+":"+endTime.substring(2);

	

	//change time to match xx:yy
	if(new Integer(time)<1000){
		time = "0"+time;
	}
	time = time.substring(0, 2)+":"+time.substring(2);
	

	if(t<=2200){		
		//time maximum is 22:00 = 10 PM which is the maximum opening hour of the library
		//reservations can only be placed if the opening hour is not exceeded
		
		ResultSet res = null;

		out.println("<p><table class='resList'>");
		out.println("<tr><td>");	

		res = queryDB("SELECT DATE_FORMAT( CURRENT_DATE+"+day+", '%a') as DayOfWeek, "+
				"DATE_FORMAT( CURRENT_DATE+"+day+", '%d') as NumberOfDay, "+
				"DATE_FORMAT( CURRENT_DATE+"+day+", '%b') as Month ;");
		res.next();
		out.println("<table>");
        	out.println("<tr class='first'><td>"+res.getString("DayOfWeek")+
				", "+res.getString("NumberOfDay")+". "+res.getString("Month"));
		out.println("<td>"+time+" - "+endTime);
		out.println("</table>");


		//-------------------------------------------------------------------


		String alreadyReservated = "select bachelor_reservation.id as reservationID from bachelor_reservation, bachelor_user  where "+
				"(user_id='" + user +"') "+				
				"and ( ('"+time+"' = startTime and '"+endTime+"' = endTime ) "+
				"or ('"+time+"' > startTime and '"+endTime+"' < endTime ) "+
				"or ('"+time+"' < startTime and '"+endTime+"' > startTime ) "+
				"or ('"+time+"' < endTime and '"+endTime+"' > endTime ) "+
				"or ('"+time+"' < startTime and '"+endTime+"' > endTime ) ) "+
				"and date=CURRENT_DATE+"+day+";";
		res = queryDB(alreadyReservated);


		

		
		if(!res.next()){
			
			String sql =  "select count(id) as freeStations, level, area from "+
				"(select id as reservationID, startTime, endTime, date, station from bachelor_reservation  where "+
				"('"+time+"'=startTime and '"+endTime+"'=endTime ) "+
				"or ('"+time+"' > startTime and '"+endTime+"' < endTime ) "+
				"or ('"+time+"' < startTime and '"+endTime+"' > startTime ) "+
				"or ('"+time+"' < endTime and '"+endTime+"' > endTime ) "+
				"or ('"+time+"' < startTime and '"+endTime+"' > endTime ) "+
				"and date=CURRENT_DATE+"+day+") as tempTable "+
				"right join bachelor_station on tempTable.station = bachelor_station.id "+
				"where reservationID is null group by level" ;


			//returns the free learning stations to the given date and time at the given level

			
			ResultSet res2 = queryDB(sql);
		
			out.println("<tr><td>Gew"+"\u00FC"+"nschtes Stockwerk anklicken:");
			out.println("<tr><td>");
			out.println("<table>");
			out.println("<tr class='first'><td>Stock<td>freie Pl"+"\u00E4"+"tze");
			//creates the table for the differnt levels
			while(res2.next()){
				String script = "onmouseover='ChangeColor(this, true);' onmouseout='ChangeColor(this, false);' onclick='DoNav("
				+"\"newReservation.jsp?level="+res2.getString("level")+"&day="+day+"&time="+time
				+"&endTime="+endTime
				+"\");' ";

				int free = 0;
				free = res2.getInt("freeStations");
				out.println("<tr "+script+"><td>"+res2.getString("level")+"<td>"+free);
			}
		
			out.println("</table>");

		}else{
			//already a reservation
			out.println("<h2>Sie haben zu dieser Zeit bereits eine Reservierung!</h2>");
			out.println("<meta HTTP-EQUIV='REFRESH' content='5; url=reservationDateTimeChooser.jsp'>");
		
		}

	}else{
		//if time exceeds 24h
		out.println("<h2>Zeitangabe ung\u00FCltig!</h2>");
		out.println("<meta HTTP-EQUIV='REFRESH' content='2; url=reservationDateTimeChooser.jsp'>");	
	}


}else{
	//if user is not logged in -> redirect
	out.println("<meta HTTP-EQUIV='REFRESH' content='0; url=index.jsp'>");

}
%>
</table>
</h2>
</body>
<%@ include file="footer.jsp"%>
</html>

