var gridBuilder;
var gridBuilderArticle;
var hasCurrencySystem = window.top.$("#hfCurrencySystem").val() === "1";
$(document).ready(function () {
    gridBuilder = $("#grdSanadList").gridBuilder();
    gridBuilderArticle = $("#grdRizSanad").gridBuilder();
    gridBuilder.onRowChanged(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex === -1) {
            return;
        }
        var isSanadUpdated = gridBuilder.getCellValue(rowIndex, "IsCertain").toString() === "true";
        var type = Number(gridBuilder.getCellValue(rowIndex, "TrsOperationCode"));
        if (isSanadUpdated) {
            $('#btnAccept').prop('disabled', true);
            $('#btnNoAccept').prop('disabled', false);
        }
        else {
            $('#btnNoAccept').prop('disabled', true);
            $('#btnAccept').prop('disabled', false);
        }
        $("#btnEdit").prop("disabled", type !== 1 && type !== 2 && type !== 9)
    });
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            return;
        }
        toggleLevel();
        loadGrid("grdRizSanad");
    });
    gridBuilder.onExportClick(function () {
        loadGrid("grdSanadList", { exportExcel: true });
    });
    gridBuilderArticle.onExportClick(function () {
        loadGrid("grdRizSanad", { exportExcel: true });
    });
    if (!hasCurrencySystem) {
        gridBuilder.removeColumn("CurrencyName");
        gridBuilderArticle.removeColumn("CurrencyName,CurrencyPrice");
    }
    
    loadGrid("grdSanadList");
    imgToSvg();
});

function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    if (gridId === "grdSanadList") {
        ajax({
            loading: false,
            gridBuilder: gridBuilder,
            url: "TreasuryApi/GetPayReceivedList",
            data: {
                showOnlyTrs: false,
                trsOperationType: -1,
                fromCode: -1, ToCode: -1,
                fromDate: $("#txtAzDate").val(),
                toDate: $("#txtTaDate").val(),
                selectionCriteria: gridBuilder.selectionViewMode ? "'" + gridBuilder.selectionList.join("','") + "'" : null,
                filter: gridBuilder.getFilter(),
                sort: gridBuilder.getSortFields(),
                pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber()
            },
            success: function (data) {
                if (isExportExcel) {
                    exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
                }
                else {
                    gridBuilder.loadData(data);
                }
            }
        });
    }
    else {
        var rowIndex = gridBuilder.getActiveRowIndex();
        var documentCode = gridBuilder.getCellValue(rowIndex, "Code");
        ajax({
            loading: false,
            gridBuilder: gridBuilderArticle,
            url: "TreasuryApi/GetPayReceivedArticleLiteList",
            data: {
                code: documentCode,
                filter: gridBuilderArticle.getFilter(),
                sort: gridBuilderArticle.getSortFields(),
                pageNo: isExportExcel ? -1 : gridBuilderArticle.getCurrentPageNumber()
            },
            success: function (data) {
                if (isExportExcel) {
                    exportExcel(data, gridBuilderArticle.getColumns(), gridBuilderArticle.getColumnsCaption());
                }
                else
                    gridBuilderArticle.loadData(data);
            }
        });
    }
}

function toggleLevel() {
    var state = $("[data-mask='full-date']").prop("disabled");
    $("[data-mask='full-date']").prop("disabled", !state);
    $("[data-level-toggle]").toggle();
}

function deleteSanad() {
    var values = gridBuilder.getSelectedRowsCellValue('code');
    if (values == "") {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            loading: true,
            url: "TreasuryApi/DeletePayReceive",
            data: { codeList: values },
            success: function () {
                showMsgModal(2, window.top.deletedSuccessfully);
                gridBuilder.setUnselected(-1);
                loadGrid("grdSanadList");
            }
        });
    }
}

function printSanad() {
    var values = gridBuilder.getSelectedRowsCellValue('code');
    if (values == "") {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    var title = window.top.printDocument;
    var src = "Report/Default?GrpName=FormTrsSanad&Inst=" + values.join(',');
    openInModal('', src, title, "", "", false);
}
function changeDocumentState(state) {
    var selectedCodes = gridBuilder.getSelectedRowsCellValue("code");
    if (selectedCodes.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (confirm(window.top.changeStatus)) {
        ajax({
            loading: true,
            url: "TreasuryApi/ChangePayReceiveState",
            data: { codeList: selectedCodes, isCertain: state },
            success: function (result) {
                showMsgModal(2, window.top.doneSuccess);
                gridBuilder.setUnselected(-1);
                loadGrid("grdSanadList");
            }
        });
    }
}
function editDocument() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    var factorSerial = gridBuilder.getCellValue(rowIndex, "FactorSerial");
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    var type = Number(gridBuilder.getCellValue(rowIndex, "TrsOperationCode"));
    var src = "Treasury/";
    var title = "";
    if (factorSerial) {
        showMsgModal(0, window.top.notPossibleEditDocument);
        return;
    }
    if (type === 9) {
        var fromCashDeskCode = gridBuilder.getCellValue(rowIndex, "FromCashDeskCode");
        var toCashDeskCode = gridBuilder.getCellValue(rowIndex, "ToCashDeskCode");
        var fromBankCode = gridBuilder.getCellValue(rowIndex, "FromBankCode");
        var toBankCode = gridBuilder.getCellValue(rowIndex, "ToBankCode");
        var transferCode = gridBuilder.getCellValue(rowIndex, "TransferCode");
        if (fromCashDeskCode && toCashDeskCode) {
            type = 1;
            title = window.top.transferTreasuryToTreasury;
        }
        else if (fromCashDeskCode && toBankCode) {
            type = 2;
            title = window.top.transferTreasuryToBank;
        }
        else if (fromBankCode && toBankCode) {
            type = 3;
            title = window.top.transferBankToBank;
        }
        else if (fromBankCode && toCashDeskCode) {
            type = 4;
            title = window.top.transferBankToTreasury;
        }
        src += "TransferDocumentSave?type=" + type + "&code=" + transferCode + "&manager=1";
        openInModal('', src, title, "750", "450", false);
    }
    else {
        src += "TreasuryPayReceiveSave?type=" + type + "&code=" + code;
        title = type === 1 ? window.top.registerReceive : window.top.registerPayment;
        openInTab(src, title, 'img/pages/menu/321.svg', true);
    }

}