
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Billing rates functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//

var BillingDateRangeDbTable = null;
var BillingDateRangeRecordId = null;
var BillingDateRange = null;


function OnAddBillingDateRange(sDbTable, sRecordId) {

    BillingDateRangeDbTable = sDbTable;
    BillingDateRangeRecordId = sRecordId;
    BillingDateRange = "";

    var proceedcallback = OnAddBillingDateRangeCallback;
    var proceedobject = null;
    PopupPromptForValueBox("Add Billing Date Range", "Enter two dates separated by a dash", "", proceedcallback, proceedobject);
}

function OnDeleteBillingDateRange(sDbTable, sRecordId, sDateRange) {

    BillingDateRangeDbTable = sDbTable;
    BillingDateRangeRecordId = sRecordId;
    BillingDateRange = sDateRange;

    var proceedcallback = OnDeleteBillingDateRangeCallback;
    var proceedobject = null;
    PopupYesNoWithDoubleVerification("Delete Billing Date Range", "Are you sure you wish to delete the date range and all billing rates associated with it?\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;" + BillingDateRange + "\r\n\r\nThis operation cannot be undone.", proceedcallback, proceedobject);
}

function OnEditBillingDateRange(sDbTable, sRecordId, sDateRange) {

    BillingDateRangeDbTable = sDbTable;
    BillingDateRangeRecordId = sRecordId;
    BillingDateRange = sDateRange;

    var proceedcallback = OnUpdateBillingDateRangeCallback;
    var proceedobject = null;
    PopupPromptForValueBox("Edit Billing Date Range", "Enter two dates separated by a dash", sDateRange, 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 BillingDateRangesGridEditCellStarted(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 BillingDateRangesGridEditCellEnded(evt, ui) {

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

    //AjaxUpdateBillingRateDateRange(ui.rowID, ui.columnKey, ui.oldValue, ui.value);
}


function OnAddBillingDateRangeCallback(prompt) {
    BillingDateRange = prompt.sValue;
    AjaxAddBillingDateRange(BillingDateRangeDbTable, BillingDateRangeRecordId, BillingDateRange);
}

function OnDeleteBillingDateRangeCallback(prompt) {
    AjaxDeleteBillingDateRange(BillingDateRangeDbTable, BillingDateRangeRecordId, BillingDateRange)
}

function OnUpdateBillingDateRangeCallback(prompt) {
    AjaxUpdateBillingDateRange(BillingDateRangeDbTable, BillingDateRangeRecordId, BillingDateRange, prompt.sValue)
}

function AjaxAddBillingDateRange(sDbTable, sRecordId, sDateRange) {

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

    var params = {
        "DbTable": sDbTable,
        "RecordId": sRecordId,
        "DateRange": sDateRange
    };

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

                RefreshPropertyEditorPartialView();
                RefreshSelectedContent();

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


function AjaxDeleteBillingDateRange(sDbTable, sRecordId, sDateRange) {

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

    var params = {
        "DbTable": sDbTable,
        "RecordId": sRecordId,
        "DateRange": sDateRange
    };

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

                RefreshPropertyEditorPartialView();
                RefreshSelectedContent();

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

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

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

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

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxUpdateBillingDateRange",
        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("AjaxUpdateBillingDateRange", errormsg);
            console.log('AJAX: ' + errormsg);
        }
    });
}




