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

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

<%
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>
<!-- main navigation end-->

<%
String user = (String) session.getAttribute("user");
String day = request.getParameter("day");
String time = request.getParameter("time");
String endTime = request.getParameter("endTime");
String level = request.getParameter("level");
String area = request.getParameter("area");


if(user!=null){	
	ResultSet res = null;

	String sqlAreaCount = "select count(area) as areaCount from (select stationID, startTime, endTime, date, level, area from "+
				"(select bachelor_station.id as stationID , bachelor_reservation.id as reservationID, "+
				"startTime, endTime, date, level, area from bachelor_station left join bachelor_reservation "+
				"on station=bachelor_station.id) as tempTable where "+
				"('"+time+"'>=endTime or '"+endTime+"'<=startTime or startTime is NULL) and "+
				"(date=CURRENT_DATE+"+day+" or date is NULL) and "+
				"level="+level+" group by area) as tempTwo;" ;

	String sqlAreaList = "select stationID, startTime, endTime, date, level, area from "+
				"(select bachelor_station.id as stationID , bachelor_reservation.id as reservationID, "+
				"startTime, endTime, date, level, area from bachelor_station left join bachelor_reservation "+
				"on station=bachelor_station.id) as tempTable where "+
				"('"+time+"'>=endTime or '"+endTime+"'<=startTime or startTime is NULL) and "+
				"(date=CURRENT_DATE+"+day+" or date is NULL) and "+
				"level="+level+" group by area;" ;

	
	if(area==null){
		//no area selected
		
		res = queryDB(sqlAreaCount);
		res.next();
		int areaCount = res.getInt("areaCount");





		if(areaCount>1){
			//multiple areas avaiable
			res = queryDB(sqlAreaList);
			
			out.println("<h2> Mehrere freie Breiche gefunden. Bitte ausw\u00E4hlen:</h2><h3>");
			out.println("<form class='search' action=newReservation.jsp>");
			out.println("<select name='area'>");
			while(res.next()){
				out.println("<option value='" +res.getString("area")+ "'>Bereich " +res.getString("area")+ "</option>");
			}
			out.println("</select>");
			out.println("<input type='hidden' name='day' value='"+day+"'/>");
			out.println("<input type='hidden' name='time' value='"+time+"'/>");
			out.println("<input type='hidden' name='endTime' value='"+endTime+"'/>");
			out.println("<input type='hidden' name='level' value='"+level+"'/>");
			out.println("<input type='submit' value='Reservieren'/>");
			out.println("</form></h2>");

			out.println("<img src='mobile-img/level"+level+"explained.jpg' border='0' width='350' height='410'>");

		}else{

			//only one area found
			if(areaCount==1){
				res = queryDB(sqlAreaList);
				res.next();
				area=res.getString("area");

				out.println("<meta HTTP-EQUIV='REFRESH' content='0; url=newReservation.jsp"+
					"?level="+res.getString("level")+
					"&day="+day+
					"&time="+time+
					"&endTime="+endTime+
					"&area="+area+
					"'>");
			}else{
				//error if area count is 0
				out.println("<h1>Datenbankfehler!</h1>");
			}
		}	

	}else{
		//if area was selected
		//perform reservation

		String sqlFreeStationOnLvlAndArea = "select stationID, startTime, endTime, date, level, area from "+
				"(select bachelor_station.id as stationID , bachelor_reservation.id as reservationID, "+
				"startTime, endTime, date, level, area from bachelor_station left join bachelor_reservation "+
				"on station=bachelor_station.id) as tempTable where "+
				"('"+time+"'>=endTime or '"+endTime+"'<=startTime or startTime is NULL) and "+
				"(date=CURRENT_DATE+"+day+" or date is NULL) and "+
				"level="+level+" and area="+area+";" ;

		res = queryDB(sqlFreeStationOnLvlAndArea);
		res.next();
		String station = res.getString("stationID");

		String sqlInsert = "insert into bachelor_reservation(date, startTime, endTime, unused, user_id, station) values "+
					"( CURRENT_DATE+"+day+", '"+time+"', '"+endTime+"', true, "+user+", "+station+" )" ;

		int rv = updateDB(sqlInsert);
		if(rv>0) {
			out.println("<br><h2>Lernplatz reserviert!</h2>");
			out.println("<meta HTTP-EQUIV='REFRESH' content='2; url=myReservations.jsp'>");
		}else{
			out.println("<h1>Datenbankfehler!</h1>");
		}
				
	}


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

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