<?php
  require('inc/sessionStart.php');
?>
<?php
  if(isset($_GET['download']) && isset($_GET['promo'])){
	$thePromo = $_GET['promo'];
	
	
	
	//require('inc/sessionStart.php');
	include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.features.inc.php");
	$storeObj = new stores();
	
	$theEntries = $storeObj->getNoPurchaseEntriesCSV($thePromo);
	
	header("Content-type: application/vnd.ms-excel");
	header("Content-disposition: attachment; filename=".$thePromo . "Entries.csv");
	header("Pragma: no-cache");
	header("Expires: 0");
	
	$data = "";
	foreach($theEntries as $anEntry){
	  $line = "";
	  foreach($anEntry as $value){
		$line .= '"' . $value . '",';
	  }
	  $data .= rtrim($line, ",") . "\n";
	}
	
	echo $data;
  }//end if(isset($_GET['download']) && isset($_GET['promo']))
  else{
	//require('inc/sessionStart.php');
?>

<?php
  $pageTitle = "Administration: No Purchase Entries";
  $pageDescription = "";
  $pageKeywords = "";
?>

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

  <style>
	#storePortal .portalContent .usersInfo table tr td{
	  height:15px;
	  padding:10px;
	}
  </style>
  
<?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'); ?>
		  </div>
		  
		  <div class="span9 portalContent">
			<div class="contentContain">
			  <div id="userContainer" class="clearfix">
				<h2>No Purchase Necessary Entries</h2>
				<?php
				  include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.features.inc.php");
				  $storeObj = new stores();
				  $theCount = 1;
				  
				  $thePromotions = $storeObj->getNoPurchasePromos();
				  $theLocations = $storeObj->getStoreLocations();
				  
				  foreach ($theLocations as $key => $row) {
					$storeIDarray[$key]  = $row['store_id'];
				  }
				  
				  array_multisort($storeIDarray, SORT_ASC, $theLocations);
				?>
				  <select id="giveawaySelect">
					<option value="None">Select a Promotion</option>
				<?php
				  foreach ($thePromotions as $aPromo){
				?>
					<option value="<?php echo $aPromo['thePromotion']; ?>"><?php echo $aPromo['thePromotion']; ?></option>
				<?php
				  }
				?>
				  </select>
				  <div id="promoContainer" class="hideObject">
					<span class="floatRight"><a href="#" id="csvDownload" class="btn">Download CSV file</a></span>
					<h4 id="promoName"></h4>
					<div class="usersInfo">
					  <table>
						<thead>
						  <tr>
							<td>Name</td>
							<td>Email Address</td>
							<td>Phone Number</td>
							<td>Location</td>
						  </tr>
						</thead>
						<tbody class="promotionResults">
						 
						</tbody>
					  </table>
					</div>
				  </div>
			  </div>
			</div>
		  </div>
		</div>
	    
      </section>
      
    </section>
      
    
  <script>
	var theLocations = <?php echo json_encode($theLocations); ?>;
	
	$("#giveawaySelect").on("change", function(){
	  var theWanted = $(this).find(":selected").text();
	  
	  $.ajax({
		type: "POST",
		url: "interactions.php",
		data: "action=getNoPurchaseEntries&value=" + theWanted,
		dataType: "json",
		success: function(theResponse){
		  var theHTML = "";
		  
		  $.each(theResponse, function(){
			theHTML += "<tr id='entry_" + this.theID + "'>";
			theHTML += "<td>" + this.userName + "</td>";
			theHTML += "<td>" + this.theEmail + "</td>";
			theHTML += "<td>" + this.thePhone + "</td>";
			theHTML += "<td>" + theLocations[(this.theLocation - 1)]['store_name'] + "</td>";
			theHTML += "</tr>";
		  });
		  
		  $("#promoName").html(theWanted);
		  $("#csvDownload").attr("href", "?download=csv&promo=" + theWanted);
		  $(".promotionResults").html(theHTML);
		  $("#promoContainer").removeClass("hideObject");
		  $(".usersInfo tbody tr:even").addClass("oddRowColor");
		}
	  });
	  
	});
	
  </script>
  
<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/footer.php'); ?>
<?php
  }//end if(!isset($_GET['download']))
?>
