var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onRowChanged(function (rowIndex) {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0)
            return;

        var state = gridBuilder.getCellValue(rowIndex, "IsConfirm").toString().toLowerCase() === "true";
        if (!state) {
            $("#liState > a").text(window.top.documentConfirmation);
        }
        else {
            $("#liState > a").text(window.top.returnConfirmation);
        }
    });

    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        gridBuilder: gridBuilder,
        url: "TreasuryApi/GetFundDocumentList",
        data: {
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber(),
            selectionCriteria: gridBuilder.selectionViewMode ? "'" + gridBuilder.selectionList.join("','") + "'" : null
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else {
                gridBuilder.loadData(data);
            }
        }
    });
}
function print() {
    var info = getDocumentInfo();
    if (info) {
        var title = window.top.printDocument;
        var src = "Report/Default?" + (info.DocumentSerial ? "GrpName=Trs_Tankhah_Sanad&Codesanad=" + info.DocumentSerial : "GrpName=Trs_SuratTankhahList");
        openInModal('', src, title, "", "", false);
    }
}
function doOperation(operation) {
    var src = "Treasury/FundDocumentSave";
    var title;
    if (operation === "insert") {
        title = window.top.fundDocument;
        openInTab(src, title, 'img/pages/menu/121.svg', true);
    }
    else if (operation === "edit") {
        var info = getDocumentInfo();
        if (info) {
            showLoading();
            title = window.top.editFundDocument;
            openInTab(src + "?code=" + info.DocumentSerial + "&state=" + info.IsConfirm, title, 'img/pages/menu/121.svg', true);
        }
    }
}
function deleteFundDocument() {
    var selected = getSelectedCodes();
    if (selected.serials.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        if (selected.acc.some(x => x)) {
            showMsgModal(0, window.top.fundDocNotDelete);
            return;
        }
        else if (selected.isConfirms.some(x => x.toString() === "true")) {
            showMsgModal(0, window.top.fundNotDelete);
            return;
        }
        ajax({
            loading: true,
            url: "TreasuryApi/DeleteFundDocument",
            data: { codeList: selected.serials },
            success: function (result) {
                showMsgModal(2, result.FinalMessage.join("<br />"));
                loadGrid();
            }
        });
    }
}
function changeFundDocumentState() {
    var selected = getSelectedCodes();
    if (selected.serials.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (!checkAllRecordType(selected.isConfirms)) {
        showMsgModal(0, window.top.sameTypeDocument);
        return;
    }

    if (confirm(window.top.changeStatus)) {
        if (selected.acc.some(x => x)) {
            showMsgModal(0, window.top.fundDocNotState);
            return;
        }
        ajax({
            loading: true,
            url: "TreasuryApi/ChangeFundDocumentState",
            data: {
                codeList: selected.serials,
                state: !(selected.isConfirms[0].toString().toLowerCase() === "true")
            },
            success: function (result) {
                showMsgModal(2, result.FinalMessage.join("<br />"));
                loadGrid();
            }
        });
    }
}
function getDocumentInfo() {
    var returnValue = {};
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    returnValue["DocumentSerial"] = gridBuilder.getCellValue(rowIndex, "DocumentSerial");
    returnValue["AccDocumentCode"] = gridBuilder.getCellValue(rowIndex, "AccDocumentCode");
    returnValue["IsConfirm"] = gridBuilder.getCellValue(rowIndex, "IsConfirm").toString().toLowerCase() === "true";
    return returnValue;
}
function refresh() {
    loadGrid();
}

function getSelectedCodes() {
    var selectedSerials = gridBuilder.getSelectedRowsCellValue("DocumentSerial");
    var selectedAccCodes = gridBuilder.getSelectedRowsCellValue("AccDocumentCode");
    var selectedIsConfirm = gridBuilder.getSelectedRowsCellValue("IsConfirm");
    if (selectedSerials.length === 0) {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex >= 0) {
            selectedSerials.push(gridBuilder.getCellValue(rowIndex, "DocumentSerial"));
            selectedAccCodes.push(gridBuilder.getCellValue(rowIndex, "AccDocumentCode"));
            selectedIsConfirm.push(gridBuilder.getCellValue(rowIndex, "IsConfirm"));
        }
    }
    return { serials: selectedSerials, acc: selectedAccCodes, isConfirms: selectedIsConfirm };
}
function checkAllRecordType(states) {
    var type = states[0];
    var result = true;
    for (var i = 0; i < states.length; i++) {
        if (states[i] !== type) {
            result = false;
        }
    }
    return result;
}







