var level = 0;

$(document).ready(function () {
    $(".grid").each(function (index, grid) {
        var builder = $(grid).gridBuilder();
        if (index === 0) {
            builder.setCaption(window.top.storeProductList);
            builder.onDblClick(function () {
                var rowIndex = $("#grdSanadList").gridBuilder().getActiveRowIndex();
                if (rowIndex < 0) {
                    return;
                }
                $("#level0").hide();
                $("#level1").show();
                $("#divStore").show();
                loadGrid("grdRizSanad");
            });
            builder.onExportClick(function () {
                loadGrid("grdSanadList", { exportExcel: true });
            });
        }
        else if (index === 1) {
            builder.setCaption(window.top.treasuryDocumentRizList);
            builder.onDblClick(function () {
                var rowIndex = $("#grdRizSanad").gridBuilder().getActiveRowIndex();
                if (rowIndex < 0) {
                    return;
                }
                $("#level1").hide();
                $("#level2").show();
                $("#divStore").hide();
                loadGrid("grdRizSanad2");
            });
            builder.onExportClick(function () {
                loadGrid("grdRizSanad", { exportExcel: true });
            });
        }
        else if (index === 2) {
            builder.setCaption(window.top.treasuryDocumentRizList);
            builder.onExportClick(function () {
                loadGrid("grdRizSanad2", { exportExcel: true });
            });
        }
    });
    loadGrid("grdSanadList");
    $("#cboStore").lookupBuilder().setSelectedValue("-1");
    imgToSvg();
});

function loadGrid(gridId, objectParameter) {
    var gridBuilder = $("#" + gridId).gridBuilder();
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;

    var code = -1;
    var storeCode = -1;
    var codePart = -1;
    if (gridId == "grdRizSanad") {
        code = $("#grdSanadList").gridBuilder().getActiveRowCellValue("ProductCode");
        storeCode = $("#cboStore").lookupBuilder().getSelectedValue();
        level = 1;
    }
    else if (gridId == "grdRizSanad2") {
        code = $("#grdRizSanad").gridBuilder().getActiveRowCellValue("ProductCode");
        storeCode = $("#grdRizSanad").gridBuilder().getActiveRowCellValue("StoreCode");
        level = 2;
    }
    ajax({
        loading: true,
        url: "SaleApi/GetRialiBrowsing",
        data: {
            level: $("#" + gridId).attr("data-level"),
            toDate: $("#txtTadate").val(),
            storeCode: storeCode,
            productCode: code,
            codePart: codePart || -1,
            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 getEventLookupCombo($combo) {
    ajax({
        url: "SaleApi/GetStoreList",
        async: false,
        data: { creditType: 0, selectedCode: "", filter: "", sort: "", pageNo: -1 },
        success: function (result) {
            result.splice(0, 0, { Code: -1, Name: window.top.all });
            $combo.lookupBuilder().bindData(result);
        }
    });
}

function levelDown(gridId) {
    $("#" + gridId).gridBuilder().dblClick();
}

$("#txtTadate").change(function () {
    refresh();
});

function levelUp() {
    if (level === 1) {
        $("#level1").hide();
        $("#level0").show();
        $("#divStore").hide();
        $("#cboStore").lookupBuilder().setSelectedValue("-1");
    }
    if (level === 2) {
        $("#level2").hide();
        $("#level1").show();
        $("#divStore").show();
    }
    level--;
}

function refresh() {
    if (level == 0)
        loadGrid("grdSanadList");
    else if (level == 1)
        loadGrid("grdRizSanad");
    else if (level == 2)
        loadGrid("grdRizSanad2");
}
