var visibleIndex;
var trRow;
var id;
var title;
var gridBuilder;
var hasCurrencySystem = window.top.$("#hfCurrencySystem").val() === "1";
var baseCurrencyName = window.top.$("#hfBaseCurrencyName").val();
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onExpandChanged(function ($expanRow, rowIndex, isExpand) {
        if (isExpand && $expanRow.html().trim() === "") {
            getRizData(rowIndex, $expanRow);
        }
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });

    $('.grid .container-button .dropdown-menu li').click(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex === -1)
            return;
        id = $(this).attr('data-id');
        title = $(this).find('a').text();
        var codeRiz = gridBuilder.getCellValue(rowIndex, "ArticleCode");
        var code_chek = gridBuilder.getCellValue(rowIndex, "ChequeCode");
        var codetarafh = gridBuilder.getCellValue(rowIndex, "CustomerCode");
        var codeMahal = gridBuilder.getCellValue(rowIndex, "BankCashDeskCode");
        var codesanad = gridBuilder.getCellValue(rowIndex, "DocumentCode");
        var currencyCode = gridBuilder.getCellValue(rowIndex, "CurrencyCode");
        var otherMoainCode = gridBuilder.getCellValue(rowIndex, "OtherMoainCode");
        if (id === '0') {
            ajax({
                loading: true,
                url: "TreasuryApi/DeleteChequeLastOperation",
                data: { code: codesanad },
                success: function () {
                    showMsgModal(2, window.top.deletedSuccessfully);
                    loadGrid();
                }
            });
        }
        else {
            switch (id) {
                case '3':
                case '4':
                case '5':
                case '20':
                    openInModal('', 'Treasury/ChequeToBank?coderiz=' + codeRiz + '&codecheque=' + code_chek + '&codeb=' + codeMahal + '&noeopr=' + id + '&type=' + $('#hfSubMain').val() + "&currency=" + currencyCode, title, '500', '400');
                    break;
                case '7':
                case '8':
                case '21':
                    openInModal('', 'Treasury/ChequeToAccount?coderiz=' + codeRiz + '&codecheque=' + code_chek + '&codeh=' + codetarafh + '&noeopr=' + id + '&type=' + $('#hfSubMain').val() + "&mo=" + otherMoainCode, title, '500', '400');
                    break;
                case '6':
                case '10':
                    openInModal('', 'Treasury/ChequeToCashDesk?coderiz=' + codeRiz + '&codecheque=' + code_chek + '&noeopr=' + id + '&type=' + $('#hfSubMain').val(), title, '500', '400');
                    break;
            }
        }
    });

    $(".main-title").click(function () {
        $('.main-title').removeClass("selected-main-title")
        $(this).addClass("selected-main-title");
        $('.selected-sub-title').removeClass("selected-sub-title");
        if ($('.selected-main-title').attr('data-id') == 'true') {
            $("[data-id='1,6']").addClass("selected-sub-title");
            //--------------------------------
            $("#sub-paidCheques").hide();
            $("#sub-receivedCheques").show();
        }
        else {
            $("[data-id='2']").addClass("selected-sub-title");
            //--------------------------------
            $("#sub-receivedCheques").hide();
            $("#sub-paidCheques").show();
        }
        $('#hfMain').val($('.selected-main-title').attr('data-id'));
        $('#hfSubMain').val($('.selected-sub-title').attr('data-id'));
        loadGrid();
    });

    $(".sub-title").click(function () {
        $(this).parent().find('.selected-sub-title').removeClass("selected-sub-title");
        $(this).addClass("selected-sub-title");
        $('#hfSubMain').val($('.selected-sub-title').attr('data-id'));
        loadGrid();
    });
    if (!hasCurrencySystem) {
        gridBuilder.removeColumn("CurrencyName");
        gridBuilder.removeColumn("CurrencyPrice");
    }
    else {
        gridBuilder.setColumnCaption("Price", window.top.documentAmount + "(" + baseCurrencyName + ")");
    }
    loadGrid();
});

function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var IsDarjarianOrNazd = false;
    if ($("#hfSubMain").val() == "1,6" || $("#hfSubMain").val() == "2" || $("#hfSubMain").val() == "4")
        IsDarjarianOrNazd = true;
    ajax({
        url: "TreasuryApi/GetChequeDetailList",
        gridBuilder: gridBuilder,
        data: {
            isChequeIn: $("#hfMain").val(),
            operationType: $("#hfSubMain").val().split(","),
            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);

        }
    });
}


