<?php
  require('inc/sessionStart.php');
?>
<?php
  require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.features.inc.php");
  $storeObj = new stores();
  if(isset($_GET['a'])){
    if($_GET['a'] == "add"){
	  $action = "add";
    }else if($_GET['a'] == "new"){
	  $action = "newItem";
	  $addItem = $storeObj->addEvent();
	}else{
	  $action = "base";
	}
  }else if(isset($_GET['id'])){
	$theEventID = $_GET['id'];
	$action = "edit";
	$stores = new stores();
  }else{
	$action = "base";
  }
  
?>

<?php
  $pageTitle = "Administration: Event Management";
  $pageDescription = "";
  $pageKeywords = "";
?>

<?php
  include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php");
?>
<script async src="//afsshareportal.com/js/moment.min.js"></script>
<script src="/Kickstrap/js/jquery-ui-1.9.0.custom.min.js"></script>
<script src="/Kickstrap/js/jquery.h5validate.js"></script>
<link href="/Kickstrap/css/smoothness/jquery-ui-1.9.0.custom.css" rel="stylesheet">
<link href="/Kickstrap/css/jquery.ui.timepicker.css" rel="stylesheet">
<script src="/Kickstrap/js/jquery.ui.timepicker.js"></script>
  
<?php
  include_once($_SERVER['DOCUMENT_ROOT'] . "/common/mainNav.php");
