<?php
//get variables from urlstring
$recordkey = $_GET['recordkey'];
$prep = $_GET['prep'];	//being prepared
$oven = $_GET['oven'];	//in oven
$box = $_GET['box'];	//boxed and ready
$out = $_GET['out'];	//out for delivery
$del = $_GET['del'];	//delivered
$date = date('Y/m/d H:i:s');


if(!empty($recordkey)){	//order selected for update...apply update here
//echo "<br>record=" . $recordkey;
	if(!empty($out)){	//change out for delivery status
		if($out==2){ $out = "0000-00-00 00:00:00";} else {$out = $date;}
		$update = "update orders set outForDel ='$out', delivered_by = '".$_SESSION['recordKey']."' where recordKey = '$recordkey'";
		//echo "qry=" . $update;
		mysql_query($update);	
	}

	if(!empty($del)){	//change delivered status
		if($del==2){ $del = "0000-00-00 00:00:00";} else {$del = $date;}
		$update = "update orders set delivered ='$del', delivered_by = '".$_SESSION['recordKey']."' where recordKey = '$recordkey'";
		//echo "qry=" . $update;
		mysql_query($update);
	}
	
	if(!empty($prep)){	//change being prepared status
		if($prep==2){ $prep = "0000-00-00 00:00:00";} else {$prep = $date;}
		$update = "update orders set in_production ='$prep', prepared_by = '".$_SESSION['recordKey']."' where recordKey = '$recordkey'";
		//echo "qry=" . $update;
		mysql_query($update);	
	}
	
	if(!empty($oven)){	//change being prepared status
		if($oven==2){ $oven = "0000-00-00 00:00:00";} else {$oven = $date;}
		$update = "update orders set in_oven ='$oven', baked_by = '".$_SESSION['recordKey']."' where recordKey = '$recordkey'";
		//echo "qry=" . $update;
		mysql_query($update);	
	}
	
	if(!empty($box)){	//change boxed status
	//echo "<br>box=" . $box;
		if($box==2){ $box = "0000-00-00 00:00:00";} else {$box = $date;}
		$update = "update orders set boxed ='$box', boxed_by = '".$_SESSION['recordKey']."' where recordKey = '$recordkey'";
		//echo "qry=" . $update;
		mysql_query($update);	
	}
}

