<?php

	// Perform Login
	$CURRENT_MODULE_KEY = 'account_apk';
    $SHORTCODE_STATUS = PHPCUS_SHORTCODE::CUS_QUICK_LOGIN();
	
	// Check nonce
	if($SHORTCODE_STATUS == false)
    {
		PHPCUS_PARAM::CUS_REQUIRES_NONCE();
	}
	
	// Dismiss Session
	PHPVC_SESSION::VAR_STOP();
	
	// Check array size
	PHPCUS_PARAM::CUS_DOES_NOT_SUPPORT_ARRAY();

	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_ACC_APK_CODE',
							'PARAM_ACC_APK_SERIAL'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
		    $PARAM_ACC_APK_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_APK_CODE', false);
		    $PARAM_ACC_APK_SERIAL = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_APK_SERIAL', false);
			
			// Abort if already processed
			// ------------------------------------------------------------------
			$PARAM_REQUEST_UNIQUE_VALUE = null;
			if(PHPRC_HTTP_PARAM::REF_ISSET('PARAM_REQUEST_UNIQUE_VALUE'))
			{
				$PARAM_REQUEST_UNIQUE_VALUE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_REQUEST_UNIQUE_VALUE', false);
			}
			
			if(PHPVC_STR::VAR_IS_BLANK($PARAM_REQUEST_UNIQUE_VALUE) == false)
			{
				$ARR_INDEX = array_search($CURRENT_MODULE_KEY, PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY); 
				if
				(
					$ARR_INDEX !== false &&
					PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE[$ARR_INDEX] == $PARAM_REQUEST_UNIQUE_VALUE
				)
				{
					PHPCUS_PARAM::CUS_JSON_ERROR('Given Request is already processed.');
				}
			}
		
		
			// Prepare PDO Handler
			// ------------------------------------------------------------------		
			$SAVE_MAP_ARRAY = [];
			$PDO_HANDLE = PHPRC_PDO_MYSQL::REF_CONNECT();
			PHPRC_PDO_MYSQL::REF_TRANSACTION_BEGIN($PDO_HANDLE);
			
		
			// Execute Stored Procedure
			// ------------------------------------------------------------------
			Try
			{
					$SAVE_MAP_ARRAY = PHPCUS_ACCOUNT_APK::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					$SAVE_MAP_ARRAY['PARAM_ACC_APK_CODE'] = $PARAM_ACC_APK_CODE;
					$SAVE_MAP_ARRAY['PARAM_ACC_APK_SERIAL'] = $PARAM_ACC_APK_SERIAL;
					
					$DATA_SET = PHPCUS_ACCOUNT_APK::CUS_VALIDATE_MA
					(
							$SAVE_MAP_ARRAY,
							true,
							$PDO_HANDLE
					);
			}
			Catch(Exception $PDO_ERR)
			{
				PHPRC_PDO_MYSQL::REF_TRANSACTION_ROLLBACK($PDO_HANDLE);
				//PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_ROLLBACK();
				PHPCUS_PARAM::CUS_JSON_ERROR($PDO_ERR);
			}
			PHPRC_PDO_MYSQL::REF_TRANSACTION_COMMIT($PDO_HANDLE);
			//PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_COMMIT();
					
					
			// Update processed request key
			// ------------------------------------------------------------------
			if(PHPVC_STR::VAR_IS_BLANK($PARAM_REQUEST_UNIQUE_VALUE) == false)
			{
				$ARR_INDEX = array_search($CURRENT_MODULE_KEY, PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY);
				if($ARR_INDEX !== false)
				{
					PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE[$ARR_INDEX] = $PARAM_REQUEST_UNIQUE_VALUE;
				}
				else
				{
					array_push(PHPCUS_Account_Login_Session::$SESSION_REQUEST_KEY, $CURRENT_MODULE_KEY);
					array_push(PHPCUS_Account_Login_Session::$SESSION_REQUEST_LAST_VALUE, $PARAM_REQUEST_UNIQUE_VALUE);
				}
			}			
			
			
			// 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');
	}

?>