var selectedItems = [];
var isAllSelected = false;
var selectedItemsCount = 0;

function resetSelection() {
    selectedItems = [];
    isAllSelected = false;
    $('.grid-data-selected-items-count').html(0);
}

function selectionChanged(cb) {
    if (!cb)
        return;

    if (cb.checked && selectedItems.indexOf(cb.value) === -1) {
        selectedItems.push(cb.value);
        setSelectedItemsCount(selectedItemsCount + 1);
    } else if (!cb.checked) {
        var index = selectedItems.indexOf(cb.value);
        if (index !== -1) selectedItems.splice(index, 1);
        setSelectedItemsCount(selectedItemsCount - 1);
    }
}

function displayCheckboxes() {
    for (var i = 0; i < selectedItems.length; i++) {
        $("#cb" + selectedItems[i]).prop("checked", true);
    }
}

function setSelectedItemsCount(count) {
    selectedItemsCount = count;
    $('.grid-data-selected-items-count').html(count);
}

function selectAll(cb, url, data) {
    if (cb.checked) {
        $(cb).hide();
        $(cb).parent().append("<i class='fa fa-spinner fa-spin fa-fw loader-icon'></i>");
        $.post(url, { filter: data })
            .done(function (response) {
                if (response !== null) {
                    selectedItems = response;
                    displayCheckboxes();
                    setSelectedItemsCount(response.length);
                }
                $(cb).parent().find(".fa-spinner").remove();
                $(cb).show();
            }).fail(function () {
                $(cb).parent().find(".fa-spinner").remove();
                $(cb).show();
            });
        isAllSelected = true;
    } else {
        $(".selector").prop("checked", false);
        selectedItems = [];
        isAllSelected = false;
        setSelectedItemsCount(0);
    }
}

function fiscalize(filter, $dataTable) {
    $("#confirmBills").append('<i id="confirmBillsSpinner" class="fa fa-spinner fa-spin fa-fw"></i>');
    $.post($("#FiscalizeDocumentsUrl").val(), { invoices: selectedItems, filter: filter })
        .done(function (response) {
            if (response !== null) {
                var count = 0;
                for (let i = 0; i < response.length; i++) {
                    if (response[i].Success)
                        count++;
                }

                var errors = "";
                for (let i = 0; i < response.length; i++) {
                    if (response[i] !== null && !response[i].Success) {
                        errors = errors + response[i].AcKey + ":<br />" + "Errors:" + "<br />" + response[i].Errors + "<br /><br />";
                    }
                }

                $().customModal({
                    destroyOnClose: true,
                    showCloseButton: false,
                    type: "error",
                    text: $("#SuccessfullyFiscalizedDocumentsLocalization").val() + ": " + count + "</br></br>" + errors,
                    buttons: [
                        {
                            icon: 'fa fa-check',
                            text: $("#custom-modal-confirm").val(),
                            closeOnAction: true,
                            type: 'primary'
                        }
                    ]
                });
            }

            selectedItems = [];
            $.fn.dataTable.reload($dataTable);
            $("#confirmBillsSpinner").remove();
        })
        .fail(function (errors) {
            $("#modalFiscalErrors").html(errors);
            $("#fiscalization-dialog").modal("show");

            selectedItems = [];
            $.fn.dataTable.reload($dataTable);
            $("#confirmBillsSpinner").remove();
        });
}