Runner.buttonEvents['2566'] = function( pageObj, proxy, pageid ) {
	var gid = '2566';
	//register a new button
	pageObj.buttonNames[ pageObj.buttonNames.length ] = gid;
		
	if ( !pageObj.buttonEventBefore[ gid ] ) {
		pageObj.buttonEventBefore[ gid ] = function( params, ctrl, pageObj, proxy, pageid, rowData, row, submit ) {		
			var ajax = ctrl;
			// ======================================================
// CLIENT BEFORE - CREATE PRODUCTION SCHEDULE
// Top/List Button
// Page: production_suggestion_detail_list.php?masterkey1=2
// ======================================================

var runId = "";

try {
    var urlParams = new URLSearchParams(window.location.search);
    runId = urlParams.get("masterkey1");
} catch (e) {
    console.log("Unable to read masterkey1:", e);
}

if (!runId || runId === "" || runId === "0") {
    swal("Error", "Cannot get Production Suggestion Run ID from URL.", "error");
    return false;
}

params["run_id"] = runId;

if (!confirm("Create Production Schedule for all valid suggested finished goods under this run?")) {
    return false;
}

return true;			
		}
	}

	if ( !pageObj.buttonEventAfter[ gid ] ) {
		pageObj.buttonEventAfter[ gid ] = function( result, ctrl, pageObj, proxy, pageid, rowData, row, params ) {
			var ajax = ctrl;
			// ======================================================
// CLIENT AFTER - CREATE PRODUCTION SCHEDULE
// ======================================================

console.log("Create Production Schedule result:", result);

if (typeof result === "undefined" || result === null) {
    swal(
        "Server Error",
        "No response from server. Please check PHPRunner PHP error log or Network response.",
        "error"
    );
    return;
}

if (typeof result === "string") {
    try {
        result = JSON.parse(result);
    } catch (e) {
        swal("Server Error", result, "error");
        return;
    }
}

if (result.success) {
    swal({
        title: "Created",
        text: result.message,
        icon: "success",
        button: "Open Production Plan"
    }).then(function () {
        window.location.href = "production_plan_edit.php?editid1=" + result.production_plan_id;
    });
} else {
    swal("Unable to create", result.message || "Unknown server error.", "warning");
}
		}
	}

	$('a[data-event=' + gid + ']').each( function() {
		if ( $(this).closest('.gridRowAdd').length ) {
			return;
		}
		var rowId = Runner.genId();
		var eventId = gid + "_" + rowId;
		$(this).attr( 'data-event', eventId );
		this.id = this.id + "_" + Runner.genId();
		var buttonObj = new Runner.form.Button({
			id: eventId,
			btnName: gid
		});
		buttonObj.init( {args: [ pageObj, proxy, pageid ]} );
	});

};