if ((isset($_POST['submit']))||($autologin==1)) {
	//get username and pwd posted to form
	if (empty($_POST['username'])) {
		$uu = FALSE;
		$message .= '<p>Please enter a User ID.</p>';
	} else {
		$uu = mysql_real_escape_string($_POST['username']);
	}
	
	if (empty($_POST['password'])) {
		$pp = FALSE;
		$message .= '<p>Please enter your password.</p>';
	} else {
		$pp = mysql_real_escape_string($_POST['password']);
	}
	//use name and pwd from autologin form if used
	if (!empty($_GET['x'])) {
		$uu = mysql_real_escape_string($_GET['x']);
	}
	
	if (!empty($_GET['y'])) {
		$pp = mysql_real_escape_string($_GET['y']);
	}

//echo "<br>uu=" . $uu;
	//echo "<br>pp=" . $pp;
	
	if ($uu && $pp) { //if ID and password are ok
		$query = "SELECT userID, firstName, isAdmin, recordKey, rolAdmin, isDriver, isChef, lastName FROM admin WHERE UserID='$uu' AND password=PASSWORD('$pp') AND active='1'";
	//echo $query;
		$result = @mysql_query ($query);
		$row = mysql_fetch_array ($result, MYSQL_NUM);
		if ($row) { //match was found
			// Start the session, register values & redirect
			$_SESSION['FirstName'] = $row[1];
			$_SESSION['UserID'] = $row[0];
			$_SESSION['admin'] =$row[2];
			$_SESSION['adminkey'] ="security";
			$_SESSION['recordKey'] =$row[3];
			$_SESSION['roladmin'] =$row[4];
			$_SESSION['driver'] =$row[5];
			$_SESSION['chef'] =$row[6];
			$_SESSION['LastName'] =$row[7];
			//any action here happens upon login only

			} else {
				echo '<p>The user ID and password entered do not match those on file, or the user account is no longer active.</p>';
				$_SESSION['admin'] =0;
			}
		
		mysql_close(); //close db connection
		
	} else {	//if everything was not ok
		echo '<p>Please try again.</p>';
	}


}	//end of submit conditional
	if (($_SESSION['chef']==1)||($_SESSION['driver']==1)||($_SESSION['admin']==1))  {
	//user is logged in
	
	//$todays_date =  strtotime(date("Y-m-d"));
	//$todays_date= date("d M Y");
	$todays_date = time();
	
	include ('includes/db1config.php'); //db connection config file
    include ('../includes/mysql_connect.php'); //connect using selected db config file
			echo "<div align=center>
			<table width=95% border=0><td><div align=center>Logged in as: {$_SESSION['LastName']}, {$_SESSION['FirstName']}";
			if($_SESSION['admin']==1){
				echo " (Admin)";
				$permission = "admin";
			} elseif($_SESSION['chef']==1){
				echo " (Cook)";
				$permission = "cook";
			}elseif($_SESSION['driver']==1){
				echo " (Driver)";
				$permission = "driver";
			}
			echo"<br>";
			echo "<a href='logout.php'><font color='blue'>Log out</font></a><br><br>";
			
			//show list of any unarchived orders, based on logged in user permission
			
			$query = "Select * from orders where UNIX_TIMESTAMP(orderTime) > " . ($todays_date -86400);	//86400 is 1 day...shows all orders in the last 1 day
			if($permission == "driver"){
				$query .= " AND delivery=1 AND boxed > '0000-00-00 00:00:00'";	//only show delivery orders if logged in user is a driver
			}
			
			if($permission == "cook"){
				$query .= " AND delivered = '0000-00-00 00:00:00'";	//only show orders not delivered yet
			}
			
			//echo "<br>qry= ". $query . "<br>";
			echo '<div class="arrowlistmenu">';	//BEGINNING OF EXPANDABLE MENU
			$rt=mysql_query($query);
			while($nt=mysql_fetch_array($rt)){
				$order = $nt[orderNumber];
				$outfordel = $nt[outForDel];
				$delivered = $nt[delivered];
				$boxed = $nt[boxed];
				$inoven = $nt[in_oven];
				$preparing = $nt[in_production];
				$recordkey = $nt[recordKey];
				$payment = $nt[payMethod];
				
				$qry2 = "select LastName, CompanyName, Address1, Address2, City, State, Zip, addressType, Phone from guestLogin where UserKey = '" .$nt[UserKey]. "'";
				$rt2=mysql_query($qry2);
				//echo "<br>qry2= ". $qry2 . "<br>";
				while($nt2=mysql_fetch_array($rt2)){
					$last = $nt2[LastName];
					$company = $nt2[CompanyName];
					$address1 = $nt2[Address1];
					$address2 = $nt2[Address2];
					$city = $nt2[City];
					$state = $nt2[State];
					$zip = $nt2[Zip];
					$type = $nt2[addressType];
					$phone = $nt2[Phone];
				}
				mysql_close;
				
				
				echo "<h3 class='menuheader expandable'>Order#: " . $order . " " . $payment . " " . $address1 ."</h3>";
				echo'<ul class="categoryitems">';
				if(!empty($company)){echo "<br>Biz: " . $company;}
				if(!empty($last)){echo "<br>Name: " . $last;}
				if(!empty($address1)){echo "<br>" . $address1;}
				if(!empty($address2)){echo "<br>" . $address2;}
				if(!empty($city)){echo "<br>" . $city . ", " . $state . " " . $zip;}
				if(!empty($type)){echo "<br>(" . $type . ")";}
				if(!empty($phone)){echo "<br><a href='tel:" . $phone . "'>". $phone . "</a>";}
				if(($permission == "cook")||($permission == "admin")){
					//show link to update 'being prepared' status
					if($preparing == "0000-00-00 00:00:00"){
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&prep=1'>Mark as being prepared</a>";
					} else {
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&prep=2'>Unmark as being prepared</a>";
					}
					//show link to update 'in oven' status
					if($inoven == "0000-00-00 00:00:00"){
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&oven=1'>Mark as in oven</a>";
					} else {
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&oven=2'>Unmark as in oven</a>";
					}
					//show link to update 'ready/boxed' status
					if($boxed == "0000-00-00 00:00:00"){
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&box=1'>Mark as ready</a>";
					} else {
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&box=2'>Unmark as ready</a>";
					}
				}
				
				if(($permission == "driver")||($permission == "admin")){
					//show link to update 'out for delivery' status
					if($outfordel == "0000-00-00 00:00:00"){
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&out=1'>Mark as out for delivery</a>";
					} else {
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&out=2'>Unmark as out for delivery</a>";
					}
					//show link to update 'delivered' status
					if($delivered == "0000-00-00 00:00:00"){
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&del=1'>Mark as delivered</a>";
					} else {
						echo "<br><a href='".$_SERVER['PHP_SELF']."?recordkey=$recordkey&del=2'>Unmark as delivered</a>";
					}
				}
				echo'</ul>';
			}
			mysql_close;
			echo"</div>";	//END OF EXPANDABLE MENU
			
			


  
			

			

	} else {
		echo '<body onload="setFocus()"><div align=center><form action="';

		echo $_SERVER['PHP_SELF']; 


		echo '" method="post">
		<div align="center">
		User ID:<br>
		<input type="text" name="username" id="username" size=40 maxlength=40 value="';

		if (isset($_POST['username'])) echo $POST['username']; 

		echo '" />
		<br>
		Password:<br>
		<input type="password" name="password" size=40 maxlength=15 />
		<br><br>
		<input type="submit" name="submit" value="Login" /></div>
		</form>';
	}
	echo '</div>';

?>