var gridBuilder;
var code = getQuery("code");
var date = getQuery("date");
var currentPage = 1;
var json = new Array();

$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onSetMaskEdit(setEventMaskEdit);
    gridBuilder.onExportClick(exportSampleExcel);
    gridBuilder.onImportClick(function (gridId, type, base64) {
        if (type === "importXml") {
            importXml(gridId, base64);
        }
        else {
            ajax({
                loading: true,
                url: "GeneralApi/ImportExcel",
                data: {
                    key: "BankConflict",
                    base64: base64
                },
                success: function (result) {
                    var data = eval(result.result);
                    gridBuilder.loadData(data);
                }
            });
        }
    });
    
    initial();
});
async function initial() {
    await getManualBankTurnoverList();
}
async function getManualBankTurnoverList() {
    var startDate = $('#lblToDate').text();
    await ajax({
        gridBuilder: gridBuilder,
        url: "AccountingApi/GetManualBankTurnoverList",
        data: {
            fromDate: date || startDate,
            conflictBankCode: code
        },
        success: function (result) {
            if (result.length > 0) {
                gridBuilder.loadData(result);
            }
            else
                gridBuilder.addNewRow();

            $("#divMain").show();
            closeLoading();
        }
    });
}
function gridCellChange(fieldName) {
    var rowIndex = gridBuilder.getActiveRowIndex();
    var columnIndex = gridBuilder.getCellIndex(fieldName);
    var value = gridBuilder.getActiveRowCellValue(fieldName);
    if (fieldName === "Debit" || fieldName === "Credit") {
        if (fieldName === "Debit") {
            if (Number(value) > 0) {
                gridBuilder.setCellValue(rowIndex, "Credit", "0");
                
            }
        } else if (fieldName === "Credit") {
            if (Number(value) > 0) {
                gridBuilder.setCellValue(rowIndex, "Debit", "0");
               
            }
        }
       
    }
    if (gridBuilder.isRowEditMode(rowIndex) && gridBuilder.getActiveCellIndex() === columnIndex)
        gridBuilder.setEditRow(rowIndex, columnIndex + 1, true);
}
function sort(array) {
    array.sort(function (a, b) {
        return Number(a.TrackingDateLocal.replace(/\//g, '')) - Number(b.TrackingDateLocal.replace(/\//g, ''));
    });
}
function save() {
    var data = gridBuilder.getDataArray()["data"].filter(function (item) { return item.Credit || item.Debit || item.Remained; });
    data.forEach(function (item) {
        if (!item.Remained)
            item.Remained = 0;
    });
    ajax({
        loading: true,
        url: 'AccountingApi/SaveManualBankTurnoverList',
        data: { fromDate: date, conflictBankCode: Number(code), manualBankTurnoverList: data },
        success: function () {
            showMsgModal(2, window.top.savedSuccessfully);
            refreshParent('refresh()');
            closeTopModal();
        }
    });
}
function openRemoveModal() {
    var data = gridBuilder.getDataArray()["data"].filter(function (item) { return (item.Credit || item.Debit || item.Remained) && item._status !== "deleted"; });
    sort(data);
    var title = window.top.deletee;
    var src = "Accounting/ConflictBankRemove?lastSanadDate=" + data[data.length - 1].TrackingDateLocal;
    openInModal('', src, title, 430, 275);
}
function removeArtikls(fromDate) {
    var data = gridBuilder.getDataArray()["data"].filter(function (item) { return (item.Credit || item.Debit || item.Remained) && item._status !== "deleted"; });
    var newData = JSON.parse(JSON.stringify(data));
    sort(newData);
    data.forEach(function (item) {
        if (Number(item.TrackingDateLocal.replace(/\//g, '')) >= Number(fromDate.replace(/\//g, ''))) {
            var index = newData.indexOf(item);
            newData.splice(index, 1);
        }
    });
    gridBuilder.clearRows();
    gridBuilder.loadData(newData);
}
function exportSampleExcel(gridId, type) {
    exportExcel(gridBuilder.getDataArray()["data"], gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
}

