<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}


//require_once ('mysql_connect.php');
$action          = $_GET["a"];

	//echo 'Action = ' . $action; (for testing)
//require_once ('config.inc');

if ($action == "add") {
			
			$query = "Insert into coupon (title, body, footer, expiration) Values ('" . $_POST['title'] . "', '" .  $_POST['body']  . "', '" .  $_POST['footer'] . "', '" .  $_POST['expiration'] . "' )";

			if (!mysql_query($query)){
					echo $query;
  					die('Error: ' . mysql_error());
 			 } else {
				echo "<div align=center><font color=red>Coupon added successfully.</font></div>";
				
			}
		}

if ($action == "update") {
$checkbox = $_POST['chkdelete'];
$active = $_POST['chkactive'];
$online = $_POST['chkonline'];


	if (empty($checkbox)) {
		//delete not checked, save other updated info

	
		
		$query = "UPDATE coupon SET title = '" . $_POST['title'] . "',  body = '" . $_POST['body'] . "', footer= '" . $_POST['footer'] . "', expiration = '" . $_POST['expiration'] .  "', Active ='";
		if (empty($active)){
			$query = $query . "0'";
		} else {
			$query = $query . "1'";
		}
$query .= ", onlineOnly ='";	
		if (empty($online)){
			$query = $query . "0'";
		} else {
			$query = $query . "1'";
		}	
$query .= ", code ='" . $_POST['code'] . "'";
$query .= ", discountAmount ='" . $_POST['discount'] . "'";
$query .= ", discountPercent ='" . $_POST['percent'] . "'";
$query .= ", couponPrice ='" . $_POST['price'] . "'";


		$query = $query . " WHERE couponNumber = '" . $_GET['key'] ."'" ;
		//echo $query;
		mysql_query($query) or die(mysql_error());   //execute query

		//confirmation message that delete worked...
		echo "<font color=red>Coupon updated successfully.</font>";

		

	} else { 
		
		
	$query = "DELETE FROM coupon WHERE couponNumber = '" . $_GET["key"] ."'" ;
	
	

	mysql_query($query) or die(mysql_error());   //execute query

	//confirmation message that delete worked...
	echo "<font color=red>Coupon deleted successfully.</font>";

		}
	}	//end of update query

		$query = "SELECT * from coupon";


		$rt=mysql_query($query);          // query executed 
		echo mysql_error();                    // if any error is there that will be printed to the screen



echo '<br><div align="center"><a href="index.php"><font color="blue">Back to Main Page</font></a>';


echo '<div align="center"><table border=0 cellpadding=0 cellspacing=8>';

while($nt=mysql_fetch_array($rt)){

	echo '<tr><td><div align=left>Coupon #:<td valign=top><div align=left><input type="text" name="number" size="40" value="' . $nt[couponNumber] . '" disabled="disabled">';



	echo '<tr><td><form action="' . $_SERVER['PHP_SELF'] . '?a=update&key=' . $nt[couponNumber] . '" method="post"><tr><td><div align=left>Title:<td valign="top"><div align=left><input type="text" name="title" size="40" value="' . $nt[title] . '">';  

	echo '<tr><td><div align=left>Online code#:<td valign=top><div align=left><input type="text" name="code" size="40" value="' . $nt[code] . '">';

echo '<tr><td><div align=left>Online only:<td valign=top><div align=left><input type="checkbox" name="chkonline"';
	if(!empty($nt[onlineOnly])){
		echo 'checked=true';
	} 
echo '>';

	echo '<tr><td><div align=left>Coupon Price:<td valign=top><div align=left><input type="text" name="price" size="40" value="' . $nt[couponPrice] . '">';

	echo '<tr><td><div align=left>Discount amount:<td valign=top><div align=left><input type="text" name="discount" size="40" value="' . $nt[discountAmount] . '">';

	echo '<tr><td><div align=left>Discount percent:<td valign=top><div align=left><input type="text" name="percent" size="40" value="' . $nt[discountPercent] . '">';	
	
	echo '<tr><td><div align=left>Body:<td valign=top><div align=left><textarea cols=42 rows=3 name="body">' . $nt[body] . ' </textarea>';  

	echo '<tr><td><div align=left>Footer:<td valign=top><div align=left><input type="text" name="footer" size="40" value="' . $nt[footer] . '">';  

	echo '<tr><td><div align=left>Expiration date:<td valign=top><div align=left><input type="text" name="expiration" size="10" value="' . $nt[expiration] . '">';

echo '<tr><td colspan=2>';



echo '<tr><td><div align=left>Active:<td valign=top><div align=left><input type="checkbox" name="chkactive"';
	if(!empty($nt[active])){
		echo 'checked=true';
	} 
echo '>';



echo '<tr><td valign=top><div align=left>Delete:<td valign=top><div align=left><input type="checkbox" name="chkdelete" value="true">';  

echo '<tr><td colspan=2>';

include ('couponmenu.php');

	echo '<tr><td><td valign=top><div align=center><input type="submit" name="save" value="Save"></form><tr><td colspan=2><img src="images/coupon-line.jpg">';
		
}

echo '</table><br><br>';


	
		

	echo '    <form action="' . $_SERVER['PHP_SELF'] . '?a=add" method="post">Add new coupon: (title) <br><input type="text" name="title" size=50>

		<input type="submit" name="save" value="Save">
		</form>
    <br><a href="index.php"><font color="blue">Return to Main Page</font></a>
		
		</div></table></div>';
	

	

?>