// ===============================================================
// Global Var
// ===============================================================-
// Cache
// _______________________________________________________________________
var PROPERTY_REC_CHARGE_ID = 0;

// Buttons [Optional]
// _______________________________________________________________________
var PRINT_BBI = null;

// DGV Controls
// _______________________________________________________________________
var CHARGE_DGV = null;
var CHARGE_LAST_GET_MAP_ARRAY = null;

function FORM_INITIALIZE()
{
    // Cache Class Properties
    // ...........................................
    if(JSVC_COOKIE.VAR_ISSET("PROPERTY_REC_CHARGE_ID"))
    {
        PROPERTY_REC_CHARGE_ID = JSVC_COOKIE.VAR_READ("PROPERTY_REC_CHARGE_ID");
    }	
    else
    {
        window.close();
    }
    // Load DataGridVuew
    // _______________________________________________________________________	
    JSTOOL_CUSTOMIZE.DGVREORDER.APPLY("CHARGE_DGV");
    CHARGE_DGV = DataGridView("CHARGE_DGV");
    
    // 
    // _______________________________________________________________________	
    var GET_MAP_ARRAY = JSCUS_SQL._RECORD._CHARGE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

    JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_CHARGE_ID', PROPERTY_REC_CHARGE_ID);

    CHARGE_LAST_GET_MAP_ARRAY = GET_MAP_ARRAY;

    var CALLBACK_REQUEST = function(IS_SUCCESS, RESPONSE_STRING, KEEP_DGV_ITEMS = false)
    {
        // Disable Dialogs
		// _______________________________________________________________________
		CUS_SEND_DIALOG_TRY_DISMISS();
		
		// Validate
		// _______________________________________________________________________
		if(IS_SUCCESS === false)
		{
			console.error("CONNECTION ERROR: \n" + RESPONSE_STRING);
			return;
		}
		
		// Validate JSON Response
		// _______________________________________________________________________
		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
		{
			console.error("REQUEST ERROR: \n" + RESPONSE_STRING);
			return;
		}
		
		// JSON Empty (Clears the DataGrodView)
		// _______________________________________________________________________
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
		{
			CHARGE_DGV.ROW.CLEAR();
			if(CHARGE_DGV.COLUMN.TOTAL_IS_READY())
			{
				CHARGE_DGV.COLUMN.TOTAL_REFRESH();
			}
			return;
		}
		// Populate DGV
		// _______________________________________________________________________
		CHARGE_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				KEEP_DGV_ITEMS,
				false,
				CHARGE_LAST_GET_MAP_ARRAY['PARAM_SKIP'] + 1,
				[2, 1]
		);
		
        // Text Populate
        JSOC_ELEMENT.VAR_INNER_HTML("REC_CHARGE_CODE_TXT", JSRC_JSON.REF_READ(JSON_OBJECT, "REC_CHARGE_CODE", 1));
        JSOC_ELEMENT.VAR_INNER_HTML("REC_CHARGE_REFERENCE_TXT", JSRC_JSON.REF_READ(JSON_OBJECT, "REC_CHARGE_REFERENCE", 1));
        JSOC_ELEMENT.VAR_INNER_HTML("REC_CHARGE_REC_EMPLOYEE_ID_DESCRIPTION_TXT", JSRC_JSON.REF_READ(JSON_OBJECT, "REC_CHARGE_REC_EMPLOYEE_ID_DESCRIPTION", 1));
        JSOC_ELEMENT.VAR_INNER_HTML("REC_CHARGE_DATE_TXT", JSVC_DATE.VAR_FORMAT( JSRC_JSON.REF_READ(JSON_OBJECT, "REC_CHARGE_DATE", 1),"MM/DD/YYYY"));
        JSOC_ELEMENT.VAR_INNER_HTML("REC_CHARGE_REASON_TXT", JSRC_JSON.REF_READ(JSON_OBJECT, "REC_CHARGE_REASON", 1));
		
		// For Applying CFormat
		// _______________________________________________________________________	
		CHARGE_DGV.COLUMN.CFORMAT("REC_CHARGE_AMOUNT");
        CHARGE_DGV.COLUMN.CFORMAT("REC_CHARGE_COST");
    }

    JSCUS_SQL._RECORD._CHARGE.CUS_GET
    (
        GET_MAP_ARRAY,
        CALLBACK_REQUEST,
        true
    ); 
}

FORM_INITIALIZE();