<?php

// Check nonce
PHPCUS_PARAM::CUS_REQUIRES_NONCE();

// 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, 'addon_queuing');

if
(
    PHPRC_HTTP_PARAM::REF_ISSET_IN_ARRAY
    (
        [
            'PARAM_ADD_QUEUING_ID',
            'PARAM_ADD_QUEUING_NUMBER',
            'PARAM_ADD_QUEUING_EXTRA_VALUE',
            'PARAM_ADD_QUEUING_STATION',
            'PARAM_ADD_QUEUING_REMARKS',
            'PARAM_ADD_QUEUING_DATE_REQUEST',
            'PARAM_ADD_QUEUING_DATE_PROCESSED',
            'PARAM_ADD_QUEUING_DATE_LAST_CALLED',
            'PARAM_ADD_QUEUING_DATE_LAST_DISPLAYED',
            'PARAM_ADD_QUEUING_STATUS'
        ], True
        )
    )
{
    Try
    {
        // Read params
        // ------------------------------------------------------------------
        $PARAM_ADD_QUEUING_ID = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_ADD_QUEUING_ID', true);
        $PARAM_ADD_QUEUING_NUMBER = PHPCUS_PARAM::CUS_READ_BIGINT('PARAM_ADD_QUEUING_NUMBER', true);
        $PARAM_ADD_QUEUING_EXTRA_VALUE = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ADD_QUEUING_EXTRA_VALUE', true);
        $PARAM_ADD_QUEUING_STATION = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ADD_QUEUING_STATION', true);
        $PARAM_ADD_QUEUING_REMARKS = PHPCUS_PARAM::CUS_READ_NVARCHAR('PARAM_ADD_QUEUING_REMARKS', true);
        $PARAM_ADD_QUEUING_DATE_REQUEST = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_ADD_QUEUING_DATE_REQUEST', true);
        $PARAM_ADD_QUEUING_DATE_PROCESSED = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_ADD_QUEUING_DATE_PROCESSED', true);
        $PARAM_ADD_QUEUING_DATE_LAST_CALLED = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_ADD_QUEUING_DATE_LAST_CALLED', true);
        $PARAM_ADD_QUEUING_DATE_LAST_DISPLAYED = PHPCUS_PARAM::CUS_READ_DATETIME('PARAM_ADD_QUEUING_DATE_LAST_DISPLAYED', true);
        $PARAM_ADD_QUEUING_STATUS = PHPCUS_PARAM::CUS_READ_INT('PARAM_ADD_QUEUING_STATUS', true);
        
        $PARAM_ARRAY_COUNT = count($PARAM_ADD_QUEUING_ID);
        
        
        // Validate if array has same size
        // ------------------------------------------------------------------
        PHPCUS_PARAM::CUS_ARRAY_SIZE_MISMATCH
        (
            [
                $PARAM_ADD_QUEUING_ID,
                $PARAM_ADD_QUEUING_NUMBER,
                $PARAM_ADD_QUEUING_EXTRA_VALUE,
                $PARAM_ADD_QUEUING_STATION,
                $PARAM_ADD_QUEUING_REMARKS,
                $PARAM_ADD_QUEUING_DATE_REQUEST,
                $PARAM_ADD_QUEUING_DATE_PROCESSED,
                $PARAM_ADD_QUEUING_DATE_LAST_CALLED,
                $PARAM_ADD_QUEUING_DATE_LAST_DISPLAYED,
                $PARAM_ADD_QUEUING_STATUS
            ]
            );
        
        
        // Prepare PDO Handler
        // ------------------------------------------------------------------
        $FULL_DATA_SET = [];
        $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)
            {
                $DATA_SET = PHPCUS_ADDON_QUEUING::CUS_SAVE
                (
                    $PARAM_ADD_QUEUING_ID[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_NUMBER[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_EXTRA_VALUE[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_STATION[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_REMARKS[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_DATE_REQUEST[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_DATE_PROCESSED[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_DATE_LAST_CALLED[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_DATE_LAST_DISPLAYED[$ARRAY_CTR],
                    $PARAM_ADD_QUEUING_STATUS[$ARRAY_CTR],
                    
                    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)
    {
        PHPCUS_PARAM::CUS_JSON_ERROR($ERR);
    }
}
else
{
    PHPCUS_PARAM::CUS_JSON_ERROR('Missing parameters');
}

?>