<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST");

require_once "config.php";

$json = file_get_contents("php://input");
$data = json_decode($json);

$role = $data->role;

$conn = connect_db();

$res = array();

if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
} else {
	$sql = "
		SELECT *
		FROM `module`
		WHERE active = 1
		AND east_west = 'mix'
		AND access LIKE '%" . $role . "%'
		ORDER BY order_sequence
	";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$temp = array(
				"order_sequence" => intval($row["order_sequence"]),
				"name" => $row['name'],
				"nameZh" => $row['nameZh'],
				"link" => $row['link'],
				"access" => $row['access'],
			);

			array_push($res, $temp);
		}
	} else {
		$res = null;
	}
}

$conn->close();
echo json_encode($res);
