<?php

	// Check nonce
	PHPCUS_PARAM::CUS_REQUIRES_NONCE();
	
	// Dismiss Session
	PHPVC_SESSION::VAR_STOP();
	
	// Check Array Input
	PHPCUS_PARAM::CUS_DOES_NOT_SUPPORT_ARRAY();
	
	// Check already authenticated
	PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();

	// Check access rights
	// PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_ONLY, 'account_audit_trail');
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_TRAIL_ID',
							'PARAM_TRAIL_REQUEST_IP_ADDRESS',
							'PARAM_TRAIL_METHOD_ID',
							'PARAM_TRAIL_ACCOUNT_ID',
							'PARAM_ACCOUNT_NAME',
							'PARAM_TRAIL_SERVER_ID',
							
							'PARAM_TRAIL_MODULE_ID',
							'PARAM_MODULE_KEYWORD',
							'PARAM_TRAIL_TARGET_ID',
							'PARAM_TRAIL_DESCRIPTION',
							'PARAM_TRAIL_PARAMETERS',
							
							'PARAM_TRAIL_TIMESTAMP_FROM',
							'PARAM_TRAIL_TIMESTAMP_TO',
							'PARAM_TRAIL_UNIQUE_SESSION_ID',
							
							'PARAM_ORDER_FIELD',
							'PARAM_ORDER_TYPE',
							'PARAM_SKIP',
							'PARAM_LIMIT'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_TRAIL_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_TRAIL_ID');
			$PARAM_TRAIL_REQUEST_IP_ADDRESS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_TRAIL_REQUEST_IP_ADDRESS');
			$PARAM_TRAIL_METHOD_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_TRAIL_METHOD_ID');
			$PARAM_TRAIL_ACCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_TRAIL_ACCOUNT_ID');
			$PARAM_ACCOUNT_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACCOUNT_NAME');
			$PARAM_TRAIL_SERVER_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_TRAIL_SERVER_ID');
			
			$PARAM_TRAIL_MODULE_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_TRAIL_MODULE_ID');
			$PARAM_MODULE_KEYWORD = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_MODULE_KEYWORD');
			$PARAM_TRAIL_TARGET_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_TRAIL_TARGET_ID');
			$PARAM_TRAIL_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_TRAIL_DESCRIPTION');
			$PARAM_TRAIL_PARAMETERS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_TRAIL_PARAMETERS');
			
			$PARAM_TRAIL_TIMESTAMP_FROM = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_TRAIL_TIMESTAMP_FROM');
			$PARAM_TRAIL_TIMESTAMP_TO = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_TRAIL_TIMESTAMP_TO');
			$PARAM_TRAIL_UNIQUE_SESSION_ID = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_TRAIL_UNIQUE_SESSION_ID');
				
			
			$PARAM_ORDER_FIELD = PHPCUS_PARAM::CUS_READ_SORT_FIELD
			(
					'PARAM_ORDER_FIELD', 
					[
		              'ACC_TRAIL_ID',
					  'ACC_TRAIL_REQUEST_IP_ADDRESS',
					  'ACC_TRAIL_SYS_METHOD_ID',
					  'ACC_TRAIL_ACC_ACCOUNT_ID',
					  'ACC_TRAIL_ACC_SERVER_ID',
					    
					  'ACC_TRAIL_SYS_MODULE_ID',
                      'ACC_TRAIL_TARGET_ID',
					  'ACC_TRAIL_DESCRIPTION',
					  'ACC_TRAIL_PARAMETERS',
					  
					  'ACC_TRAIL_TIMESTAMP',
					  'ACC_TRAIL_UNIQUE_SESSION_ID',
					    
					  'ACC_ACCOUNT_ID',
					  'ACC_ACCOUNT_ACC_LEVEL_ID',
					  'ACC_ACCOUNT_FIRST_NAME',
					  'ACC_ACCOUNT_MIDDLE_NAME',
					  'ACC_ACCOUNT_LAST_NAME',
					  'ACC_ACCOUNT_SYS_GENDER_ID',
					  'ACC_ACCOUNT_STATUS',
					  'ACC_ACCOUNT_SYSTEM_GENERATED',
					    
					  'SYS_MODULE_ID',
					  'SYS_MODULE_CATEGORY',
					  'SYS_MODULE_KEYWORD',
					  'SYS_MODULE_NAME',
					  'SYS_MODULE_DESCRIPTION',
					  'SYS_MODULE_STATUS',
					  'SYS_MODULE_SHOW_ACCESS_RIGHTS'
					]
			);
			
			$PARAM_ORDER_TYPE = PHPCUS_PARAM::CUS_READ_SORT_TYPE('PARAM_ORDER_TYPE');
			$PARAM_SKIP	= PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_SKIP');
			// ------------------------------------------------------------------
			
			
			// Execute Stored Procedure
			$DATA_SET = PHPCUS_Account_Audit_Trail::CUS_AUDIT_TRAIL_GET
						(
								$PARAM_TRAIL_ID,
								$PARAM_TRAIL_REQUEST_IP_ADDRESS,
								$PARAM_TRAIL_METHOD_ID,
								$PARAM_TRAIL_ACCOUNT_ID,
								$PARAM_ACCOUNT_NAME,
								$PARAM_TRAIL_SERVER_ID,
								
								$PARAM_TRAIL_MODULE_ID,
								$PARAM_MODULE_KEYWORD,
								$PARAM_TRAIL_TARGET_ID,
								$PARAM_TRAIL_DESCRIPTION,
								$PARAM_TRAIL_PARAMETERS,
								
								$PARAM_TRAIL_TIMESTAMP_FROM,
								$PARAM_TRAIL_TIMESTAMP_TO,
								$PARAM_TRAIL_UNIQUE_SESSION_ID,
								
								$PARAM_ORDER_FIELD, 
								$PARAM_ORDER_TYPE, 
								$PARAM_SKIP
						);
			
			// Return JSON response	
			PHPCUS_PARAM::CUS_JSON_SUCCESS($DATA_SET);	
		}
		Catch(Exception $ERR)
		{
			PHPCUS_PARAM::CUS_JSON_ERROR($ERR);
		}
	}
	else
	{
		PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
	}

?>