<?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;
	}
$query = "SELECT * from storePrefs";
$rt=mysql_query($query);
while($nt=mysql_fetch_array($rt)){
	$tastepos_enabled=$nt[tastepos_enabled];
	$tastepos_subscribed=$nt[tastepos_subscribed];
}

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

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


if ($action == "add") {
			
			$query = "Insert into admin (userID, email) Values ('" . $_POST['userID'] . "', '" . $_POST['userID'] . "' )";

			if (!mysql_query($query)){
					//echo $query;
  					die('Error: ' . mysql_error());
 			 } else {
				echo "<div align=center><font color=red>User added successfully. Please enter the rest of their information to activate the account.</font></div>";
				
			}
			
			$selectedUserId = mysql_insert_id();
		}

if ($action == "update") {
	$checkbox = $_POST['chkdelete'];
	$admin = $_POST['chkadmin'];
	$driver = $_POST['chkdriver'];
	$chef = $_POST['chkchef'];
	$scheduled = (isset($_POST['chkscheduled']) && $_POST['chkscheduled'] == 'true' ? 1 : 0);
	$active = $_POST['chkactive'];
	$tastepos_login_id = $_POST['tastepos_login_id'];
	//clear $tastepos_login_id if it contains anything other than numbers
	if(is_numeric($tastepos_login_id)==false){$tastepos_login_id="";}

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

	
		
		$query = "UPDATE admin SET userID= '" . $_POST['userID'] . "', email= '" . $_POST['email'] . "', firstName = '" . $_POST['firstname'] . "', lastName= '" . $_POST['lastname'] . "'";
			if ($_POST['password'] !==""){
				$query = $query . ", password=PASSWORD('" . $_POST['password'] . "')";
			}

		$query = $query .  ", Active ='";
		if (empty($active)){
			$query = $query . "0'";
		} else {
			$query = $query . "1'";
		}
		
		$query = $query .  ", isAdmin ='";
		if (empty($admin)){
			$query = $query . "0'";
		} else {
			$query = $query . "1'";
		}
		
		$query = $query .  ", isDriver ='";
		if (empty($driver)){
			$query = $query . "0'";
		} else {
			$query = $query . "1'";
		}
		
		$query = $query .  ", isChef ='";
		if (empty($chef)){
			$query = $query . "0'";
		} else {
			$query = $query . "1'";
		}

		$query .= ", scheduled = '".$scheduled."'";
		$query .= ", tastepos_login_id = '".$tastepos_login_id."'";

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

		//confirmation message that update worked...
		echo "<center><font color=red>User updated successfully.</font></center>";

		

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

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

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

		}
}	//end of update query
	
	if ($_SESSION['admin'] == "1") {	//if not system admin, only view own record, otherwise view all users
			$query = "SELECT * from admin where masterAdmin='0'";
		} else {
			$query = "SELECT * from admin where recordKey ='" . $_SESSION['recordKey'] . "'" ;

		}

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

if (isset($_GET['key']) && !empty($_GET['key'])) {
	$selectedUserId = $_GET['key'];
}

echo '<div id="single-admin-button">
<ul>
<a href="index.php"><li>BACK TO HOME ADMIN</li></a>
</ul>
</div>';


echo '<div align="center">';

if ($_SESSION['admin'] == "1") {
	echo'<form action="' . $_SERVER['PHP_SELF'] . '?a=add" method="post">Add new user: (email address) <br><input type="text" name="userID" size=50>
	<input type="submit" name="save" value="Save">
	</form><br> OR <br><br>';
} else {
	$selectedUserId = $_SESSION['recordKey'];
}

echo 'Edit user: <select id="edit-user">';
echo '<option value="">-- Select User --</option>';
while($nt=mysql_fetch_array($rt)){
	echo '<option value="'.$nt['recordKey'].'"';
	if($selectedUserId==$nt['recordKey']){echo " selected='selected'";}
	echo'>'.$nt['userID'].' ('.$nt['firstName'].' '.$nt['lastName'].')</option>';
}
echo '</select>';
echo '<input id="save-button" type="submit" name="select" value="Edit" onclick="editUser(); return false;">';



