var grdSaleBuilder;
var grdStoreBuilder;
var grdAccountingBuilder;
var hasSystemAccess;
var hasAccounting = false;
var hasSale = false;
var hasStore = false;

$(document).ready(function () {
    initial();
    $(".menu-item").on("click", function () {
        $(".menu-item").removeClass("selected-menu-item");
        $(".div-content").hide();
        $(this).addClass("selected-menu-item");
        $("#" + $(this).attr("data-target")).show();
    });
});
async function initial() {
    var type = "Sale";
    await hasSystem(380);
    await hasSystem(392);
    await hasSystem(391);

    if (!hasSale) {
        $("#divBtnSale").hide();
        $("#divSale").hide();
        type = "Store";
    }

    if (!hasStore) {
        $("#divBtnAnbar").hide();
        $("#divStore").hide();
        if (type === "Store")
            type = "Accounting";
    }
    else {
        if (type != "Store")
            $("#divStore").hide();
    }

    if (!hasAccounting) {
        $("#divBtnAccounting").hide();
        $("#divAccounting").hide();
    }
    else {
        if (type != "Accounting")
            $("#divAccounting").hide();
    }

    if (type === "Accounting")
        $("#divBtnAccounting").addClass("selected-menu-item");
    else if (type === "Sale")
        $("#divBtnSale").addClass("selected-menu-item");
    else if (type === "Store")
        $("#divBtnAnbar").addClass("selected-menu-item");

    if ($("#divSale").length === 1)
        grdSaleBuilder = $("#grdSale").gridBuilder();
    if ($("#divStore").length === 1)
        grdStoreBuilder = $("#grdStore").gridBuilder();
    if ($("#divAccounting").length === 1)
        grdAccountingBuilder = $("#grdAccounting").gridBuilder();
    loadGrid("grd" + type);

    $("#divMain").show();
    closeLoading();
}
function loadGrid(gridId, objectParameter) {
    var systemName = $("#" + gridId).attr("data-id");
    var gridBuilder = eval(gridId + "Builder");
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    if (!gridBuilder)
        return;
    ajax({
        gridBuilder: gridBuilder,
        url: "GeneralApi/GetRecycleBinList",
        data: {
            systemName: systemName,
            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 gridAction(systemName, actionName) {
    var gridBuilder = eval("grd" + systemName + "Builder");
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    if (actionName === "recover") {
        var src = "/";
        switch (systemName) {
            case "Sale":
                src += "Sale/FactorSave?rcode=";
                break;
            case "Store":
                src += "Store/InOutSave?rcode=";
                break;
            case "Accounting":
                src += "Accounting/InvoiceSave?rcode=";
                break;
        }
        src += code + "&recover=1";
        openInTab(src, window.top.documentRecovery, '', false);
    }
    else if (actionName === "delete") {
        ajax({
            loading: true,
            url: "GeneralApi/DeleteRecycleBin",
            data: { code: code },
            success: function (retValue) {
                showMsgModal(2, retValue.FinalMessage.join("<br />"));
                loadGrid("grd" + systemName);
            }
        });
    }
}
async function hasSystem(code) {
    await ajax({
        url: "GeneralApi/CheckSystemAccess",
        data: { code: code },
        success: function (result) {
            if (code === 380)
                hasAccounting = result;
            else if (code === 392)
                hasSale = result;
            else if (code === 391)
                hasStore = result;
        }
    });
}

