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

/*The following code deletes the sent rescord from SYNC_TABLE*/

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

//get json string
$json = file_get_contents('php://input');

//get json array
$jsonArr = json_decode($json, true);

// check for required fields
if (count($jsonArr)>0){
	// include db config class
	require_once __DIR__ . '/db_config.php';
	
	// connecting to db
    	$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE); 
    	mysqli_set_charset($mysqli, "utf8");
    	
    	//for each row in the array
    	foreach($jsonArr as $row) {
    		//get values
    		$tableName = $row['tableName'];
    		$rowID = $row['rowID'];
		$type = $row['type'];
		$androidId = $row['androidId'];
			
		//delete row from sync table
		$stmt = $mysqli->prepare("DELETE FROM SYNC_TABLE WHERE TABLE_NAME = ? AND ROW_ID = ? AND TYPE = ? AND ANDROID_ID = ?");
		$stmt->bind_param("siss", $tableName, $rowID, $type, $androidId);
		$result = $stmt->execute();		
	}
	
	// Close the prepared statement.
	if($stmt != null){
		$stmt->close(); 
	}
		    	
	//send success message
	$response["success"] = 1;
	$response["message"] = "Success";
					
	// echoing JSON response
	echo json_encode($response);	
	
	mysqli_close($mysqli);
}
else{
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}
?>