
Runner.pages.PageSettings.addPageEvent( 
	"purchaseorderdetail", 
	"add", 
	"afterPageReady", 
	function(pageObj, proxy, pageid, inlineRow, inlineObject, row ) {
		// ======================================================
// purchaseorderdetail - JavaScript OnLoad
// Inline Add/Edit
//
// Editable:
//   - SKU (item_id)
//   - Unit Cost
//   - Remarks
//
// Disabled:
//   - Mother Facility
//   - Parent Item
//   - Item Description
//   - Quantity
//   - UOM
//   - Amount
//
// All controls are enabled before saving so their
// values are submitted.
// ======================================================

// ------------------------------------------------------
// Controls
// ------------------------------------------------------
var ctrlWarehouse       = Runner.getControl(pageid, "warehouse");
var ctrlMotherFacility  = Runner.getControl(pageid, "mother_facility_id");
var ctrlParentItem      = Runner.getControl(pageid, "parent_item_id");
var ctrlSku             = Runner.getControl(pageid, "item_id");
var ctrlItemDescription = Runner.getControl(pageid, "item_description");
var ctrlQty             = Runner.getControl(pageid, "quantity");
var ctrlUnitCost        = Runner.getControl(pageid, "unit_cost");
var ctrlUom             = Runner.getControl(pageid, "uom");
var ctrlAmount          = Runner.getControl(pageid, "amount");
var ctrlRemarks         = Runner.getControl(pageid, "remarks");
var ctrlPrDetailId      = Runner.getControl(pageid, "pr_detail_id");

// ------------------------------------------------------
// Safely disable a control
// ------------------------------------------------------
function disableCtrl(ctrl) {
    if (ctrl) {
        ctrl.setDisabled();
    }
}

// ------------------------------------------------------
// Safely enable a control
// ------------------------------------------------------
function enableCtrl(ctrl) {
    if (ctrl) {
        ctrl.setEnabled();
    }
}

// ------------------------------------------------------
// Force Remarks to remain editable
// ------------------------------------------------------
function enableRemarks() {

    if (ctrlRemarks) {
        ctrlRemarks.setEnabled();

        // Extra protection for PHPRunner inline controls
        if (typeof ctrlRemarks.getElement === "function") {
            var remarksElement = ctrlRemarks.getElement();

            if (remarksElement) {
                $(remarksElement)
                    .prop("disabled", false)
                    .prop("readonly", false)
                    .removeAttr("disabled")
                    .removeAttr("readonly");
            }
        }
    }

    // Fallback for dynamically generated inline input
    $(
        "input[name^='value_remarks_'], " +
        "textarea[name^='value_remarks_'], " +
        "[data-fieldname='remarks'] input, " +
        "[data-fieldname='remarks'] textarea"
    )
    .prop("disabled", false)
    .prop("readonly", false)
    .removeAttr("disabled")
    .removeAttr("readonly");
}

// ------------------------------------------------------
// Convert formatted value to number
// ------------------------------------------------------
function toNumber(value) {
    value = value || "0";
    value = value.toString().replace(/,/g, "");

    var numberValue = parseFloat(value);

    return isNaN(numberValue)
        ? 0
        : numberValue;
}

// ------------------------------------------------------
// Compute Amount = Quantity × Unit Cost
// ------------------------------------------------------
function computeAmount() {

    if (!ctrlQty || !ctrlUnitCost || !ctrlAmount) {
        return;
    }

    var quantity = toNumber(ctrlQty.getValue());
    var unitCost = toNumber(ctrlUnitCost.getValue());

    var amount = quantity * unitCost;

    ctrlAmount.setValue(amount.toFixed(2));
}

// ------------------------------------------------------
// Configure disabled fields
// ------------------------------------------------------
disableCtrl(ctrlWarehouse);
disableCtrl(ctrlMotherFacility);
disableCtrl(ctrlParentItem);
disableCtrl(ctrlItemDescription);
disableCtrl(ctrlQty);
disableCtrl(ctrlUom);
disableCtrl(ctrlAmount);
disableCtrl(ctrlPrDetailId);

// ------------------------------------------------------
// Keep these fields editable
// ------------------------------------------------------
enableCtrl(ctrlSku);
enableCtrl(ctrlUnitCost);
enableRemarks();

// Run again after PHPRunner finishes initializing
setTimeout(function () {
    enableRemarks();
}, 300);

setTimeout(function () {
    enableRemarks();
}, 800);

// ------------------------------------------------------
// Recompute amount when Unit Cost changes
// ------------------------------------------------------
if (ctrlUnitCost) {

    ctrlUnitCost.on("change", function () {
        computeAmount();
    });

    ctrlUnitCost.on("keyup", function () {
        computeAmount();
    });

    ctrlUnitCost.on("blur", function () {
        computeAmount();
    });
}

// ------------------------------------------------------
// SKU change
// ------------------------------------------------------
if (ctrlSku) {

    ctrlSku.on("change", function () {
        computeAmount();

        // Keep remarks enabled after dependent lookup refresh
        setTimeout(function () {
            enableRemarks();
        }, 300);
    });
}

// Initial computation
computeAmount();

// ------------------------------------------------------
// Before Save
// Enable all controls so PHPRunner submits the values
// ------------------------------------------------------
this.on("beforeSave", function (
    formObj,
    fieldControlsArr,
    pageObj
) {

    enableCtrl(ctrlWarehouse);
    enableCtrl(ctrlMotherFacility);
    enableCtrl(ctrlParentItem);
    enableCtrl(ctrlSku);
    enableCtrl(ctrlItemDescription);
    enableCtrl(ctrlQty);
    enableCtrl(ctrlUnitCost);
    enableCtrl(ctrlUom);
    enableCtrl(ctrlAmount);
    enableCtrl(ctrlRemarks);
    enableCtrl(ctrlPrDetailId);

    enableRemarks();
    computeAmount();

    return true;
});


// ======================================================
// PURCHASE ORDER DETAIL - LIST PAGE JAVASCRIPT ONLOAD
// Automatically place all existing detail rows
// into Inline Edit mode
// ======================================================


});


