<?php
	/* Show README */
	if (empty($_REQUEST) || isset($_GET["readme"])) {
		echo printReadMe();
		die;
	}

	$code = isset($_REQUEST["code"]) ? $_REQUEST["code"] : 200;
	$response = isset($_REQUEST["response"]) ? $_REQUEST["response"] : "";

	/* Allow all ajax requests */
	header("Access-Control-Allow-Origin: *");

	/* Set header: Http header status code */
	http_response_code($code);

	/* Check if it has valid JSON format */
	$response = @json_decode($response);

	/* Response if needed */
	if (!empty($response)) {
		/* Set header: Json response */
		header("Content-Type: application/json; charset=utf-8");

		echo json_encode($response);
	}

	function printReadMe() {
		/* README content below */
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <link rel="icon" href="https://luanxt.com/favicon.ico">
    <link rel="icon" href="https://luanxt.com/assets/images/favicon.svg" type="image/svg+xml">
    <link rel="apple-touch-icon" href="https://luanxt.com/assets/images/favicon.png">
	<title>Mock API - README</title>
</head>
<body>
	<section class="container">
		<div class="row">
			<div class="col">
				<!-- Parameters table -->
				<div class="responsive-table mt-4">
					<table class="table table-hover table-bordered table-striped-">
						<thead class="thead-light">
							<tr>
								<th>Parameters</th>
								<th>Description</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td><mark>code</mark></td>
								<td>
									<p>Response status code.</p>
									<p class="mb-0">Default: <code>200</code></p>
								</td>
							</tr>
							<tr>
								<td><mark>response</mark></td>
								<td>
									<p>URL encoded response JSON body data.</p>
									<p>If not present or invalid JSON format, then no data will be responded.</p>
									<p class="mb-0">Default: <code>No default data</code></p>
								</td>
							</tr>
						</tbody>
					</table>
				</div>

				<!-- Example table -->
				<?php $currentUrl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]; ?>

				<div class="responsive-table mt-4">
					<table class="table table-hover table-bordered table-striped-">
						<thead class="thead-light">
							<tr>
								<th>Examples</th>
								<th>Request URL</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td><mark>200 with no data</mark></td>
								<td><?php echo $currentUrl; ?>?<code>code</code>=200</td>
							</tr>
							<tr>
								<td><mark>200 with response data</mark></td>
								<td><?php echo $currentUrl; ?>?<code>code</code>=200&<code>response</code>=%7B%22status%22%3A%22success%22,%22data%22%3A%20%5B%5D%7D</td>
							</tr>
							<tr>
								<td><mark>503 with no data</mark></td>
								<td><?php echo $currentUrl; ?>?<code>code</code>=503</td>
							</tr>
							<tr>
								<td><mark>503 with response data</mark></td>
								<td><?php echo $currentUrl; ?>?<code>code</code>=503&<code>response</code>=%7B%22status%22%3A%22fail%22,%22data%22%3A%20%5B%5D%7D</td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</section>
</body>
</html>
<?php } ?>