
Runner.pages.PageSettings.addPageEvent( 
	"delivery_receipt", 
	"add", 
	"afterPageReady", 
	function(pageObj, proxy, pageid, inlineRow, inlineObject, row ) {
		// Place event code here.
// Use "Add Action" button to add code snippets.

// Place event code here.
// Use "Add Action" button to add code snippets.

// Place event code here.
// Use "Add Action" button to add code snippets.

// Place event code here.
// Use "Add Action" button to add code snippets.

setTimeout(function() {

    var ctrlReferenceNo = Runner.getControl(pageid, "delivery_receipt_no");

    if (!ctrlReferenceNo) {
        console.log("Reference Number control not found.");
        return;
    }

    if (proxy["preview_reference_number"]) {
        ctrlReferenceNo.setValue(proxy["preview_reference_number"]);
    }

    // Optional: make it readonly
    ctrlReferenceNo.setDisabled();

}, 500);

this.on('beforeSave', function(formObj, fieldControlsArr, pageObj) {

    var ctrlref = Runner.getControl(pageid, "delivery_receipt_no");


    if (ctrlref) {
        ctrlref.setEnabled();
    }
   
    return true;
});


var ctrlMode = Runner.getControl(
    pageid,
    "mode_of_transport"
);

var ctrlSubcategory = Runner.getControl(
    pageid,
    "transport_subcategory_id"
);


// Initially disable Transport Subcategory
if (
    !ctrlMode.getValue()
) {
    ctrlSubcategory.setDisabled();
}


// When Mode of Transport changes
ctrlMode.on(
    "change",
    function()
    {
        // Clear old subcategory
        ctrlSubcategory.clear();

        var mode =
            ctrlMode.getValue();

        if (
            mode !== null &&
            mode !== ""
        ) {
            // Enable subcategory
            ctrlSubcategory.setEnabled();
        }
        else {
            // No parent selection
            ctrlSubcategory.setDisabled();
        }
    }
);
});







