<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);

/*DEPRECATED Use version 2*/
/*The following code pulls data for the sent Android ID and sends the data back*/

// array for JSON response (default to failure)
$response = array();
$response["success"] = 0;
$response["message"] = "Failed";

// check for required fields
if (isset($_POST['androidId'])){
	// include db config class
	require_once __DIR__ . '/db_config.php';
	
	// connecting to db
    	$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
    	
    	//get android id
	$androidId = $_POST['androidId'];
	
	//get data from sync table
	if($stmt = $mysqli->prepare("SELECT TABLE_NAME, ROW_ID, TYPE, USER_ID FROM SYNC_TABLE WHERE ANDROID_ID = ? ORDER BY COUNTER")){
		$sync_data = array();
		$stmt->bind_param("s", $androidId);
		$stmt->execute(); 
		//storing result since we are running another query
		$stmt->store_result();
		
		$stmt->bind_result($temp_table, $temp_row_id, $temp_type, $temp_user_id);		
		while($stmt->fetch()){
			$table = $temp_table;
			$row_id = $temp_row_id;
			$type = $temp_type;
			$user_id = $temp_user_id;

			//get row from table if table is not Settings or Alarm
			if(strcmp($table, "SETTINGS")!=0 && strcmp($table, "ALARM")!=0){
				if($result = $mysqli->query("SELECT * FROM " . $table . " WHERE USER_ID = " . $user_id . " AND _ID = " . $row_id)){
					$row = array();
					$row["table"] = $table;
					$row["type"] = $type;
					$row["_ID"] = $row_id;
							
					$i = 0;
					while ($temp_row = $result->fetch_row()){
						while ($finfo = mysqli_fetch_field($result)) {
							//add all columns to row[] except _ID (since we have added it already) and USER_ID (since not needed)
							if(strcmp($finfo->name, "_ID")!=0 && strcmp($finfo->name, "USER_ID")!=0){
								$row[$finfo->name] = $temp_row[$i];
							}
								
							++$i;
						}
					}
					
					//if vehicle has image then send that as well
					if(strcmp($table, "VEH_TABLE")==0){
						$pic = $row["PICTURE"];

						if(!empty($pic)){
							$file_path = "../FuelBuddyImages/";
							$file_path = $file_path . $pic; 
							$row["PIC"] = base64_encode(file_get_contents($file_path));
						}
						else{
							$row["PIC"] = "";
						}
					}
					
					//if receipt then need to send image as well
					if(strcmp($table, "LOG_TABLE")==0){
						//check if receipt should be attached
						$pic = $row["RECEIPT"];

						if(!empty($pic)){
							$file_path = "../FuelBuddyReceipts/";
							$file_path = $file_path . $pic; 
							$row["PIC"] = base64_encode(file_get_contents($file_path));
						}
						else{
							$row["PIC"] = "";
						}							
					}
					
					//safety check to send data only if type is delete or row has more than 3 items
					//prevents sending data when row is not found found for add or update
					if(strcmp($type, "del")==0 || count($row)>3){
						array_push($sync_data, $row);
					}																
						
					if($result != null){
						$result->close();
					}														
				}
			}
			//else table is Settings
			else if(strcmp($table, "SETTINGS")==0){
				if($result = $mysqli->query("SELECT * FROM SETTINGS WHERE USER_ID = " . $user_id)){
					$row = array();
					$row["table"] = $table;
					$row["type"] = $type;
					$row["_ID"] = $row_id;
					
					$i = 0;
					while ($temp_row = $result->fetch_row()){
						while ($finfo = mysqli_fetch_field($result)) {
							//add all columns to row[] except USER_ID (since not needed)
							if(strcmp($finfo->name, "USER_ID")!=0){
								$row[$finfo->name] = $temp_row[$i];
							}
								
							++$i;
						}
					}
					
					//safety check to send data only if row has more than 3 items
					//prevents sending data when row is not found
					if(count($row)>3){
						array_push($sync_data, $row);
					}
						
					if($result != null){
						$result->close();
					}
				}
			}
			//else table is ALARM
			else{
				$row = array();
				$row["table"] = $table;
				$row["type"] = $type;
				$row["_ID"] = $row_id;
				
				array_push($sync_data, $row);
			}				
		}
		
		$response["success"] = 1;
		$response["message"] = "Success";
		$response["sync_data"] = $sync_data;
		
		// echoing JSON response
		echo json_encode($response);
		
		// Close the prepared statement.
		if($stmt != null){
	    		$stmt->close(); 
	    	} 
	}
	
	//else send generic failure message
	if($response["success"] == 0){
		$response["success"] = 0;
		$response["message"] = "Failed";
								 
		// echoing JSON response
		echo json_encode($response);
	}
	
	$mysqli->close();
}
else{
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}

?>