<?php
include 'common.php';


function UseItem_RecoveryExp1($account_id, $character_uid, $login_key)
{
	$dbhandle = DB_Connect();
	//Select DB
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");
	
	if(!CheckLoginKey($dbhandle, $account_id, $login_key))
	{
		PrintLoginKeyError();
		mysqli_close($dbhandle);
		return;
	}
	
	$array_id = array();
	$total_exp = 0;
	$count = 0;
	
	
	$stmt = $dbhandle->prepare("SELECT * FROM drop_exp_log WHERE character_uid=? AND is_recovery=0");	
	$stmt->bind_param("i", $character_uid);
	$stmt->execute();
	$result = $stmt->get_result();
	if ($result)
	{
		$now = new DateTime('now', new DateTimeZone('UTC'));
		while($row = $result->fetch_assoc())
		{
			//$diff = strtotime($now->format('Y-m-d H:i:s')) - strtotime($row['log_time']);
			//if ($diff <= 172800)//60*60*48 = 48시간, 172800초
			{
				array_push($array_id, $row['id']);
				$total_exp += $row['reduced_exp'];
				$count++;
			}
		}
	}
	

	$response = "RECOVERY_EXP1_OK";
	$error = "";

	mysqli_close($dbhandle);
 
    $isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
    (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
    ($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
 
    echo json_encode(array('result'=>$response,'error'=>$error, 'total_drop_exp'=>$total_exp, 'array_drop_log_id'=>$array_id));
}


$json = json_decode(file_get_contents('php://input'));
$account_id = $json->account_id;
$character_uid = $json->character_uid;
$login_key = $json->login_key;




if(CheckValidatedServerIP())
{
	UseItem_RecoveryExp1($account_id, $character_uid, $login_key);

}
else
{
	$response = "INVALIDATED_SERVER";
	$error = "CheckValidatedServerIP ERROR";
	$isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
	(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
	($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain'); 
 
	echo json_encode(array(	'result'=>$response,'error'=>$error	));
}

?>