<?php
include 'common.php';



function IsExistAndStateApply_GuildApplication($dbhandle, $character_uid, $guild_uid)
{
	$stmt = $dbhandle->prepare("SELECT * FROM guild_apply WHERE character_uid=? AND guild_uid=?");
	$stmt->bind_param("ii", $character_uid, $guild_uid);
	$stmt->execute();
	$result = $stmt->get_result();

	if ($result)
	{
		if($row = $result->fetch_assoc())
		{
			if ($row["state"] === "APPLY")
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	else
	{
		$return = "DB_ERROR";
		$error = mysqli_error($dbhandle);
		$response = array('result'=>$return,'error'=>$error);

		EchoResponse($response);

		mysqli_close($dbhandle);
		exit;
	}
}
function ChangeApplicationState_Reject($dbhandle, $character_uid, $guild_uid)
{
	$stmt = $dbhandle->prepare("UPDATE guild_apply SET state='REJECT' WHERE character_uid=? AND guild_uid=?");
	$stmt->bind_param("ii", $character_uid, $guild_uid);
	$result = $stmt->execute();
	$stmt->close();
			
	if($result)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function GuildApplyReject($account_id, $character_uid, $guild_uid, $login_key)
{
	//Connect DB
	$dbhandle = DB_Connect();
		
	$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;
	}

	$result_existAndApply = IsExistAndStateApply_GuildApplication($dbhandle, $character_uid, $guild_uid);
	if ($result_existAndApply)
	{
		$result_changeState = ChangeApplicationState_Reject($dbhandle, $character_uid, $guild_uid);
		if ($result_changeState)
		{
		
			$return = "GUILD_APPLY_REJECT_OK";
			$error = "";
			$response = array('result'=>$return,'error'=>$error);

			EchoResponse($response);
		}
		else
		{
			$return = "CHANGE_STATE_FAIL";
			$error = "";
			$response = array('result'=>$return,'error'=>$error);

			EchoResponse($response);
		}
	}
	else
	{
		$return = "NOT_EXIST_APPLICATION";
		$error = "";
		$response = array('result'=>$return,'error'=>$error);

		EchoResponse($response);

	}

	mysqli_close($dbhandle);
}

function EchoResponse($response)
{
	$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);
}


$account_id = $_GET['account_id'];
$character_uid = $_GET['character_uid'];
$guild_uid = $_GET['guild_uid'];
$login_key = $_GET['login_key'];

if(CheckValidatedServerIP())
{
	GuildApplyReject($account_id, $character_uid, $guild_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	));
}


?>