if (!empty($selectedUserId)) {

	echo '<table border=0 cellpadding=0 cellspacing=8>';

	mysql_data_seek($rt, 0);
	while($nt=mysql_fetch_array($rt)){
		if ($nt['recordKey'] != $selectedUserId) {
			continue;
		}
		echo '<tr><td><form id="edit-form" action="' . $_SERVER['PHP_SELF'] . '?a=update&key=' . $nt[recordKey] . '" method="post" onsubmit="saveUser(); ">';
		echo '<tr><td><div align=left>User #:<td valign=top><div align=left><input type="text" name="body" size="40" value="' . $nt[recordKey] . '" disabled="disabled">';
		echo '<tr><td><div align=left>User ID:<td valign="top"><div align=left><input id="userID" type="text" name="userID" size="40" value="' . $nt[userID] . '">';  

		echo '<tr><td><div align=left>Email:<td valign="top"><div align=left><input id="email" type="text" name="email" size="40" value="' . $nt[email] . '">';  
		
		echo '<tr><td><div align=left>First name:<td valign=top><div align=left><input id="firstname" type="text" name="firstname" size="40" value="' . $nt[firstName] . '">';

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

		echo '<tr><td><div align=left>Password:<td valign=top><div align=left><input id="password" type="text" name="password" size="40" value="">';
		if(	$tastepos_enabled=='1' && $tastepos_subscribed=='1'){
		if($nt[tastepos_login_id]=='0'){
			$tastepos_login_id="";
		} else {
			$tastepos_login_id=$nt[tastepos_login_id];
		}
		echo '<tr><td><div align=left>Taste POS Login:<td valign=top><div align=left><input id="tastepos_login_id" type="text" name="tastepos_login_id" size="5" value="' . $tastepos_login_id . '"> (* A string of 4 numbers, like 1234.)';
		}
	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>Roles: <td><div align=left>Admin <input type="checkbox" name="chkadmin"';

	if ($_SESSION['admin'] !== "1"){	//disable checkbox if logged in user is not administrator
			echo ' disabled=disabled ';
			}

		if($nt[isAdmin] =="1"){
			echo 'checked=true';
			
		} 
	echo '>';
	if($restaurant_pkg=='1'){
		echo '&nbsp;&nbsp;&nbsp;&nbsp;Driver <input type="checkbox" name="chkdriver"';

		if ($_SESSION['admin'] !== "1"){	//disable checkbox if logged in user is not administrator
				echo ' disabled=disabled ';
				}

			if($nt[isDriver] =="1"){
				echo 'checked=true';
				
			} 
		echo '>';
	}
	if($restaurant_pkg=='1'){
		echo '&nbsp;&nbsp;&nbsp;&nbsp;Food Prep <input type="checkbox" name="chkchef"';

		if ($_SESSION['admin'] !== "1"){	//disable checkbox if logged in user is not administrator
				echo ' disabled=disabled ';
				}

			if($nt[isChef] =="1"){
				echo 'checked=true';
				
			} 
		echo '>';
	}
	echo '&nbsp;&nbsp;&nbsp;&nbsp;Scheduled <input type="checkbox" name="chkscheduled" value="true" ';

	if ($_SESSION['admin'] !== "1"){	//disable checkbox if logged in user is not administrator
			echo ' disabled=disabled ';
			}

		if($nt[scheduled] =="1"){
			echo 'checked=true';
			
		} 
	echo '>';

		echo '<tr>';
		echo '<td><input id="save-button" type="submit" name="save" value="Delete User" onclick="deleteUser(); return false;"></td>';
		echo '<td valign=top><div align=right><input id="save-button" type="submit" name="save" value="Save" onclick="saveUser(); return false;"></form><tr><td colspan=2><hr />';
			
		echo '<form id="delete-form" action="' . $_SERVER['PHP_SELF'] . '?a=update&key=' . $nt[recordKey] . '" method="post">';
		echo '<input type="hidden" name="chkdelete" value="true" />';
		echo '</form>';
	}

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

}




	
		

	//echo '<br><i>Add new user ID and save. <br>Then you will be able to insert their name and password.</i><br><br><a href="index.php"><font color="blue">Return to Main Page</font></a>';
		
	echo'	</div></table></div>';
	

	

?>