<?php

	// Check nonce
	PHPCUS_PARAM::CUS_REQUIRES_NONCE();
	
	// Check array size
	PHPCUS_PARAM::CUS_ARRAY_TOO_LARGE();

	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_ACC_ACCOUNT_ID',
							'PARAM_ACC_ACCOUNT_ACC_LEVEL_ID',
							'PARAM_ACC_ACCOUNT_ACC_GROUP_ID',
							
							'PARAM_ACC_ACCOUNT_USERNAME',
							'PARAM_ACC_ACCOUNT_PASSWORD',
							'PARAM_ACC_ACCOUNT_FIRST_NAME',
							'PARAM_ACC_ACCOUNT_MIDDLE_NAME',
							'PARAM_ACC_ACCOUNT_LAST_NAME',
	
							'PARAM_ACC_ACCOUNT_SYS_GENDER_ID',
							'PARAM_ACC_ACCOUNT_POSITION',

							'PARAM_ACC_ACCOUNT_STATUS'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_ACCOUNT_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_ACC_ACCOUNT_ID', true);
			$PARAM_ACCOUNT_LEVEL_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_ACC_ACCOUNT_ACC_LEVEL_ID', true);
			$PARAM_ACCOUNT_GROUP_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_ACC_ACCOUNT_ACC_GROUP_ID', true);
			
			$PARAM_ACCOUNT_USERNAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_ACCOUNT_USERNAME', true);
			$PARAM_ACCOUNT_PASSWORD = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_ACCOUNT_PASSWORD', true);
			$PARAM_ACCOUNT_FIRST_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_ACCOUNT_FIRST_NAME', true);
			$PARAM_ACCOUNT_MIDDLE_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_ACCOUNT_MIDDLE_NAME', true);
			$PARAM_ACCOUNT_LAST_NAME = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_ACCOUNT_LAST_NAME', true);
			
			$PARAM_ACCOUNT_GENDER_ID = PHPCUS_PARAM::CUS_READ_TINYINT('PARAM_ACC_ACCOUNT_SYS_GENDER_ID', true);
			$PARAM_ACCOUNT_POSITION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ACC_ACCOUNT_POSITION', true);
			$PARAM_ACCOUNT_STATUS = PHPCUS_PARAM::CUS_READ_TINYINT('PARAM_ACC_ACCOUNT_STATUS', true);

			$PARAM_ARRAY_COUNT = count($PARAM_ACCOUNT_ID);
			
			
			// Validate if array has same size
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			(
					[
							$PARAM_ACCOUNT_ID,
							$PARAM_ACCOUNT_LEVEL_ID,
							$PARAM_ACCOUNT_GROUP_ID,
							
							$PARAM_ACCOUNT_USERNAME,
							$PARAM_ACCOUNT_PASSWORD,
							$PARAM_ACCOUNT_FIRST_NAME,
							$PARAM_ACCOUNT_MIDDLE_NAME,
							$PARAM_ACCOUNT_LAST_NAME,
							
							$PARAM_ACCOUNT_GENDER_ID,
							$PARAM_ACCOUNT_POSITION,
							$PARAM_ACCOUNT_STATUS
					]
			);
			
			
			// Prepare PDO Handler
			// ------------------------------------------------------------------
			$FULL_DATA_SET = [];
			$DATA_SET = null;
			$ARRAY_CTR = 0;
			
			$PDO_HANDLE = PHPRC_PDO_MYSQL::REF_CONNECT();
			PHPRC_PDO_MYSQL::REF_TRANSACTION_BEGIN($PDO_HANDLE);
		
			
			// Execute Stored Procedure
			// ------------------------------------------------------------------
			Try
			{
				while($ARRAY_CTR < $PARAM_ARRAY_COUNT)
				{
				    /*
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACCOUNT_ID', $PARAM_ACCOUNT_ID[$ARRAY_CTR]);
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_ACC_LEVEL_ID', $PARAM_ACCOUNT_LEVEL_ID[$ARRAY_CTR]);
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_ACC_GROUP_ID', $PARAM_ACCOUNT_GROUP_ID[$ARRAY_CTR]);
				    
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_USERNAME', $PARAM_ACCOUNT_USERNAME[$ARRAY_CTR]);
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_PASSWORD', $PARAM_ACCOUNT_PASSWORD[$ARRAY_CTR]);
				    
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_FIRST_NAME', $PARAM_ACCOUNT_FIRST_NAME[$ARRAY_CTR]);
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_MIDDLE_NAME', $PARAM_ACCOUNT_MIDDLE_NAME[$ARRAY_CTR]);
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_LAST_NAME', $PARAM_ACCOUNT_LAST_NAME[$ARRAY_CTR]);
				    
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_POSITION', $PARAM_ACCOUNT_GENDER_ID[$ARRAY_CTR]);
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_SYS_GENDER_ID', $PARAM_ACCOUNT_POSITION[$ARRAY_CTR]);
				    PHPVC_LOG::VAR_COMMENTS('PARAM_ACC_ACCOUNT_STATUS', $PARAM_ACCOUNT_STATUS[$ARRAY_CTR]);
				    */
				    
					// Save
					$SAVE_MAP_ARRAY = PHPCUS_Account_Member::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_ID'] = $PARAM_ACCOUNT_ID[$ARRAY_CTR];
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_ACC_LEVEL_ID'] = $PARAM_ACCOUNT_LEVEL_ID[$ARRAY_CTR];
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_ACC_GROUP_ID'] = $PARAM_ACCOUNT_GROUP_ID[$ARRAY_CTR];
					
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_USERNAME'] = $PARAM_ACCOUNT_USERNAME[$ARRAY_CTR];
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_PASSWORD'] = $PARAM_ACCOUNT_PASSWORD[$ARRAY_CTR];
					
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_FIRST_NAME'] = $PARAM_ACCOUNT_FIRST_NAME[$ARRAY_CTR];
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_MIDDLE_NAME'] = $PARAM_ACCOUNT_MIDDLE_NAME[$ARRAY_CTR];
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_LAST_NAME'] = $PARAM_ACCOUNT_LAST_NAME[$ARRAY_CTR];
					
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_SYS_GENDER_ID'] = $PARAM_ACCOUNT_GENDER_ID[$ARRAY_CTR];
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_POSITION'] = $PARAM_ACCOUNT_POSITION[$ARRAY_CTR];
					$SAVE_MAP_ARRAY['PARAM_ACC_ACCOUNT_STATUS'] = $PARAM_ACCOUNT_STATUS[$ARRAY_CTR];
					
					$DATA_SET = PHPCUS_Account_Member::CUS_SAVE_MA($SAVE_MAP_ARRAY, true, $PDO_HANDLE);			
					$FULL_DATA_SET = PHPRC_PDO_MYSQL_ARRAY::REF_MERGE($FULL_DATA_SET, $DATA_SET);
					$ARRAY_CTR ++;
				}	
			}
			Catch(Exception $PDO_ERR)
			{
				PHPRC_PDO_MYSQL::REF_TRANSACTION_ROLLBACK($PDO_HANDLE);
				PHPCUS_PARAM::CUS_JSON_ERROR($PDO_ERR);
			}
			PHPRC_PDO_MYSQL::REF_TRANSACTION_COMMIT($PDO_HANDLE);
			
			
			// Return JSON response
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_JSON_SUCCESS($FULL_DATA_SET);
		}
		Catch(Exception $ERR)
		{
			PHPVC_LOG::VAR_REPORT_ERROR($ERR, 0);
			PHPCUS_PARAM::CUS_JSON_ERROR($ERR->getMessage());
		}
	}
	else
	{
		PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
	}


?>