$(document).ajaxError(function () {
    location.reload();
});

function initializeAccountCard() {
    $("#printButton").prop("disabled", false);
    $("#printButton + button").prop("disabled", false);

    initializePartnerTypeahead($('#SubjectId').attr('dataUrl'), $('#SubjectId'), $("#SubjectId"), function (item) {
        $("#SubjectId").val(item.value);
        $.fn.dataTable.reload($dataTable);
        return { selected: true, value: item.id };
    });

    $dataTable = $('#accountcard');

    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = $("#Keywords").val();
                d.DateFrom = reformatDate($("#DateFrom").val());
                d.DateTo = reformatDate($("#DateTo").val());
                d.SelectedSubjects = $("#SelectedSubjects").val();
                d.SelectedAccounts = $("#SelectedAccounts").val();
                d.SubjectId = $("#SubjectId").val();
                d.Department = $("#Department").val();
                d.CostDrv = $("#CostDrv").val();
            }
        },
        columns: [
            { data: "acKeyView", name: "acKey", filtertype: "text" },
            {
                data: "adDate", name: "adDate", filtertype: "text", render: function (data) {
                    if (data === null) {
                        return "";
                    } else {
                        return moment(data).format('DD.MM.YYYY');
                    }
                }
            },
            { data: "acAcct", name: "acAcct", filtertype: "text" },
            { data: "acSubject", name: "acSubject", filtertype: "text" },
            { data: "acShortDocName", name: "acShortDocName", filtertype: "text" },
            {
                data: "adDateDoc", name: "adDateDoc", filtertype: "text", render: function (data) {
                    if (data === null) {
                        return "";
                    } else {
                        return moment(data).format('DD.MM.YYYY');
                    }
                }
            },
            { data: "acLinkDoc", name: "acLinkDoc", filtertype: "text" },
            { data: "anValDebit", name: "anValDebit", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            { data: "anValCredit", name: "anValCredit", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' },
            { data: "acCurrency", name: "acCurrency", filtertype: "text", className: 'dt-body-right' }
        ],
        colReorder: {
            fixedColumnsLeft: 1,
        },
    }).on('xhr.dt', function (e, settings, json, xhr) {
        reloadTotals(json.prop);
    });

    $("select").off("change.tableReload");
    $("select").on("change.tableReload", function () {
        $.fn.dataTable.reload($dataTable);
    });
}

function reloadTotals(data) {
    if (!data) {
        return;
    }
    $("#DebitSum").text(data.DebitSum);
    $("#CreditSum").text(data.CreditSum);
    $("#BalanceSum").text(data.BalanceSum + " " + data.Currency);
}

function printAccountCard(reportCode) {
    var url = $("#AccountCardPrintUrl").val() + "?DateFrom=" + reformatDate($("#DateFrom").val());

    if ($("#DateTo").val())
        url = url + "&DateTo=" + reformatDate($("#DateTo").val());

    var subject = $("#SubjectId").val();
    var accounts = $("#Accounts").val();

    if (accounts)
        url = url + "&Accounts=" + accounts;
    if (subject)
        url = url + "&SubjectId=" + subject;
    if (reportCode)
        url = url + "&ReportType=" + reportCode;

    url = url + "&filterColumn=" + $("#filterColumn").val()
        + "&filterCriteria=" + $("#filterCriteria").val()
        + "&filterValue=" + $("#filterValue").val()
        + "&filterValue2=" + $("#filterValue2").val();

    printWindow = window.open();
    printWindow.document.write(generatePrintingPage());
    printPAAS("", false, false, url, false);
}