
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Manage Inventory functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


var InventoryActionDbTable = null;
var InventoryActionRecordId = null;
var InventoryLocation = null;


function OnAddInventoryLocation(sDbTable, sRecordId) {

    InventoryActionDbTable = sDbTable;
    InventoryActionRecordId = sRecordId;
    InventoryLocation = "";

    var proceedcallback = OnAddInventoryLocationCallback;
    var proceedobject = null;
    PopupPromptForValueBox("Add Inventory Location", "Enter a locatoin", "", proceedcallback, proceedobject);
}

function OnDeleteInventoryLocation(sDbTable, sRecordId, sLocation) {

    InventoryActionDbTable = sDbTable;
    InventoryActionRecordId = sRecordId;
    InventoryLocation = sLocation;

    var proceedcallback = OnDeleteInventoryLocationCallback;
    var proceedobject = null;
    PopupYesNoWithCallback("Delete Inventory Location", "Are you sure you wish to delete the inventory location?\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;" + InventoryLocation + "\r\n\r\nThis operation cannot be undone.", proceedcallback, proceedobject);
}


// EditCellStarted
// Event which is raised after start cell editing.
// Function takes arguments evt and ui.
// Use ui.owner to get reference to igGridUpdating.
// Use ui.owner.grid to get reference to igGrid.
// Use ui.rowID to get key or index of row.
// Use ui.columnIndex to get index of column.
// Use ui.columnKey to get key of column.
// Use ui.editor to get reference to igEditor.
// Use ui.value to get value of editor.
// Use ui.rowAdding to check if that event is raised while new-row-adding.
// https://www.igniteui.com/help/api/2021.2/ui.iggridupdating.html
function InventoryLocationsGridEditCellStarted(evt, ui) {

    var GridId = evt.target.id;

    //ToastError("Start edit mode");
}




// EditCellEnded
// Event which is raised after end cell editing.
// Function takes arguments evt and ui.
// Use ui.owner to get reference to igGridUpdating.
// Use ui.owner.grid to get reference to igGrid.
// Use ui.rowID to get key or index of row.
// Use ui.columnIndex to get index of column.
// Use ui.columnKey to get key of column.
// Use ui.editor to get reference to igEditor.
// Use ui.value to get value of cell.
// Use ui.oldValue to get old value.
// Use ui.update to check if cell was modified and data source will be updated.
// Use ui.rowAdding to check if that event is raised while new-row-adding.
// https://www.igniteui.com/help/api/2021.2/ui.iggridupdating.html
function InventoryLocationsGridEditCellEnded(evt, ui) {

    // Any change?
    if (!ui.update)
        return;
    if (!evt.target.id)
        return;
    if (!ui.rowID)
        return;
    if (!ui.columnKey)
        return;

    AjaxUpdateInventoryLocation(ui.rowID, ui.columnKey, ui.oldValue, ui.value);
}


function OnAddInventoryLocationCallback(prompt) {
    InventoryLocation = prompt.sValue;
    AjaxAddInventoryLocation(InventoryActionDbTable, InventoryActionRecordId, InventoryLocation);
}

function OnDeleteInventoryLocationCallback(prompt) {
    AjaxDeleteInventoryLocation(InventoryActionDbTable, InventoryActionRecordId, InventoryLocation)
}

