<?php
  require('inc/sessionStart.php');
?>

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


<?php
  $pageTitle = "Administration: Online Store Directory Management";
  $pageDescription = "";
  $pageKeywords = "";
?>

<?php
  include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php");
?>

<style>
	  #mainContent .row .span12{
			width: 95%;
	  }
	  
	  .remLen{
			width:30px;
	  }
	  
	  #ingredientTable, #allDirections, .newDirectionDiv, .newIngredientDiv{
			margin-left:30px;
	  }
	  
	  .formTitles{
			font-size:18px;
			color:#c71c22;
			font-weight:bold;
			margin:12px 0 6px 0;
	  }
	  
	  .nicEdit-main img{
			width:250px;
			display: block;
	  }
</style>

<script src="/Kickstrap/js/jquery-ui-1.9.0.custom.min.js"></script>
<link href="/Kickstrap/css/smoothness/jquery-ui-1.9.0.custom.css" rel="stylesheet">

<script>
	  
</script>

<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/mainNav.php'); ?>
<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/admin/inc/class.features.inc.php'); ?>

<?php
	  $storeObj = new stores();
	  //IF THE FORM WAS SUBMITTED PROPERLY
	  if(isset($_POST['store_id'])){
			$storeObj->processDirectory();
	  }
?>

    
    <section class="container">
      <div id="blankBuffer"></div>
      <section class="container" id="storePortal">
	    <div class="row">
		  <div class="span3 sideNav">
			<?php include('inc/sideNav.php'); ?>
		  </div>
		  
		  <div class="span9 portalContent">
			<div class="contentContain">
				  <!--<h2>All Recipes</h2>-->
				  <h2 class="pull-left">Store Directory</h2>
				  <a href="directory.php?a=add" id="addDirectoryItem" class="btn btn-primary pull-right">Add a directory Item</a>
				  <div style="clear:both;"></div>
				  <div id="directoryContain">
						<?php
							  if($action == "base"){
						?>
							  <div id="updateText"></div>
							  <div class="tabbable">
									<ul class="nav nav-tabs" id="myTab">
						  
									<?php
										  $theLocations = $storeObj->getStoreLocations();
										  $count = 0;
										  
										  foreach($theLocations as $aLocation){
												if($count == 0){
													  $theClass = "active";
												}else{
													  $theClass = "";
												}
									?>
										  <li class="<?php echo $theClass; ?>">
												<a href="#<?php echo str_replace(" ", "_", $aLocation['store_name']); ?>" data-toggle="tab"><?php echo $aLocation['store_name']; ?></a>
										  </li>
									<?php
												$count++;
										  }
										  
										  $count = 0;
									?>
									
									</ul>
									
									<div class="tab-content" id="myTabContent">
										  <?php
												foreach($theLocations as $aLocation){
													  if($count == 0){
															$theClass = " active";
													  }else{
															$theClass = "";
													  }
										  ?>
												<div class="tab-pane<?php echo $theClass; ?>" id="<?php echo str_replace(" ", "_", $aLocation['store_name']); ?>">
													  <?php
															$allItems = $storeObj->getDirectoryItemsStore($aLocation['store_id']);
													  ?>
															<form action="directory.php" method="post" id="directoryForm">
																  <input type="hidden" name="store_id" value="<?php echo $aLocation['store_id']; ?>">
																  <table>
																		<thead>
																			  <tr>
																					<td>&nbsp;</td>
																					<td>Item Name</td>
																					<td>Aisle Number</td>
																			  </tr>
																		</thead>
																		
																		<?php
																			  foreach($allItems as $anItem){
																		?>
																			  <tr>
																					<td><i class="icon-remove removeDirItem" id="directory_<?php echo $anItem['item_id']; ?>"> &nbsp;</i></td>
																					<td><?php echo $anItem['item_name']; ?></td>
																					<td>
																						  <input type="text" name="item[<?php echo $anItem['item_id']; ?>]" class="span2" value="<?php echo $anItem['aisle']; ?>">
																					</td>
																			  </tr>
																		<?php
																			  }
																		?>
																  </table>
																  
																  <input type='submit' id='submit' name='submit' value='Update' class="btn btn-primary"/>
															</form>
												</div>
										  <?php
													  $count++;
												}
										  ?>
									</div>
							  </div>
						<?php
							  }//end if($action == "base"){
							  else if($action == "add"){
						?>
							  <div id="addUpdateText"></div>
							  <label for="newDireItem">Item Name to add</label><input name="newDirItem" id="newDirItem">
							  <br><br><p><a href="#" class="btn" id="dirBtnSubmit">Add Item</a> <a href="directory.php" class="btn">Go Back</a></p>
						<?php
							  }
						?>
						
				  </div>
			</div>
			
			
			  
				  
		  <!--</form>-->
	    </div>
	    
	    
      </section>
      
    </section>
      
    
  
  <script>
	  
	  $("#dirBtnSubmit").bind("click", function(e){
			e.preventDefault();
			var itemName = $("#newDirItem").val();
			
			$.ajax({
				  type: "POST",
				  url: "interactions.php",
				  data: "action=newDirItem&itemName=" + itemName,
				  dataType: "json",
				  success: function(theResponse){
						if(theResponse == true){
							  $("#addUpdateText").text("Your item was added successfully.").removeClass("text-error").addClass("text-success");
						}else{
							  $("#addUpdateText").text("Your item could not be added to the database").removeClass("text-success").addClass("text-error");
						}
						
						$("#newDirItem").val("");
						
						setTimeout(function(){
							  $("#addUpdateText").text("");
						}, 2500);
				  }
			});
	  });
	  
	  $("td").on("click", ".removeDirItem", function(){
		var $this = $(this);
		var theID = $this.attr("id").replace("directory_", "");
		//console.log("clicked " + theID);
		
		$.ajax({
		  type: "POST",
		  url: "interactions.php",
		  data: "action=deleteDirItem&itemID=" + theID,
		  dataType: "json",
		  success: function(theResponse){
			if(theResponse == true){
				  $this.closest("tr").addClass("alert");
				  
				  setTimeout(function(){
					$this.closest("tr").remove();
				  }, 2500);
			}else{
				  var $theAdded = $this.closest("tr").append("<td class='alert'>The item could not be deleted.</td>");
				  setTimeout(function(){
					$theAdded.remove();
				  }, 2500);
			}
		  }
		});
	  });
  </script>
  
<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/footer.php'); ?>
