var hasCurrencySystem = window.top.$("#hfCurrencySystem").val() === "1";
var baseCurrencyName = window.top.$("#hfBaseCurrencyName").val();
$(document).ready(function () {
    $(".grid").each(function (index, grid) {
        var builder = $(grid).gridBuilder();
        builder.onDblClick(function () {

        });
        builder.onExportClick(function (gridId) {
            loadGrid(gridId, { exportExcel: true });
        });
        if (!hasCurrencySystem) {
            builder.removeColumn("CurrencyName,CurrencyRate,CurrencyPrice");
        }
        else {
            builder.setColumnCaption("Price", window.top.amount + "(" + baseCurrencyName + ")");
        }
    });
    loadGrid("gridTreasuryToTreasury");
});

function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var gridBuilder = $("#" + gridId).gridBuilder();
    ajax({
        gridBuilder: gridBuilder,
        url: "TreasuryApi/GetTransferDocumentList",
        data: {
            type: $("#" + gridId).attr("data-type"),
            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);
        }
    });
}


$(".menu-item").on("click", function () {
    $(".menu-item").removeClass("selected-menu-item");
    $(".div-content").hide();
    $(this).addClass("selected-menu-item");
    $("#" + $(this).attr("data-target")).show();
});

function actionRow(gridId,action){
    var gridBuilder = $("#" + gridId).gridBuilder();
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex == -1) {
        showMsgModal(0, window.top.itemNotSelect);
    }
    else if (action === "delete") {
        var values = gridBuilder.getSelectedRowsCellValue('Code');
        if (values == "") {
            showMsgModal(0, window.top.itemNotSelect);
            return;
        }
        if (confirm(window.top.deleteConfirm)) {
            ajax({
                loading: true,
                url: "TreasuryApi/DeleteTransferDocument",
                data: { codeList: values },
                success: function () {
                    showMsgModal(2, window.top.deletedSuccessfully);
                    loadGrid(gridId);
                }
            });
        }
    }
    else if (action === "print") {
        var values = gridBuilder.getSelectedRowsCellValue('TrsDocumentCode');
        if (values.length === 0)
            values = [gridBuilder.getCellValue(rowIndex, "TrsDocumentCode")];
        var title = window.top.printDocument;
        var src = "Report/Default?GrpName=FormTrsSanad&Inst=" + values.join(',');
        openInModal('', src, title,"","",false);
    }
}

function edit(gridId) {
    var gridBuilder = $("#" + gridId).gridBuilder();
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex == -1)
        return;
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    openTransferDocument(gridId, code);
}
        
function openTransferDocument(gridId,code) {
    var title = "";
    var type = 0;
    switch (gridId){
        case "gridTreasuryToTreasury":
            title = window.top.transferTreasuryToTreasury;
            type = 1;
           break;
        case "gridTreasuryToBank":
            title = window.top.transferTreasuryToBank;
            type = 2;
           break;
        case "gridBankToBank":
            title = window.top.transferBankToBank;
            type = 3;
           break;
        case "gridBankToTreasury":
            title = window.top.transferBankToTreasury;
            type = 4;
           break;
    }
    var src = "Treasury/TransferDocumentSave?type=" + type + (code ? "&code=" + code : "");
    openInModal('', src, title, 450,535,false);
}