<?php session_start(); ?>
<?php
	//secure the page to only Admins
	if(!isset($_SESSION['userType']) || $_SESSION['userType']!="Admin"){
		echo "<script>location.href='index.php?err=rights';</script>";
	}
		
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>SignMeUp - Event Management - Admin Home</title>

	<?php include("includes/head.php"); ?>
	<?php include("includes/connect.php"); ?>
	<style>
		ol{
			display:none;
		}
	</style>
	
  </head>
  <body>
  <header>
   <?php include("includes/nav.php"); ?>
</header>
<main role="main" class="container" >
	<h3>Admin Home - Event Management</h3>
	<input type='radio' id='all' name='filter' value='all' checked='checked' class='filter' />
	<label for='all'>All</label>
	<input type='radio' class='filter' id='prev' name='filter' value='Prev'/><label for='prev'>Previous</label>
	<input type='radio' class='filter' id='up' name='filter' value='Up' />
	<label for='up'>Upcoming</label><br/>
	

</main><!-- /.container -->
<?php include("includes/footer.php"); ?>
<script>
	$(document).ready(function(){
		
		$(document).on("click", ".event",function(){
			$(this).find('ol').slideToggle();
		});
		
	});//end ready
	$(document).ready(function(){
		
		$(".filter").click(function(){
			//clear the current data
			$("section").remove();
			$.ajax({
				type:"POST",
				url:"scripts/eventScript.php",
				data:{
					filter:$(this).val()
				},
				async:false, //due to nested ajax call
				success:function(response){
					//alert(response);
					$events=response.split(":::");
					for($x=0; $x<$events.length; $x++){
						//create the section with class=event
						$secNode=$("<section></section>").addClass("event");
						//add the row into the section
						$secNode.html($events[$x]);
						//create the ol 
						$olNode=$("<ol></ol>");
						//add the ol to the section
						$secNode.append($olNode);
						//add the section to the page
						$("main").append($secNode);
						
						$.ajax({
							type:"POST",
							url:"scripts/participantScript.php",
							async:false,
							data:{
								eventId:$("main").find("section").last().children().first().attr("data-id")
								
							},
							success:function(response){
								//alert(response);
								$participants=response.split(":::");
								for($y=0; $y<$participants.length-1; $y++){
									$liNode=$("<li></li>").html($participants[$y]);
									$("main").find("section").last().find("ol").append($liNode);
								}
								
							},
							error:function(){
								alert("participant error");
							}
							
						});//end participant ajax
					} //end events loop
					
				},
				error:function(){
					alert("Error");
				}
			});//end ajax
		});//end click
		$("#all").trigger("click");
		
	});//end ready
</script>
</body>
</html>
