<?php
include 'common.php';
include 'server_exchange_common.php';

function GetExchangeItemDetail($id)
{
	$dbhandle = DB_Connect();
	//Select DB
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");

	$return = "GET_EXCHANGE_ITEM_DETAIL_FAILED";
	$error = "";

	if (CheckExchangeIsMaintenance($dbhandle))
	{
		$return = "EXCHANGE_UNDER_MAINTENANCE";
		$error = "";
		$response = array('result'=>$return,'error'=>$error,'list'=>"");
	}
	else
	{
		$stmt = $dbhandle->prepare("SELECT * FROM exchange_item WHERE is_cancel=0 AND id=?");
		$stmt->bind_param("i", $id);
		$stmt->execute();
		$result = $stmt->get_result();
	
		if ($result)
		{
			if($row = $result->fetch_assoc())
			{
				$data = $row["id"].",".$row["item_uid"].",".$row["count"].",".$row["price"].",".$row["enchant"].",".$row["durability"]
				.",".$row["optType1"].",".$row["optValue1"].",".$row["optType2"].",".$row["optValue2"].",".$row["optType3"].",".$row["optValue3"]
				.",".$row["optType4"].",".$row["optValue4"];
	
				$return = "GET_EXCHANGE_ITEM_DETAIL_OK";
				$error = "";
				$response = array('result'=>$return,'error'=>$error,'list'=>$data);
	
				mysqli_stmt_free_result($stmt);
			}
			else
			{
				$return = "NOT_EXIST_EXCHANGE_ITEM";
				$error = "";
				$response = array('result'=>$return,'error'=>$error,'list'=>"");
			}
		}
		else
		{
			$return = "DB_ERROR";
			$error = mysqli_error($dbhandle);
			$response = array('result'=>$return,'error'=>$error,'list'=>"");
	
		}
	}

	$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($response);
		
	mysqli_close($dbhandle);
}

$id = $_GET['id'];

if(CheckValidatedServerIP())
{
	GetExchangeItemDetail($id);
}
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	));
}

?>