<?php 
	require_once('common.php');
	checkUser();
	include("header.php");
	include("constants.php");
?>
		<!-- content -->
		<div id="content">
			<!-- table -->
			<div class="box">
				<!-- box / title -->
				<div class="title">
					<h5>Jimmy Clean's - Remove Email Addres</h5>
		
				</div>
				<!-- end box / title -->
				<div class="table">
					
				<form method="post" id="remove_email" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
				<div class="form">
					<div class="fields">
						
	
<?php
	  $dbc = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME)
				or die('Error connecting to MySQL server.');
      if (isset($_POST['submit'])) {
		if ($_POST['todelete'] == ''){
			echo '
			<div class="messages">
				<div id="message-error" class="message message-error">
					<div class="image">
						<img src="images/icons/error.png" alt="Error" height="32" />
					</div>
					<div class="text">
						<h6>Error Message</h6>
						<span>Nothing has been selected, please select an email address to delete</span>
					</div>
					<div class="dismiss">
						<a href="#message-error"></a>
					</div>
				</div>
			</div>
			';
		}
		
		else{
		foreach ($_POST['todelete'] as $delete_id) {
        $query = "DELETE FROM " . DB_TABLE . " WHERE email = '$delete_id'";
        mysqli_query($dbc, $query)
        or die('<div class="messages">
				<div id="message-error" class="message message-error">
					<div class="image">
						<img src="images/icons/error.png" alt="Error" height="32" />
					</div>
					<div class="text">
						<h6>Error Message</h6>
						<span>Nothing has been selected, please select an email address to delete</span>
					</div>
					<div class="dismiss">
						<a href="#message-error"></a>
					</div>
				</div>
			</div>');
        }
        echo '<div class="messages">
						<div id="message-success" class="message message-success">
							<div class="image">
								<img src="images/icons/success.png" alt="Success" height="32" />
							</div>
							<div class="text">
								<h6>Success</h6>
								<span>Email address has been remove<br/><br/></span>						
							</div>
							<div class="dismiss">
								<a href="#message-success"></a>
							</div>
			</div>
			</div>';
      }
	  }
?>
	<table>
						<thead>
							<tr>
								<th class="left"><input type='checkbox' name='checkall' onclick='checkedAll();'></th>
								<th>Name</th>
								<th>Email Address</th>
								
							</tr>
						</thead>
	<tbody>
	<?php
  $query = "SELECT * FROM " . DB_TABLE;
  $result = mysqli_query($dbc, $query);
  while ($row = mysqli_fetch_array($result)) {
	$name = $row['first_name'] . ' ' . $row['last_name'];
	
	echo '<tr>';
	echo '<td>';
	echo '<input type="checkbox" value="' . $row['email'] . '" name="todelete[]" />';
	echo '</td>';
	
	echo '<td>';
	echo $name;
	echo '</td>';
	

	echo '<td>';
	echo  $row['email'] ;
	echo '</td>';
	echo '</tr>';
  }

  mysqli_close($dbc);
?>
</tbody>
</table>

	<div class="remove_buttons buttons">
									<div class="highlight">
<input type="submit" id="submit-button" class="add_email" name="submit" value="Remove Addresses">
									</div>
								</div>	
								
								
</form>	
					
					
					
					
					
					
				</div>
			</div>
			<!-- end table -->
			
		</div>
		</div>
			
			
			<!-- end box / right -->
		<!-- end content -->
		<?php
include("footer.php");
?>