<?php

	// Perform Login
    $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_ARRAY_TOO_LARGE();
	
	// Check already authenticated
	PHPCUS_PARAM::CUS_REQUIRES_AUTHENTICATION();

	// Check access rights
	PHPCUS_PARAM::CUS_HAS_PERMISSION(PHPCUS_SYSTEM::AUTHORITY_VIEW_EDIT, 'category_address_region');
	
	if
	(
			PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
			(
					[
							'PARAM_CAT_REGION_ID',
							'PARAM_CAT_REGION_CAT_COUNTRY_ID', 
							'PARAM_CAT_REGION_CODE', 
							'PARAM_CAT_REGION_CODE_B', 
							'PARAM_CAT_REGION_CODE_C', 
							'PARAM_CAT_REGION_DESCRIPTION', 
							'PARAM_CAT_REGION_DESCRIPTION_B', 
							'PARAM_CAT_REGION_DESCRIPTION_C', 
							'PARAM_CAT_REGION_STATUS'
							// , 'PARAM_CAT_REGION_ID_FILE'
					], True
			)
	)
	{
		Try
		{
			// Read params
			// ------------------------------------------------------------------
			$PARAM_CAT_REGION_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_REGION_ID', true);
			$PARAM_CAT_REGION_CAT_COUNTRY_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_CAT_REGION_CAT_COUNTRY_ID', true);
			$PARAM_CAT_REGION_CODE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_REGION_CODE', true);
			$PARAM_CAT_REGION_CODE_B = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_REGION_CODE_B', true);
			$PARAM_CAT_REGION_CODE_C = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_REGION_CODE_C', true);
			$PARAM_CAT_REGION_DESCRIPTION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_REGION_DESCRIPTION', true);
			$PARAM_CAT_REGION_DESCRIPTION_B = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_REGION_DESCRIPTION_B', true);
			$PARAM_CAT_REGION_DESCRIPTION_C = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_CAT_REGION_DESCRIPTION_C', true);
			$PARAM_CAT_REGION_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_CAT_REGION_STATUS', true);
			
			//$PARAM_CAT_REGION_ID_FILE = PHPCUS_PARAM::CUS_READ_FILES('PARAM_CAT_REGION_ID_FILE');
			$PARAM_ARRAY_COUNT = count($PARAM_CAT_REGION_ID);
			
				
			// Validate if array has same size
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
			(
					[
							$PARAM_CAT_REGION_ID,
							$PARAM_CAT_REGION_CAT_COUNTRY_ID, 
							$PARAM_CAT_REGION_CODE, 
							$PARAM_CAT_REGION_CODE_B, 
							$PARAM_CAT_REGION_CODE_C, 
							$PARAM_CAT_REGION_DESCRIPTION, 
							$PARAM_CAT_REGION_DESCRIPTION_B, 
							$PARAM_CAT_REGION_DESCRIPTION_C, 
							$PARAM_CAT_REGION_STATUS
							// ,$PARAM_CAT_REGION_ID_FILE['name']
					]
			);
			
				
			// Prepare PDO Handler
			// ------------------------------------------------------------------
			$FULL_DATA_SET = [];
			$ARRAY_CTR = 0;
			
			$SAVE_MAP_ARRAY = [];
			$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)
				{
					$SAVE_MAP_ARRAY = PHPCUS_CATEGORY_ADDRESS_REGION::CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
					
					$SAVE_MAP_ARRAY['PARAM_CAT_REGION_ID'] = $PARAM_CAT_REGION_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_REGION_CAT_COUNTRY_ID'] = $PARAM_CAT_REGION_CAT_COUNTRY_ID[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_REGION_CODE'] = $PARAM_CAT_REGION_CODE[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_REGION_CODE_B'] = $PARAM_CAT_REGION_CODE_B[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_REGION_CODE_C'] = $PARAM_CAT_REGION_CODE_C[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_REGION_DESCRIPTION'] = $PARAM_CAT_REGION_DESCRIPTION[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_REGION_DESCRIPTION_B'] = $PARAM_CAT_REGION_DESCRIPTION_B[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_REGION_DESCRIPTION_C'] = $PARAM_CAT_REGION_DESCRIPTION_C[$ARRAY_CTR];
$SAVE_MAP_ARRAY['PARAM_CAT_REGION_STATUS'] = $PARAM_CAT_REGION_STATUS[$ARRAY_CTR];
							
					$DATA_SET = PHPCUS_CATEGORY_ADDRESS_REGION::CUS_SAVE_MA
					(
							$SAVE_MAP_ARRAY,
							true,
							$PDO_HANDLE
					);
			
					/*
					if
					(
						isset($PARAM_CAT_REGION_ID_FILE['name'][$ARRAY_CTR]) && 
						empty($DATA_SET) == false && 
						isset($DATA_SET[0]['INSERTED_ID'])
					)
					{		
						$FILE_PATH = PHPRC_UPLOAD::REF_SUGGEST_PATH
						(
							PHPSET::$ROOT . '/upload/priv/Category/address_region/', 
							$DATA_SET[0]['INSERTED_ID'],
							$PARAM_CAT_REGION_ID_FILE['name'][$ARRAY_CTR]
						);
						PHPRC_UPLOAD::REF_UPLOAD_TRANSACTION_MOVEFILE
						(
							$PARAM_CAT_REGION_ID_FILE['tmp_name'][$ARRAY_CTR],
							$FILE_PATH,
							true
						);
					}
					*/
					
					$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);
				//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();
						
			
			
			// Return JSON response
			// ------------------------------------------------------------------
			PHPCUS_PARAM::CUS_JSON_SUCCESS($FULL_DATA_SET);
			
		}
		Catch(Exception $ERR)
		{
			PHPCUS_PARAM::CUS_JSON_ERROR($ERR);
		}
	}
	else
	{
		PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
	}

?>