<?php
date_default_timezone_set('Asia/Singapore');

require_once('Constants.php') ;
require_once(PATH_LIB . 'AutoLoader.php') ;
require_once(PATH_LIB . 'Util.php') ;
require_once(PATH_LIB . 'Log.php') ;
require_once(PATH_MESSAGE . 'Message.php') ;
require_once(PATH_CODE . "database/MsSqlDb.php") ;
require_once(PATH_CODE . "database/MySqlDb.php") ;
require_once(PATH_CODE . "database/MySqliDb.php") ;
require_once('ConfigDb.php') ;

require '../vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

function myException($e)
{
	Log::write('[Service]' . $e->getMessage(),"") ;
	//die("Sorry, there is an error and the application can not continue and will be terminated.") ;
	die("") ;
}
set_exception_handler('myException');
register_shutdown_function('fatalErrorShutdownHandler');

function fatalErrorShutdownHandler()
{
	$last_error = error_get_last();
	if ($last_error['type'] > 0)
		Log::write('[Service]' . $last_error['message'] . " - " . $last_error['file'] . " on line " . $last_error['line']);
}
session_start() ;

//if (isset($_SERVER['QUERY_STRING']))
	//$query = $_SERVER['QUERY_STRING'] ;
//else 
	//$query = "";


if (!isset($_SESSION[SE_DB])) {
	$config = new ConfigDb(PATH_CODE . 'db.xml') ;
	$config->loadConfig() ;
	if ($config->getDbType() == DbType::MySql)
		$_SESSION[SE_DB] = new MySqlDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword(),$config->getPortNo()) ;
	else if ($config->getDbType() == DbType::MySqli)
		$_SESSION[SE_DB] = new MySqliDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword(),$config->getPortNo()) ;
	else
		$_SESSION[SE_DB] = new MsSqlDb($config->getHost(),$config->getDbName(),$config->getUserName(),$config->getPassword(),$config->getPortNo()) ;
	unset($config) ;
}

if (isset($_GET['c']))
	$class = $_GET['c'];
else
	$class = "" ;


try {	
	$page = "Login";
	$data = array() ;
	$called = false ;
	
	if ($class !=""){
		if (isset($_SESSION[SE_USERID]) && $_SESSION[SE_USERID] != "" ){
            if(isset($_GET['turn_off']) && $_GET['turn_off'] == 'yes' ){
                $page = $class;
            } else {
                $page = Util::revertLink($class,$_SESSION[SE_ID]) ;
            }
        } else {
            if(isset($_GET['turn_off']) && $_GET['turn_off'] == 'yes' ){
                $page = $class;
            }
        }
		// if (isset($_SESSION[SE_USERID]) && $_SESSION[SE_USERID] != "" ){
			// $page = Util::revertLink($class) ;
		// } 
		if ($_POST) {
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
		}	
		if ($_GET) {
			foreach ($_GET as $key => $value) {
				$data[$key] = $value;
			}
		}	
		if (class_exists($page)) {
			$called = call_user_func_array(array(new $page,"processRequest"),array($data)) ;
		} 
	}
} catch (Exception $e) {
	Log::write('[Service]' . $e->getMessage(),$page) ;
}
if (!$called) {
	header('Content-type: application/json');
	$arr = array(FIELD_STATUS =>Status::Invalid,FIELD_MESG =>"Invalid Request.",FIELD_DATA => "index.pzx");
	echo json_encode($arr) ;
}
?>