Runner.pages.PageSettings.addPageEvent( 
	"purchaseorderdetail", 
	"edit", 
	"afterPageReady", 
	function(pageObj, proxy, pageid, inlineRow, inlineObject, row ) {
		// ======================================================
// purchaseorderdetail - JavaScript OnLoad
// Inline Add/Edit
//
// Editable:
//   - SKU (item_id)
//   - Unit Cost
//   - Remarks
//
// Disabled:
//   - Mother Facility
//   - Parent Item
//   - Item Description
//   - Quantity
//   - UOM
//   - Amount
//
// All controls are enabled before saving so their
// values are submitted.
// ======================================================

// ------------------------------------------------------
// Controls
// ------------------------------------------------------
var ctrlWarehouse       = Runner.getControl(pageid, "warehouse");
var ctrlMotherFacility  = Runner.getControl(pageid, "mother_facility_id");
var ctrlParentItem      = Runner.getControl(pageid, "parent_item_id");
var ctrlSku             = Runner.getControl(pageid, "item_id");
var ctrlItemDescription = Runner.getControl(pageid, "item_description");
var ctrlQty             = Runner.getControl(pageid, "quantity");
var ctrlUnitCost        = Runner.getControl(pageid, "unit_cost");
var ctrlUom             = Runner.getControl(pageid, "uom");
var ctrlAmount          = Runner.getControl(pageid, "amount");
var ctrlRemarks         = Runner.getControl(pageid, "remarks");
var ctrlPrDetailId      = Runner.getControl(pageid, "pr_detail_id");

// ------------------------------------------------------
// Safely disable a control
// ------------------------------------------------------
function disableCtrl(ctrl) {
    if (ctrl) {
        ctrl.setDisabled();
    }
}

// ------------------------------------------------------
// Safely enable a control
// ------------------------------------------------------
function enableCtrl(ctrl) {
    if (ctrl) {
        ctrl.setEnabled();
    }
}

// ------------------------------------------------------
// Force Remarks to remain editable
// ------------------------------------------------------
function enableRemarks() {

    if (ctrlRemarks) {
        ctrlRemarks.setEnabled();

        // Extra protection for PHPRunner inline controls
        if (typeof ctrlRemarks.getElement === "function") {
            var remarksElement = ctrlRemarks.getElement();

            if (remarksElement) {
                $(remarksElement)
                    .prop("disabled", false)
                    .prop("readonly", false)
                    .removeAttr("disabled")
                    .removeAttr("readonly");
            }
        }
    }

    // Fallback for dynamically generated inline input
    $(
        "input[name^='value_remarks_'], " +
        "textarea[name^='value_remarks_'], " +
        "[data-fieldname='remarks'] input, " +
        "[data-fieldname='remarks'] textarea"
    )
    .prop("disabled", false)
    .prop("readonly", false)
    .removeAttr("disabled")
    .removeAttr("readonly");
}

// ------------------------------------------------------
// Convert formatted value to number
// ------------------------------------------------------
function toNumber(value) {
    value = value || "0";
    value = value.toString().replace(/,/g, "");

    var numberValue = parseFloat(value);

    return isNaN(numberValue)
        ? 0
        : numberValue;
}

// ------------------------------------------------------
// Compute Amount = Quantity × Unit Cost
// ------------------------------------------------------
function computeAmount() {

    if (!ctrlQty || !ctrlUnitCost || !ctrlAmount) {
        return;
    }

    var quantity = toNumber(ctrlQty.getValue());
    var unitCost = toNumber(ctrlUnitCost.getValue());

    var amount = quantity * unitCost;

    ctrlAmount.setValue(amount.toFixed(2));
}

// ------------------------------------------------------
// Configure disabled fields
// ------------------------------------------------------
disableCtrl(ctrlWarehouse);
disableCtrl(ctrlMotherFacility);
disableCtrl(ctrlParentItem);
disableCtrl(ctrlItemDescription);
disableCtrl(ctrlQty);
disableCtrl(ctrlUom);
disableCtrl(ctrlAmount);
disableCtrl(ctrlPrDetailId);

// ------------------------------------------------------
// Keep these fields editable
// ------------------------------------------------------
enableCtrl(ctrlSku);
enableCtrl(ctrlUnitCost);
enableRemarks();

// Run again after PHPRunner finishes initializing
setTimeout(function () {
    enableRemarks();
}, 300);

setTimeout(function () {
    enableRemarks();
}, 800);

// ------------------------------------------------------
// Recompute amount when Unit Cost changes
// ------------------------------------------------------
if (ctrlUnitCost) {

    ctrlUnitCost.on("change", function () {
        computeAmount();
    });

    ctrlUnitCost.on("keyup", function () {
        computeAmount();
    });

    ctrlUnitCost.on("blur", function () {
        computeAmount();
    });
}

// ------------------------------------------------------
// SKU change
// ------------------------------------------------------
if (ctrlSku) {

    ctrlSku.on("change", function () {
        computeAmount();

        // Keep remarks enabled after dependent lookup refresh
        setTimeout(function () {
            enableRemarks();
        }, 300);
    });
}

// Initial computation
computeAmount();

// ------------------------------------------------------
// Before Save
// Enable all controls so PHPRunner submits the values
// ------------------------------------------------------
this.on("beforeSave", function (
    formObj,
    fieldControlsArr,
    pageObj
) {

    enableCtrl(ctrlWarehouse);
    enableCtrl(ctrlMotherFacility);
    enableCtrl(ctrlParentItem);
    enableCtrl(ctrlSku);
    enableCtrl(ctrlItemDescription);
    enableCtrl(ctrlQty);
    enableCtrl(ctrlUnitCost);
    enableCtrl(ctrlUom);
    enableCtrl(ctrlAmount);
    enableCtrl(ctrlRemarks);
    enableCtrl(ctrlPrDetailId);

    enableRemarks();
    computeAmount();

    return true;
});


// ======================================================
// PURCHASE ORDER DETAIL - LIST PAGE JAVASCRIPT ONLOAD
// Automatically place all existing detail rows
// into Inline Edit mode
// ======================================================


});


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

});