function AjaxAddInventoryLocation(sDbTable, sRecordId, sLocation) {

    if (!sDbTable)
        return;
    if (!sRecordId)
        return;
    if (!sLocation)
        return;

    var params = {
        "DbTable": sDbTable,
        "RecordId": sRecordId,
        "Location": sLocation
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxAddInventoryLocation",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {

                RefreshPropertyEditorPartialView();
                RefreshSelectedContent();

                ToastMessage("Added", response.Location);
            }
            else {
                ToastError("Delete", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxAddInventoryLocation", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}


function AjaxDeleteInventoryLocation(sDbTable, sRecordId, sLocation) {

    if (!sDbTable)
        return;
    if (!sRecordId)
        return;
    if (!sLocation)
        return;

    var params = {
        "DbTable": sDbTable,
        "RecordId": sRecordId,
        "Location": sLocation
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxDeleteInventoryLocation",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {

                RefreshPropertyEditorPartialView();
                RefreshSelectedContent();

                ToastMessage("Deleted", response.Location);
            }
            else {
                ToastError("Delete", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxDeleteInventoryLocation", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}

function AjaxUpdateInventoryLocation(sRecordId, ColumnKey, sOldValue, sNewValue) {

    if (!sRecordId)
        return;
    if (!ColumnKey)
        return;

    var params = {
        "RecordId": sRecordId,
        "ColumnKey": ColumnKey,
        "OldValue": sOldValue,
        "NewValue": sNewValue,
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxUpdateInventoryLocation",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {
                RefreshPropertyEditorPartialView();
                RefreshSelectedContent();
            }
            else {
                ToastError("Update", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxUpdateInventoryLocation", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}



//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Inventory Bom Items


function OnMoveInventoryBomItem(sDbTable, sInventoryId, sInventoryBomItemId, sInventoryItemId, sInventoryName, bMoveUp) {

    //ToastError("OnMoveInventoryBomItem", "Not implemented yet. Check back later!");

    AjaxMoveInventoryBomItem(sInventoryName, sInventoryItemId, sInventoryId, bMoveUp);
}


var InventoryRemoveBomItem = {};

function OnRemoveInventoryBomItem(sDbTable, sInventoryId, sInventoryBomItemId, sInventoryItemId, sInventoryName) {

    //ToastError("OnRemoveInventoryBomItem", "Not implemented yet. Check back later!");

    // Remember these values for the callback
    InventoryRemoveBomItem = {};
    InventoryRemoveBomItem.sDbTable = sDbTable;
    InventoryRemoveBomItem.sInventoryId = sInventoryId;
    InventoryRemoveBomItem.sInventoryBomItemId = sInventoryBomItemId;
    InventoryRemoveBomItem.sInventoryItemId = sInventoryItemId;
    InventoryRemoveBomItem.sInventoryName = sInventoryName;

    var proceedcallback = OnRemoveInventoryBomItemCallback;
    var proceedobject = null;
    PopupYesNoWithCallback("Remove BOM Item", "Are you sure you wish to remove the item?\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;" + sInventoryName + "\r\n\r\nThis operation cannot be undone.", proceedcallback, proceedobject);

    // Moved to callback below
    //AjaxRemoveInventoryItemFromBom(sInventoryName, sInventoryItemId, sInventoryId);
}

function OnRemoveInventoryBomItemCallback(prompt) {
    // Callback after answering "Yes" prompt
    AjaxRemoveInventoryItemFromBom(InventoryRemoveBomItem.sInventoryName, InventoryRemoveBomItem.sInventoryItemId, InventoryRemoveBomItem.sInventoryId);
}

function OnAddInventoryBomItemByPopupMenu(Element, sDbTable, sInventoryId) {

    if (!Element)
        return;
    var ElementObj = $(Element);
    if (!ElementObj)
        return;
    if (!ElementObj.length)
        return;

    AjaxGetRecordItemsContentMenu(event, ElementObj, ""/*ContentType*/, sDbTable, sInventoryId, "OnClickAddInventoryBomItem");
}

function OnAddInventoryBomItemByPopupList(Element, sDbTable, sInventoryId) {

    if (!Element)
        return;
    var ElementObj = $(Element);
    if (!ElementObj)
        return;
    if (!ElementObj.length)
        return;

    InventoryActionDbTable = sDbTable;
    InventoryActionRecordId = sInventoryId;

    CreatePopupForRecordsetList(ElementObj, "PopupListDiv-InvItems-", sDbTable, sInventoryId, "OnClickAddInventoryBomItem", null/*PerformMenuAction*/);
}

function OnClickAddInventoryBomItem(evt, sToAddInventoryName, sToAddInventoryId) {

    // These need to be set up earlier
    //InventoryActionDbTable
    //InventoryActionRecordId

    //ToastError("OnClickAddInventoryBomItem", "Added\r\n" + sToAddInventoryName + "\r\n" + sToAddInventoryId + "\r\n" + InventoryActionDbTable + "\r\n" + InventoryActionRecordId);

    AjaxAddInventoryItemToBom(sToAddInventoryName, sToAddInventoryId, InventoryActionRecordId);

    CloseLastPopup();
}

function OnChangeInventoryBomItemQty(Element) {

    if (!Element)
        return;
    var ElementObj = $(Element);
    if (!ElementObj)
        return;
    if (!ElementObj.length)
        return;

    // Record values are stored in the element attributes
    var sDbTable = ElementObj.attr("data-dbtable");
    var sDbColumn = ElementObj.attr("data-dbcolumn");
    var sInventoryId = ElementObj.attr("data-invid");
    var sInventoryName = ElementObj.attr("data-invname");
    var sInventoryBomItemId = ElementObj.attr("data-invbomitemid");
    var sInventoryItemId = ElementObj.attr("data-invitemid");
    var OldValue = ElementObj.attr("value");
    var NewValue = ElementObj.val();

    // Any change?
    if (OldValue == NewValue)
        return;

    //console.log(Element);
    //ToastError("OnChangeInventoryBomItemQty", "Not implemented yet. Check back later!");

    AjaxChangeInventoryBomItemQty(sInventoryName, sInventoryItemId, sInventoryId, NewValue);
}

function AjaxAddInventoryItemToBom(sToAddInventoryName, sToAddInventoryId, sAddToInventoryBomId) {

    if (!sToAddInventoryName)
        return;
    if (!sToAddInventoryId)
        return;
    if (!sAddToInventoryBomId)
        return;

    var params = {
        "ToAddInventoryName": sToAddInventoryName,
        "ToAddInventoryId": sToAddInventoryId,
        "AddToInventoryBomId": sAddToInventoryBomId
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxAddInventoryItemToBom",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {

                RefreshSelectedContent();

                ToastMessage("Added", sToAddInventoryName);
            }
            else {
                ToastError("Error", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxAddInventoryItemToBom", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}


function AjaxRemoveInventoryItemFromBom(sToRemoveInventoryName, sToRemoveInventoryId, sRemoveFromInventoryBomId) {

    if (!sToRemoveInventoryName)
        return;
    if (!sToRemoveInventoryId)
        return;
    if (!sRemoveFromInventoryBomId)
        return;

    var params = {
        "ToRemoveInventoryName": sToRemoveInventoryName,
        "ToRemoveInventoryId": sToRemoveInventoryId,
        "RemoveFromInventoryBomId": sRemoveFromInventoryBomId
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxRemoveInventoryItemFromBom",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {

                RefreshSelectedContent();

                ToastMessage("Removed", sToRemoveInventoryName);
            }
            else {
                ToastError("Error", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxRemoveInventoryItemFromBom", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}




function AjaxMoveInventoryBomItem(sToMoveInventoryName, sToMoveInventoryId, sMoveWithinInventoryBomId, bMoveUp) {

    if (!sToMoveInventoryName)
        return;
    if (!sToMoveInventoryId)
        return;
    if (!sMoveWithinInventoryBomId)
        return;

    var params = {
        "ToMoveInventoryName": sToMoveInventoryName,
        "ToMoveInventoryId": sToMoveInventoryId,
        "MoveWithinInventoryBomId": sMoveWithinInventoryBomId,
        "MoveUp": bMoveUp
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxMoveInventoryBomItem",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {

                RefreshSelectedContent();

                ToastMessage("Moved", sToMoveInventoryName);
            }
            else {
                ToastError("Error", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxMoveInventoryBomItem", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}




function AjaxChangeInventoryBomItemQty(sToChangeInventoryName, sToChangeInventoryId, sChangeInventoryBomId, NewValue) {

    if (!sToChangeInventoryName)
        return;
    if (!sToChangeInventoryId)
        return;
    if (!sChangeInventoryBomId)
        return;

    var params = {
        "ToChangeInventoryName": sToChangeInventoryName,
        "ToChangeInventoryId": sToChangeInventoryId,
        "ChangeInventoryBomId": sChangeInventoryBomId,
        "NewValue": NewValue,
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxChangeInventoryBomItemQty",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (response) {
            if (response.Success) {

                // Do not refresh because it closes the popup
                //RefreshSelectedContent();

                // Toaster is distracting and covers the popup
                //ToastMessage("Updated Qty", sToChangeInventoryName + ", " + NewValue);
            }
            else {
                ToastError("Error", response.ErrorResults);
            }
        },
        error: function (xhr, status, error) {
            var errormsg = "Error " + xhr.status + ", " + xhr.statusText + "\r\nText: " + BarcodeText;
            ToastError("AjaxChangeInventoryBomItemQty", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}










