
Runner.pages.PageSettings.addPageEvent( 
	"augmentation_request_for_approval", 
	"add", 
	"afterPageReady", 
	function(pageObj, proxy, pageid, inlineRow, inlineObject, row ) {
		// ======================================================
// AUGMENTATION REQUEST - ADD PAGE - JAVASCRIPT ONLOAD
//
// Auto-populate and disable:
// 1. Request Number
// 2. Request Date
// 3. Mother Facility
// ======================================================

var previewRequestNo =
    proxy["preview_reference_number"] || "";

var currentRequestDate =
    proxy["current_request_date"] || "";

var loggedUserMotherFacilityId =
    parseInt(
        proxy["logged_user_mother_facility_id"],
        10
    ) || 0;

// ------------------------------------------------------
// Get controls
// ------------------------------------------------------
var ctrlRequestNo =
    Runner.getControl(pageid, "request_no");

var ctrlRequestDate =
    Runner.getControl(pageid, "request_date");

var ctrlMotherFacility =
    Runner.getControl(pageid, "mother_facility_id");

// ------------------------------------------------------
// Request Number
// This controls value_request_no_1
// ------------------------------------------------------
function setRequestNumber() {

    ctrlRequestNo =
        Runner.getControl(pageid, "request_no");

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

    if (previewRequestNo !== "") {
        ctrlRequestNo.setValue(previewRequestNo);
    }

    ctrlRequestNo.setDisabled();
}

// ------------------------------------------------------
// Request Date
// ------------------------------------------------------
function setRequestDate() {

    ctrlRequestDate =
        Runner.getControl(pageid, "request_date");

    if (!ctrlRequestDate) {
        console.log("Request Date control not found.");
        return;
    }

    if (currentRequestDate !== "") {
        ctrlRequestDate.setValue(currentRequestDate);
    }

    ctrlRequestDate.setDisabled();
}

// ------------------------------------------------------
// Mother Facility
// ------------------------------------------------------
function setMotherFacility() {

    ctrlMotherFacility =
        Runner.getControl(pageid, "mother_facility_id");

    if (!ctrlMotherFacility) {
        console.log("Mother Facility control not found.");
        return;
    }

    if (loggedUserMotherFacilityId > 0) {
        ctrlMotherFacility.setValue(
            loggedUserMotherFacilityId
        );
    }

    ctrlMotherFacility.setDisabled();
}

// ------------------------------------------------------
// Apply values
// ------------------------------------------------------
function applyAutomaticValues() {

    setRequestNumber();
    setRequestDate();
    setMotherFacility();
}

// Apply immediately
applyAutomaticValues();

// Apply after PHPRunner lookup controls finish loading
setTimeout(function () {
    applyAutomaticValues();
}, 300);

setTimeout(function () {
    applyAutomaticValues();
}, 700);

setTimeout(function () {
    applyAutomaticValues();
}, 1200);

// ------------------------------------------------------
// Re-enable before save so PHPRunner submits the values
// ------------------------------------------------------
this.on(
    "beforeSave",
    function (
        formObj,
        fieldControlsArr,
        pageObj
    ) {

        var saveRequestNo =
            Runner.getControl(
                pageid,
                "request_no"
            );

        var saveRequestDate =
            Runner.getControl(
                pageid,
                "request_date"
            );

        var saveMotherFacility =
            Runner.getControl(
                pageid,
                "mother_facility_id"
            );

        if (saveRequestNo) {
            saveRequestNo.setEnabled();
        }

        if (saveRequestDate) {
            saveRequestDate.setEnabled();
        }

        if (saveMotherFacility) {
            saveMotherFacility.setEnabled();
        }

        return true;
    }
);
});







