var state = getQuery("state").toString() === "true";
var documentData;
$(document).ready(function () {
    if (!state) {
        $("#spanToDate").hide();
        $("#txtDateReturn").hide();
    }
    else {
        $("#spanDateControlAz").hide();
        $("#txtDate").hide();
    }
    getConfirmStoreInfo();
});
function saveDepot() {
    var data;
    var date = $("#" + (state ? "txtDateReturn" : "txtDate")).val();
    if (state)
        data = { fromDate: date };
    else
        data = { toDate: date };
    if (!state)
        getStoreDocumentForConfirm(date);
    else
        ajax({
            loading: true,
            url: "SaleApi/ConfirmReturnStoreDocument",
            data: data,
            success: function (result) {
                saveSuccess();
            }
        });
}
function saveSuccess() {
    showMsgModal(2, window.top.savedSuccessfully);
    closeTopModal();
}
function getConfirmStoreInfo() {
    ajax({
        loading: true,
        url: "SaleApi/GetConfirmStoreInfo",
        data: { state: state },
        success: function (result) {
            $("#spanCountAllAsnad").text(result.AllDocumentCount);
            $("#spanCountControl").text(result.ControledDocumentCount);
            $("#spanCountAbtal").text(result.InvalidDocumentCount);
            $("#spanCountNotControl").text(result.NoControledDocumentCount);
            $("#spanMahdodeAsnadAz").text(result.FromDateLocal);
            $("#spanMahdodeAsnadTa").text(result.ToDateLocal);
            $("#spanControlShodeAz").text(result.ControledFromDateLocal);
            $("#spanControlShodeTa").text(result.ControledToDateLocal);
            $("#spanDateControlAz").text(result.ControledToDateLocal || result.FromDateLocal);
            if (state) {
                $("#txtDate").val(result.ToDateLocal);
                $("#spanToDate").text(result.ControledToDateLocal || result.ToDateLocal);
            }
        }
    });
}
async function getStoreDocumentForConfirm(toDate) {
    await ajax({
        loading: true,
        url: "SaleApi/GetStoreDocumentForConfirm",
        data: { toDate: toDate },
        success: function (result) {
            documentData = result;
        }
    });
    confirmStoreDocument();
}
async function confirmStoreDocument() {
    if (documentData.length > 0) {
        var j = Math.ceil(documentData.length / 100);
        showLoading();
        var div = $("<div style='position:absolute;width:300px;top:50%;left:50%;margin-top:-150px;margin-left:-150px;padding:10px 20px;background-color:#fff;z-index:9999999999;border:1px dotted #aaa' class='wait-counter'></div>");
        $(window.top.document).find("body").append(div);
        for (var i = 0; i < j; i++) {
            var toIndex = (100 * (i + 1));
            if (toIndex > documentData.length)
                toIndex = documentData.length;
            var list = documentData.slice(100 * i, toIndex);
            var desc = window.top.ongoingDocuments + " " + ((100 * i) + 1) + "-" + toIndex + " " + window.top._Az + " " + + documentData.length;
            div.html("<p style='text-align:center'>" + desc + "</p>");
            await confirmStoreDocumentBySerial(list);
        }
        div.remove();
        closeLoading();
        saveSuccess();
    }
}
async function confirmStoreDocumentBySerial(data) {
    await ajax({
        url: "SaleApi/ConfirmStoreDocumentBySerial",
        data: { data: data },
        success: function (result) {

        },
        error: function (err) {
            $(window.top.document).find(".wait-counter").remove();
            closeLoading();
            if (window.top.loadCompletePage !== undefined)
                window.top.loadCompletePage = true;
            var message, description = "";
            if (err.responseJSON && err.responseJSON.Message) {
                message = err.responseJSON.Message;
                description = err.responseJSON.Description;
            } else
                message = err.responseText;
            if (!message || message.includes("network-related"))
                showConnectionError();
            else
                showMsgModal(0, message, description);
        }
    });
}
