<?php
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');

// error_reporting(E_ALL);
// ini_set("display_errors", 1);

$mUser              = $_GET['user'];
$mMenu				= $_GET['menu'];

$mPassword			= $_GET['password'];
$mFunction  		= $_GET['function'];
$mConnect   		= $_GET['connection'];
$mHost              = $_GET['host'];
$mDbUser            = $_GET['dbuser'];
$mDbPassword        = $_GET['dbpassword'];
$mDbName			= $_GET['database'];
$mTableName 		= $_GET['table'];
$mOffset    		= $_GET['offset'];
$mRecords   		= $_GET['records'];
$mSort      		= $_GET['sort'];
$mFilter    		= $_GET['filter'];

if ($mConnect == "")
	$mConnect = "mysql";
if ($mHost == "")
	$mHost = "localhost";
if ($mDbName == "")
	$mDbName = "vims";
if ($mDbUser == "")
	$mDbUser = "root";
if ($mDbPassword == "")
	$mDbPassword = "root";

$mTableStr = "";

	
$mConnection=mysqli_connect($mHost,$mDbUser,$mDbPassword,$mDbName) or
	die("FAIL-Connecting To Database");
$mTableStr = $mDbName.".".$mTableName;

// User
// Company
// Menu Header
// Menu Body

// Menu Detail
if (($mFunction != null) && ($mFunction == "menudetail"))
{
// Get Menu Header by Menu name
	If ($mMenu != "")				
		$mDataQuery = "SELECT id AS id FROM menu_header WHERE menu_name = '".$mMenu."' LIMIT 1";
	else
		$mDataQuery = "SELECT id AS id FROM menu_header LIMIT 1";
			
	$mReturnData = array();
	$i=0;
	$mId = "";
	$mData = mysqli_query($mConnection,$mDataQuery);
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mId = ($mRow['id']);
		$i++;
	}

// If Menu Exists Get Matching Body Records (categories) 
	if ($i > 0)
	{
		$i = 0;
		$mDataQuery = "SELECT id AS id, category_name AS category_name FROM menu_body WHERE menu_header_id=".$mId;				
		$mData = mysqli_query($mConnection,$mDataQuery) or
			die("FAIL-MySQL Error :".mysqli_error($mConnection));
		$jsonResponse = array();
		while($mRow = mysqli_fetch_assoc($mData)) 
		{
			$rowArray = array();
			$mBodyId = ($mRow['id']);
			$mCategory = ($mRow['category_name']);
			$mReturnData = array();
			$rowArray['category'] = $mCategory;
			$rowArray['list'] = array();
// For Each Body Get the Matching Details (Menu Items) into an array
			$mDetailQuery = "SELECT menu_detail.description AS name, menu_detail.main_Sequence AS main_sequence, menu_detail.sub_sequence AS sub_sequence, menu_detail.menu_code AS menu_code, menu_detail.action AS url FROM menu_detail WHERE menu_body_id=".$mBodyId;
			$mDetailData = mysqli_query($mConnection,$mDetailQuery);
			while($mRow = mysqli_fetch_assoc($mDetailData))
			{
				$mReturnData[] = $mRow;
//				$rowArray[$mCategory][] = $mRow;
				$rowArray['list'][] = $mRow;
			}
// Combine the Body Items & Detail Array into a JSON Array Element
			$i++;
			$mJsonData[$i] = array($mCategory,$mReturnData);
			unset($mReturnData);
			
			array_push($jsonResponse,$rowArray);
			unset($rowArray);
		}
		$mJson = json_encode($mJsonData);
		$mJson = str_replace(",[",":[",$mJson);
		$mJson = str_replace("]:[",",",$mJson);
		$mJson = str_replace("[[","{",$mJson);
		$mJson = str_replace("]]]","]}",$mJson);
	}
					
	mysqli_close($mConnection);

// Encode the JSON Array into a JSON Object & echo this to the Calling Program
//	echo json_encode($jsonResponse, JSON_FORCE_OBJECT);
	echo json_encode($jsonResponse);
//	echo($mJson);
}

?>