?>



    
    <section class="container">
      <div id="blankBuffer" class="carousel-buffer"></div>
      <div id="noEditError" class="hideObject">
			<div class="alert alert-error">
				  <a class="close" data-dismiss="alert" href="#">x</a>You can only <span id="noEditErrorText"></span> if you have editing capabilities.
			</div>
	  </div>
	  <section class="container" id="storePortal">
	    <div class="row">
		  <div class="span3 sideNav">
			<?php include('inc/sideNav.php'); ?>
			<br><br>
		  </div>
		  
		  <div class="span9 portalContent">
			<div class="contentContain">
			  <?php
				if($action == "base" || $action == "newItem"){
				  if($action == "newItem" && $addItem != false){
			  ?>
					<div id="addSuccess">
					  <div class="alert alert-success">
						<a class="close" data-dismiss="alert" href="#">x</a>You successfully added the event to the database.
					  </div>
					</div>
			  <?php
				  }else if($action == "newItem" && $addItem == false){
			  ?>
					<div id="addError">
					  <div class="alert alert-error">
						<a class="close" data-dismiss="alert" href="#">x</a>There was a problem adding your event to the database.
					  </div>
					</div>
			  <?php
				  }
				  
				  $theEvents = $storeObj->getAllEvents();
			  ?>
				<div id="eventContain">
				  <h2>Store Events <span class="floatRight"><a href="events.php?a=add" class="btn btn-primary">Add an Event</a></span></h2>
				  <?php
					foreach($theEvents as $anEvent){
				  ?>
					<div class="anEvent">
					  <h4>
						<?php echo stripslashes($anEvent['event_name']); ?>&nbsp;&nbsp;&nbsp;
						<a href="events.php?id=<?php echo $anEvent['event_id'] ?>"><i class="icon-edit muted" title="Edit this event"></i></a>&nbsp;&nbsp;&nbsp;
						<a href="#" id="delete<?php echo $anEvent['event_id']; ?>" class="deleteEvent"><i class="icon-trash muted" title="Delete This Event"></i></a>
					  </h4>
					  <div class="offsetPadLeft">
						<h6><?php echo $anEvent['event_date']; ?> from <?php echo $anEvent['event_start']; ?> to <?php echo $anEvent['event_end']; ?></h6>
						<h6>Locations: <?php echo $anEvent['event_location']; ?></h6>
						<p><?php echo stripslashes($anEvent['event_desc']); ?></p>
					  </div>
					  
					  <div class="deleteAlert alert alert-error hideObject">
						Are you sure you want to delete this Event?<br><br>
						<a href="#" class="deleteEventConfirm btn">Yes</a> &nbsp;&nbsp;<a href="#" class="deleteEventNo btn">No</a>
					  </div>
					</div>
				  <?php
					}
				  ?>
				</div>
			  <?php
				}// end if($action == "base")
				else if($action == "add"){
				  $theLocations = $storeObj->getStoreLocations();
			  ?>
				<h2>Add an Event</h2>
				<div id="eventContain">
				  <form action="events.php?a=new" method="post" name="newEvent" id="newEvent">
					<!--<input type="hidden" name="action" value="newDeliItem">-->
					<div>
					  <label for="event_name">Event Name:</label>
					  <input type="text" name="event_name" id="event_name">
					</div><br>
					
					<div id="datepicker_start1" class="input-append date datepicker" data-date-format="yyyy-mm-dd" data-date="<?php echo date('Y-m-d'); ?>">
					  <label for="datepicker_start">Event Date:</label>
					  <input type="text" id="datepicker_start" name="datepicker_start" value="" readonly="readonly" required/>
					  <span class="add-on" id=><i class="icon-calendar"></i></span>
					</div><br>
					
					<div>
					  <label for="event_desc">Event Description:</label>
					  <textarea name="event_desc" id="event_desc" rows="6"></textarea>
					</div><br>
					
					<div>
					  <label for="event_location">Event Locations:</label>
					  <div class="offsetPadLeft">
						<?php
						  foreach($theLocations as $aLocation){
						?>
						  <input type="checkbox" name="event_location[]" value="<?php echo $aLocation['store_name']; ?>"> <?php echo $aLocation['store_name']; ?><br>
						<?php
						  }
						?>
					  </div>
					</div><br>
					
					<div>
					  <label for="timepicker_start">Start Time: </label>
					  <input type="text" name="timepicker_start" id="timepicker_start" value="" required>
					  <label for="timepicker_end">End Time: </label>
					  <input type="text" name="timepicker_end" id="timepicker_end" value="" required>
					</div>
					<br><br>
					<input type="submit" id="submit" name="submit" value="Submit" class="btn" />
				  </form>
				</div>
				<script>
				  $("body").on("submit", "#newEvent", function(e){
					//e.preventDefault();
					
					return true;
				  });
				</script>
			  <?php
				}//end else if($action == "add")
				else if($action == "edit"){
				  $theEvent = $storeObj->getOneEvent($theEventID);
			  ?>
				<h2>Edit <?php echo stripslashes($theEvent['event_name']); ?></h2>
				<table class="addFeature" id="event_<?php echo $theEventID; ?>">
				  <tr>
					<td class="aLabel">
					  Event Name:
					  <span class="featureControls">
							<a href="#" class="ajaxEdit" id="name_<?php echo $theEventID; ?>"><i class="icon-edit" title="Change the Event Name"></i></a>&nbsp;&nbsp;&nbsp;
					  </span>
					</td>
					<td><?php echo stripslashes($theEvent['event_name']); ?></td>
				  </tr>
				  <tr>
					<td class="aLabel">
					  Date:
					  <span class="featureControls">
							<a href="#" class="ajaxEdit" id="date_<?php echo $theEventID; ?>"><i class="icon-edit" title="Change the Date"></i></a>&nbsp;&nbsp;&nbsp;
					  </span>
					</td>
					<td id="startDate">
					  <?php echo stripslashes($theEvent['event_date']); ?>
					</td>
				  </tr>
				  <tr>
					<td class="aLabel">
					  Description:
					  <span class="featureControls">
							<a href="#" class="ajaxEdit" id="desc_<?php echo $theEventID; ?>"><i class="icon-edit" title="Change the Description"></i></a>&nbsp;&nbsp;&nbsp;
					  </span>
					</td>
					<td id="endDate">
					  <?php echo stripslashes($theEvent['event_desc']); ?>
					</td>
				  </tr>
				  
				  <tr>
					<td class="aLabel">
					  Event Location:
					  <span class="featureControls">
							<a href="#" class="ajaxEdit" id="location_<?php echo $theEventID; ?>"><i class="icon-edit" title="Change the Headline"></i></a>&nbsp;&nbsp;&nbsp;
					  </span>
					</td>
					<td><?php echo stripslashes($theEvent['event_location']); ?></td>
				  </tr>
				  <tr>
					<td class="aLabel">
					  Start Time:
					  <span class="featureControls">
							<a href="#" class="ajaxEdit" id="start_<?php echo $theEventID; ?>"><i class="icon-edit" title="Change the Start Time"></i></a>&nbsp;&nbsp;&nbsp;
					  </span>
					</td>
					<td><?php echo stripslashes($theEvent['event_start']); ?></td>
				  </tr>
				  <tr>
					<td class="aLabel">
					  End Time:
					  <span class="featureControls">
							<a href="#" class="ajaxEdit" id="end_<?php echo $theEventID; ?>"><i class="icon-edit" title="Change the Start Time"></i></a>&nbsp;&nbsp;&nbsp;
					  </span>
					</td>
					<td><?php echo stripslashes($theEvent['event_end']); ?></td>
				  </tr>
				</table>
			  <?php
				}
			  ?>
			</div>
		  </div>
	    </div>
	    
      </section>
      
    </section>
      
    
  
  <script src="/admin/js/includeJS.js"></script>
  
  <script>
	var theAction = "<?php echo $action; ?>";
	ks.ready(function() {
      if(theAction == "base" || theAction == "newItem"){
		$(".deleteEvent").on("click", function(e){
			e.preventDefault();
			$(this).parents(".anEvent").children(".deleteAlert").removeClass("hideObject");
		});
		
		$(".deleteEventNo").on("click", function(e){
			  e.preventDefault();
			  $(this).parents(".deleteAlert").addClass("hideObject");
		});
		
		$(".deleteEventConfirm").on("click", function(e){
		  e.preventDefault();
		  var eventID = $(this).parents(".anEvent").find(".deleteEvent").attr("id").replace("delete", "");
		  $.ajax({
			type: "POST",
			url: "interactions.php",
			data: "action=deleteEvent&event_id=" + eventID,
			dataType: "json",
			success: function(theResponse){
			  if(theResponse == true){
					$("#delete" + eventID).parents(".anEvent").remove();
			  }else{
					$("#noEditErrorText").text("delete a event")
					$("#noEditError").removeClass("hideObject");
					$(document).scrollTop("0");
			  }
			}
		  });
		});
	  }else if(theAction == "add"){
		var now = new moment().set('hour', 0);
	  
		var dateStart = $("#datepicker_start1").datepicker({
		  onRender: function(date){
			return date.valueOf() < now.valueOf() ? 'disabled' : '';
		  }
		}).on('changeDate', function(ev){
		  dateStart.hide();
		}).data('datepicker');
		
		$('#timepicker_start').timepicker({
			  showLeadingZero: false,
			  onHourShow: tpStartOnHourShowCallback,
			  onMinuteShow: tpStartOnMinuteShowCallback,
			  showPeriod: true,
		});
		  
		$('#timepicker_end').timepicker({
			  showLeadingZero: false,
			  onHourShow: tpEndOnHourShowCallback,
			  onMinuteShow: tpEndOnMinuteShowCallback,
			  showPeriod: true,
		});
	  }else if(theAction == "edit"){
		$("body").on("click", "#killAjaxBox", function(e){
		  e.preventDefault();
		  $("#theAjaxUpdateDiv").remove();
		});
		
		$(".ajaxEdit").bind("click", function(e){
		  e.preventDefault();
		  $("#theAjaxUpdateDiv").remove();
		  var $this = $(this);
		  var theID = $this.attr("id");
		  //var theValue = "This will be the value";
		  
		  var appendHere = $this.parents(".aLabel").siblings();
		  
		  var fullDate = new moment();
		  
		  var todaysDate = fullDate.format("YYYY-MM-DD");
		  
		  
		  var underIndex = theID.indexOf("_");
		  var theColumn = theID.substring(0, underIndex);
		  var featureID = theID.substring(underIndex+1, theID.length);
		  
		  //Create an input based on the value they want to change
		  switch(theColumn){
			case "name":
			  var updateText = "<p>Enter the new Event Name: <span class='floatRight'><a href='#' id='killAjaxBox'><i class='icon-remove'></i></a></span></p>";
			  var newInput = "<input type='text' name='newUpdate' id='newUpdate'>";
			  break;
			case "date":
			  var updateText = "<p>Pick the new Date: <span class='floatRight'><a href='#' id='killAjaxBox'><i class='icon-remove'></i></a></span></p>";
			  var newInput = "<div id='datepicker_start2' class='input-append date datepicker' data-date-format='yyyy-mm-dd' data-date='" + todaysDate + "'><input type='text' name='newUpdate' id='newUpdate'><span class='add-on'><i class='icon-calendar'></i></span></div>";
			  break;
			case "start":
			  var updateText = "<p>Pick the new Start Time: <span class='floatRight'><a href='#' id='killAjaxBox'><i class='icon-remove'></i></a></span></p>";
			  var newInput = "<input type='text' name='newUpdate' id='newUpdate' value='' required>";
			  break;
			case "end":
			  var updateText = "<p>Pick the new End Time: <span class='floatRight'><a href='#' id='killAjaxBox'><i class='icon-remove'></i></a></span></p>";
			  var newInput = "<input type='text' name='newUpdate' id='newUpdate' value='' required>";
			  break;
			case "desc":
			  var updateText = "<p>Enter the new Text: <span class='floatRight'><a href='#' id='killAjaxBox'><i class='icon-remove'></i></a></span></p>";
			  var newInput = "<textarea name='newUpdate' id='newUpdate' rows='6'></textarea>";
			  break;
		  }
		  
		  var theHTML = "";
		  theHTML += "<div id='theAjaxUpdateDiv'>";
		  theHTML += updateText;
		  
		  theHTML += newInput;
		  
		  theHTML += "<p><a id='newUpdateSubmit' href='#' class='btn'>Update</a></p>";
		  
		  theHTML += "</div>";
		  
		  appendHere.append(theHTML);
		  
		  var now = new moment().set('hour', 0);
		  
		  var dateStart = $("#datepicker_start2").datepicker({
			onRender: function(date){
			  //console.log(date.valueOf() < now.valueOf() ? 'disabled' : '');
			  return date.valueOf() < now.valueOf() ? 'disabled' : '';
			}
		  }).on('changeDate', function(ev){
			dateStart.hide();
		  }).data('datepicker');
		  
		  $('#newUpdate').timepicker({
				showLeadingZero: false,
				showPeriod: true,
		  });
			
		});
		
		$("body").on("click", "#newUpdateSubmit", function(e){
		  e.preventDefault();
		  var $theParent = $(this).parents("tr");
		  var theID = $(".aLabel span a", $theParent).attr("id");
		  
		  var updateLocation = $(this).parents("tr").children("td")[1];
		  //console.log($updateLocation);
		  var underIndex = theID.indexOf("_");
		  var theColumn = theID.substring(0, underIndex);
		  
		  var theValue = $("#newUpdate").val();  
		  
		  //THIS IS THE CODE FOR UPDATING ONCE THE VALUE IS DETERMINED
		  $.ajax({
			type: "POST",
			url: "interactions.php",
			data: "action=updateEventValue&column_ID=" + theID + "&value=" + theValue,
			dataType: "json",
			success: function(theResponse){
			  var updateVal = theResponse['value'];
			  
			  $("#theAjaxUpdateDiv").remove();
			  $(updateLocation).text(updateVal).addClass("text-success").removeClass('text-error');
			  
			  
			  setTimeout(function(){
				$(updateLocation).removeClass("text-success");
			  }, 2000);
			}
		  });
		});
	  }
	  
   });
  </script>
  
<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/footer.php'); ?>