function getRizData(rowIndex, $expanRow) {
    var codeCheque = gridBuilder.getCellValue(rowIndex, "ChequeCode");
    ajax({
        loading: true,
        url: "TreasuryApi/GetChequeTurnover",
        data: { chequeCode: codeCheque, isChequeIn: $('.selected-main-title').attr('data-id') },
        success: function (result) {
            var body =
                '<div style="background-color:#e5e8eb;overflow:hidden;margin-bottom: 12px;">' +
                '<div style="margin-right:30px;margin-left:30px;">' +
                '<label class="float-right" style="margin-right:15px;margin-left:15px;height:30px;line-height:40px;">' + window.top.checkFlowInfo + '</label>' +
                '<div class="col-xs-12" style="border-bottom:3px solid #C0C2C5;border-top:1px solid #ddd;line-height: 35px;">' +
                '<div class="float-right text-ellipsis" style="width:7%">' + window.top.docNumber + '</div>' +
                '<div class="float-right text-ellipsis" style="width:13%">' + window.top.oprType + '</div>' +
                '<div class="float-right text-ellipsis" style="width:10%">' + window.top.Datee + '</div>' +
                '<div class="float-right text-ellipsis" style="width:30%">' + window.top.accountName + '</div>' +
                '<div class="float-right text-ellipsis" style="width:10%">' + window.top.bankName + '</div>' +
                '<div class="float-right text-ellipsis" style="width:10%">' + window.top.cashdeskName + '</div>' +
                '<div class="float-right text-ellipsis" style="width:10%">' + window.top.personName + '</div>' +
                '<div class="float-right text-ellipsis" style="width:10%">' + window.top.accDocCode + '</div>' +
                '</div>';
            for (var ii = 0; ii < result.length; ii++) {
                body += '<div class="col-xs-12" style="padding: 12px 15px;">' +
                    '<div class="float-right" style="line-height:16px;width:7%">' + (result[ii].ReferenceCode || "&nbsp;") + '</div>' +
                    '<div class="float-right" style="line-height:16px;width:13%">' + (result[ii].OperationName || "&nbsp;") + '</div>' +
                    '<div class="float-right" style="line-height:16px;width:10%">' + (result[ii].RegDateLocal || "&nbsp;") + '</div>' +
                    '<div class="float-right" style="line-height:16px;width:30%">' + (result[ii].AccountName || "&nbsp;") + '</div>' +
                    '<div class="float-right" style="line-height:16px;width:10%">' + (result[ii].BankName || "&nbsp;") + '</div>' +
                    '<div class="float-right" style="line-height:16px;width:10%">' + (result[ii].CashDeskName || "&nbsp;") + '</div>' +
                    '<div class="float-right" style="line-height:16px;width:10%">' + (result[ii].CustomerName || "&nbsp;") + '</div>' +
                    '<div class="float-right" style="line-height:16px;width:10%">' + (result[ii].AccDocumentCode || "&nbsp;") + '</div>' +
                    '</div>';
            }

            $expanRow.html(body);
        }
    });

}

function showMenu() {
    $('.grid .container-button .dropdown-menu li[data-id!="0"]').hide();
    var selected = $('.selected-sub-title').attr('data-id').split(',');

    if ($.inArray('1', selected) > -1 || $.inArray('6', selected) > -1) {
        $('.grid .container-button .dropdown-menu [data-id="3"]').show();
        $('.grid .container-button .dropdown-menu [data-id="4"]').show();
        $('.grid .container-button .dropdown-menu [data-id="7"]').show();
        $('.grid .container-button .dropdown-menu [data-id="8"]').show();
        $('.grid .container-button .dropdown-menu [data-id="10"]').show();
    }
    if ($.inArray('4', selected) > -1) {
        $('.grid .container-button .dropdown-menu [data-id="5"]').show();
    }
    if ($.inArray('4', selected) > -1) {
        $('.grid .container-button .dropdown-menu [data-id="6"]').show();
    }
    if ($.inArray('2', selected) > -1) {
        $('.grid .container-button .dropdown-menu [data-id="20"]').show();
        $('.grid .container-button .dropdown-menu [data-id="21"]').show();
    }
}

function refresh() {
    loadGrid();
}
function print() {
    var operationTypeCodes = $("#hfSubMain").val();
    var isChequeIn = $("#hfMain").val().toLowerCase() === "true";
    var isDjNz = operationTypeCodes == "1,6" || operationTypeCodes == "2" || operationTypeCodes == "4";
    var title = window.top.chequeManagerPrint;
    var currentYear = window.top.$("#hfCurrentYear").val();
    var dates = $("#hfDates").val().split(",");
    var src = "Report/Default?GrpName=FormChek&IsDjNz=" + Number(isDjNz) + "&chek_in=" + Number(isChequeIn) + "&CodeNoeAmaliyat=" + operationTypeCodes + "&AzSalMali=" + currentYear + "&AzDate=" + dates[0] + "&TaDate=" + dates[1];
    openInModal('', src, title, "", "", false);
}

