<?php
include_once '../Connect/cms-config.php' ;
include_once 'function-api.php' ;

// check if post exsists
$array = array() ;
if ( appArrayCheck($_POST) ){
	foreach ( $_POST as $key => $value ){
		if ( appArrayCheck($value) ){ foreach ( $value as $key2 => $value2 ){ $array[$key][$key2] = appEscapeString($value2) ; }
		}else{ $array[$key] = appEscapeString($value) ; }
	}
}

$status  = '500' ;
$message = 'Failed to connect.' ;
$result  = 'failed' ;
$content = [] ;

$type    = $array['type'] ;
$action  = $array['action'] ;

// pagination selection
$current = $array['current'] ;
$limit = $array['limit'] ;
$limit = ( $limit != '' ? $limit : '30' ) ;
$limit_page = '' ;
// $current = 1 ;
// $limit = 1000 ;
// $limit_page = '' ;

// page pagination
if ( isset($current) && !empty($current) ) { $pagination = $current ; }  else { $pagination = '' ; }
if ( $pagination != '' ){
	$start_from = ($pagination - 1) * $limit ;
	$limit_page = 'LIMIT '.$start_from.', '.$limit.'';
}

switch ( $type ){

	case 'version' :
		switch ( $action ){
			case 'version2'     				: include 'Version/version2.php' ; break ;
		}
	break ;

	// for website view
	case 'website' :

		switch ( $action ){
			case 'about-us' 					: include 'Website/aboutUsContent.php' ; break ;
			case 'contact-us' 					: include 'Website/contactUsContent.php' ; break ;
			case 'others' 						: include 'Website/othersContent.php' ; break ;
			case 'video' 						: include 'Website/videoContent.php' ; break ;
			case 'videodt' 						: include 'Website/videoContentDetails.php' ; break ;
			case 'news' 						: include 'Website/newsContent.php' ; break ;
			case 'newsdt' 						: include 'Website/newsContentDetails.php' ; break ;
		}

	break ;

	// for user login, password
	case 'user' :

		switch ( $action ){
			case 'push' 						: include 'User/push.php' ; break ;
			case 'token' 						: include 'User/token.php' ; break ;
			case 'login' 						: include 'User/login.php' ; break ;
			case 'forgot-password' 				: include 'User/forgot_password.php' ; break ;
		}

	break ;

	// for category, product
	case 'category' :
	case 'product' :

		switch ( $action ){
			case 'category-list' 				: include 'Product/category-list.php' ; break ;
			case 'productdt' 					: include 'Product/productdt.php' ; break ;
			case 'productRemark' 				: include 'Product/product-remark.php' ; break ;
		}

	break ;

	case 'namecard' :

		switch ( $action ){
			case 'new' 							: include 'Namecard/new.php' ; break ;
			case 'new2' 						: include 'Namecard/new2.php' ; break ;
			case 'list' 						: include 'Namecard/list.php' ; break ;
			case 'customer-list'				: include 'Namecard/customer-list.php' ; break ;
		}

	break ;

	case 'customer' :

		switch ( $action ){
			case 'new' 							: include 'Customer/new.php' ; break ;
			case 'new2'							: include 'Customer/new2.php' ; break ;
			case 'list' 						: include 'Customer/list.php' ; break ;
		}

	break ;


	case 'order' :

		switch ( $action ){
			case 'quotation' 					: include 'Order/quotation.php' ; break ;
			case 'quotation-new' 				: include 'Order/quotation_new.php' ; break ;
			case 'quotation-export2' 			: include 'Order/quotation_export2.php' ; break ;
			case 'invoice' 						: include 'Order/invoice.php' ; break ;
			case 'invoice-export2' 				: include 'Order/invoice_export2.php' ; break ;
			case 'proforma-invoice' 			: include 'Order/proforma_invoice.php' ; break ;
			case 'proforma-invoice-export2' 	: include 'Order/proforma_invoice_export2.php' ; break ;
		}

	break ;
	case 'report' :

		switch ( $action ){
			case 'salesReport' 					: include 'Report/MonthlySales.php' ; break ;
			case 'annualProductList' 			: include 'Report/annualProductList.php' ; break ;
		}

	break ;
	case 'webview' :

		switch ( $action ){
			case 'aboutUsContent' 				: include 'Webview/aboutUsContent.php' ; break ;
			case 'newsContent' 					: include 'Webview/newsContent.php' ; break ;
			case 'videoContent' 				: include 'Webview/videoContent.php' ; break ;
			case 'webviewContent' 				: include 'Webview/webviewContent.php' ; break ;
			case 'category' 					: include 'Webview/category.php' ; break ;
		}

	break ;

}

$data['status']  = $status ;
$data['message'] = $message ;
$data['result']  = $result ;
$data['content'] = $content ;


// $mysqli->query( "INSERT INTO system_log
// 				 (log_post, log_date) VALUES
// 				 ('".appJsonEncodeDecode('encode', $_POST)."', '".$today_date."')" ) ;

$mysqli->query( "INSERT INTO system_log
				 (log_post, log_file, log_result, log_date) VALUES
				 ('".appJsonEncodeDecode('encode', $_POST)."', '".appJsonEncodeDecode('encode', $_FILES)."', '".appJsonEncodeDecode('encode', $data)."', '".$today_date."')" ) ;

header('Content-Type: application/json') ;
echo json_encode($data) ;

?>
