var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    $(".go-step").on("click", function () {
        var level = $(this).attr("data-target");
        if (level === "step2") {
            var exceptionText = checkValidation('');
            if (exceptionText === '') {
                loadGrid();
            }
            else {
                showMsgModal(0, exceptionText);
            }
        }
        else if (level === "step3") {
            var childDocument = $("#iframe-InvoiceSave")[0].contentDocument;
            $(childDocument).find("#btnSave").click();
        }
        else {
            showNextStep(level);
        }
    });
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        loading: true,
        url: "SaleApi/GetNegativeStoreInventory",
        data: {
            storeCode: $("#cboAnbar").lookupBuilder().getSelectedValue(), date: $("#txtDate").val()
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else {
                if (data.Artikles.length > 0) {
                    $("#divNegativeKala").show();
                    $("#iframe-InvoiceSave").hide();
                    showNextStep("step2");
                    gridBuilder.loadData(data.Artikles);
                }
                else {
                    checkProductRemained();
                }
            }
                
        }
    });
}
function showNextStep(step) {
    $(".div-step").hide();
    $("#" + step).show();
}
function closeAndRefresh() {
    closeCurrentTab();
    refreshParent("loadGrid('gridDocument');");
}
function checkNegativeKala() {
    loadGrid();
}
function firstEndSanadLastLevel(codeSanad, sanadType, sum) {
    $("#lblEndSanadNumber").text(codeSanad);
    $("#lblEndSanadDate").text($("#txtDate").val());
    $("#lblEndSanadType").text(sanadType);
    $("#lblAnbar").text($("#cboAnbar").lookupBuilder().getSelectedText());
    $("#lblSum").text(sum);

    $("#divResultNoMande").hide();
    $("#divResult").show();
    showNextStep("step3");
}
function openModalInOutSave() {
    var selectedCustomer = $("#cboCustomer").lookupBuilder().getSelectedValue().split("|");
    openInModal('', "Store/InOutSave?firstEndState=negativeKala&type=in&codeAnbar=" + $("#cboAnbar").lookupBuilder().getSelectedValue() + "&codeTarafH=" + selectedCustomer[0] + "&tarafH=" + $("#cboCustomer").lookupBuilder().getSelectedText() + "&date=" + $("#txtDate").val() + "&time=" + $("#txtTime").val() + "&arzTarafH=" + selectedCustomer[1] + "&decimalcountTarafH=" + selectedCustomer[2], window.top.invoiceDeductionDocument, 1000, '');
}
async function checkProductRemained() {
    await ajax({
        url: "SaleApi/GetOpeningClosingStoreDocument",
        data: {
            storeCode: $("#cboAnbar").lookupBuilder().getSelectedValue(),
            date: $("#txtDate").val(),
            sourceSalMali: window.top.$("#hfCurrentYear").val(),
            state: "end",
            time: $("#txtTime").val()
        },
        success: function (result) {
            if (result.Artikles.length !== 0) {
                var selectedCustomer = $("#cboCustomer").lookupBuilder().getSelectedValue().split("|");
                var url = "../Store/InOutSave?firstEndState=end&type=out&codeAnbar=" + $("#cboAnbar").lookupBuilder().getSelectedValue() + "&codeTarafH=" + selectedCustomer[0] + "&tarafH=" + $("#cboCustomer").lookupBuilder().getSelectedText() + "&date=" + $("#txtDate").val() + "&time=" + $("#txtTime").val() + "&arzTarafH=" + selectedCustomer[1] + "&decimalcountTarafH=" + selectedCustomer[2];
                redirectUrl(url, "iframe-InvoiceSave");
                $("#divNegativeKala").hide();
                $("#iframe-InvoiceSave").show();
                showNextStep("step2");
            }
            else {
                $("#lblAnbarNoMande").text($("#cboAnbar").lookupBuilder().getSelectedText());
                $("#lblDateNoMande").text($("#txtDate").val());
                $("#divResultNoMande").show();
                $("#divResult").hide();
                showNextStep("step3");
            }
        }
    });
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    if (!id)
        return;
    var url = "";
    var data = null;
    switch (id) {
        case "cboCustomer":
            url = "GeneralApi/GetCustomerList";
            data = { type: -1, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboAnbar":
            url = "SaleApi/GetStoreList";
            data = { search: search, pageNo: 1, selectedCode: code };
            break;
    }
    await ajax({
        url: url,
        data: data,
        success: function (result) {
            if (id === "cboCustomer") {
                $.each(result, function () {
                    this.Code = this.Code + "|" + this.CurrencyCode + "|" + this.CurrencyDecimalCount;
                });
            }
            $combo.lookupBuilder().bindData(result);
        }
    });
}