function ajax_report_request(requestData, targetElementId = 'reportArea') {
    // Get the first key from the data object
    const firstKey = Object.keys(requestData)[0];
    if (!firstKey) {
        console.error("The data object is empty or invalid.");
        return false;
    }

    

    // Update the target element with a loading image
    const targetElement = $('#' + targetElementId);
    if (!targetElement.length) {
        console.error(`Element with ID '${targetElementId}' not found.`);
        return false;
    }
    targetElement.html(loadingImage);

    // Get the date range value
    const dateRange = $('#dRange').val();
    if (dateRange) {
        requestData['dRange'] = dateRange;
    }

    // Store the requestData in localStorage with the first key as the identifier
    localStorage.setItem(firstKey, JSON.stringify(requestData));

    // Perform the AJAX request
    $.ajax({
        type: 'POST',
        url: ajUrl, // Ensure 'ajUrl' is defined globally or passed to the function
        data: requestData,
        success: function (response) {
            if (response && typeof response.status !== "undefined") {
                if (response.status == 1) {
                    targetElement.html(response.html);
                } else {
                    swMessageFromJs(response.m || "An unknown error occurred.");
                    targetElement.html('');
                }
            } else {
                targetElement.html('');
                swMessage(AJAX_ERROR_MESSAGE || "Unexpected error occurred.");
            }
        },
        error: function (error) {
            console.error("AJAX request failed:", error);
            targetElement.html('');
            swMessage(AJAX_ERROR_MESSAGE || "Error occurred during the AJAX request.");
        }
    });
}
function contra_list(){
    let debit=$('#s_debit').val();
    let credit=$('#s_credit').val();
    ajax_report_request({contra_list:1,debit:debit,credit:credit},'trDetailsArea');
}
function sales_discount_avg_value_sp(){
    ajax_report_request({sales_discount_avg_value_sp:1});
}
function headStatement(){
    let ledger_id   = parse_int($('#ledger_id').val());
    let type   = parse_int($('#type').val());
    ajax_report_request({headStatement:1,ledger_id:ledger_id,type:type})
}
function doctor_visit_list(){
    let base_id = $('#base_id').val();
    let doctor_id = $('#doctor_id').val();
    ajax_report_request({doctor_visit_list:1,base_id:base_id,doctor_id:doctor_id})
}
function doctor_visit_report(){
    let base_id = parse_int($('#base_id').val());
    let doctor_id = parse_int($('#doctor_id').val());
    let type = parse_int($('#type').val());
    ajax_report_request({doctor_visit_report:1,base_id:base_id,doctor_id:doctor_id,type:type})
}
function income_statement(){
    let date_range   = $('#date_range input[name="date_range[]"]').map( function(){return $(this).val(); }).get(); 
    ajax_report_request({income_statement:1,date_range:date_range});
}
function trial_balance(){
    let date_range   = $('#date_range input[name="date_range[]"]').map( function(){return $(this).val(); }).get(); 
    ajax_report_request({trial_balance:1,date_range:date_range});
}
function income_expense_list(type){
    let hID=$('#shID').val();
    let uID=$('#uID').val();
    ajax_report_request({income_expense_list:1,hID:hID,uID:uID,type:type});
}
function general_ledger_summary(){
    let type = $('#type').val();
    ajax_report_request({general_ledger_summary:1,type});
}
function customer_statement(){
    let customer_id   = parse_int($('#customer_id').val());
    let customer_category_id   = parse_int($('#customer_category_id').val());
    let base_id   = parse_int($('#base_id').val());
    let bazar_id   = parse_int($('#bazar_id').val());
    let type   = $('#type').val();
    let type_zero   = $('#type_zero').val();
    ajax_report_request({customer_statement:1,base_id,customer_category_id,bazar_id,customer_id,type,type_zero});
}
function sales_report(){
    let with_tp  = $('#with_tp').val();
    let toll_sale_type  = $('#toll_sale_type').val();
    ajax_report_request({sales_report:1,with_tp:with_tp,toll_sale_type:toll_sale_type});
}
function customer_visit_list(){
    let base_id = $('#base_id').val();
    let customer_id = $('#customer_id').val();
    ajax_report_request({customer_visit_list:1,base_id:base_id,customer_id:customer_id})
}
function paid_and_unpaid_invoice_report(){
    let base_id = $('#base_id').val();
    let customer_id = $('#customer_id').val();
    let paid_type = $('#paid_type').val();
    let pay_type = $('#pay_type').val();
    ajax_report_request({paid_and_unpaid_invoice_report:1,base_id:base_id,customer_id:customer_id,paid_type:paid_type,pay_type:pay_type})
}