<?php
ob_start();
session_start();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");

if(empty($_POST["msgKey"]) || empty($_POST["status"]))
{
	echo 2;
	exit();
}
$msgKey = $_POST['msgKey'];
$status = (int) $_POST['status'];
if(empty($status))
{
	echo 2;
	exit();
}
if(mysqli_query($conn, "START TRANSACTION") )
{
	$mysqlError = "";
	$queryCheckMsgs = "SELECT 1 FROM `temp_history` WHERE `msg_key`='$msgKey' AND `approved`=0";
	$resultCheckMsgs = mysqli_query($conn, $queryCheckMsgs);
	if(!$resultCheckMsgs)
		$mysqlError.= mysqli_error($conn);
	
	if( mysqli_num_rows($resultCheckMsgs) )
	{
		if($status == 1)
		{
			$queryApprovedMsg = "UPDATE `temp_history` SET `approved`=1,`approved_time`=".time()." WHERE `msg_key`='".$msgKey."'";
			$resultApprovedMsg = mysqli_query($conn, $queryApprovedMsg);
			if(!$resultApprovedMsg)
				$mysqlError.= mysqli_error($conn);		
		}
		elseif($status == 2)
		{
			$queryGetMsgs = "SELECT `points`, `user_id` FROM `temp_history` WHERE `msg_key`='$msgKey' AND `approved`=0";
			$resultGetMsgs = mysqli_query($conn, $queryGetMsgs);
			if(!$resultGetMsgs)
				$mysqlError.= mysqli_error($conn);
			else
			{
				if( mysqli_num_rows($resultGetMsgs) )
				{
					/*while( $rowGetMsgs = mysqli_fetch_assoc($resultGetMsgs) )
					{
						$queryUpdateAccount = "UPDATE `user_login` SET `account`=`account`+$rowGetMsgs[points] WHERE `id`=$rowGetMsgs[user_id]";
						$resultUpdateAccount = mysqli_query($conn, $queryUpdateAccount);
						if(!$resultUpdateAccount)
							$mysqlError.= mysqli_error($conn);
						$queryUpdateAccount = "UPDATE `user_login` SET `account`=`account`+$rowGetMsgs[points] WHERE `id`='1'";
						$resultUpdateAccount = mysqli_query($conn, $queryUpdateAccount);
						if(!$resultUpdateAccount)
							$mysqlError.= mysqli_error($conn);	
					}*/

					$queryDelMsgs = "DELETE FROM `temp_history` WHERE `msg_key`='$msgKey' AND `approved`=0";
					$resultDelMsgs = mysqli_query($conn, $queryDelMsgs);				
					if(!$resultDelMsgs)
						$mysqlError.= mysqli_error($conn);				
				}
			}		
		}
		else
		{
			mysqli_query($conn, "ROLLBACK");
			echo 2;
			exit();
		}
	}
	else
	{
		mysqli_query($conn, "ROLLBACK");
		echo 2;
		exit();
	}
	
	if( empty($mysqlError) )
	{
		mysqli_query($conn, "COMMIT");
		echo 1;
		exit();
	}
	else
	{
		mysqli_query($conn, "ROLLBACK");
		echo 2;
		exit();
	}	
}
else
{
	echo 2;
	exit();
}
ob_end_flush();
?>
