(function($) {
    "use strict";

    var table_invoices = $("table.table-invoices");

    if (table_invoices.length > 0) {
        var Sales_table_ServerParams = {};
        var Sales_table_Filter = $("._hidden_inputs._filters input");

        $.each(Sales_table_Filter, function() {
            Sales_table_ServerParams[$(this).attr("name")] =
                '[name="' + $(this).attr("name") + '"]';
        });

        if (table_invoices.length) {
            var property_id = $('input[name="property_id"]').val();
            // Invoices tables
            initDataTable(
                table_invoices,
                admin_url +
                "propertybase/propertybase/invoice_table/" + property_id +
                ($("body").hasClass("recurring") ? "?recurring=1" : ""),
                "undefined",
                "undefined",
                Sales_table_ServerParams,
                !$("body").hasClass("recurring")
                    ? [
                        [3, "desc"],
                        [0, "desc"],
                    ]
                    : [table_invoices.find("th.next-recurring-date").index(), "asc"]
            );
        }
    }

    function init_property_invoices_total(manual) {
        if ($("#invoices_total").length === 0) {
            return;
        }
        var _inv_total_inline = $(".invoices-total-inline");
        var _inv_total_href_manual = $(".invoices-total");

        if (
            $("body").hasClass("invoices-total-manual") &&
            typeof manual == "undefined" &&
            !_inv_total_href_manual.hasClass("initialized")
        ) {
            return;
        }

        if (
            _inv_total_inline.length > 0 &&
            _inv_total_href_manual.hasClass("initialized")
        ) {
            // On the next request won't be inline in case of currency change
            // Used on dashboard
            _inv_total_inline.removeClass("invoices-total-inline");
            return;
        }

        _inv_total_href_manual.addClass("initialized");
        var _years = $("body")
            .find('select[name="invoices_total_years"]')
            .selectpicker("val");
        var years = [];
        $.each(_years, function(i, _y) {
            if (_y !== "") {
                years.push(_y);
            }
        });

        var currency = $("body").find('select[name="total_currency"]').val();
        var data = {
            currency: currency,
            years: years,
            init_total: true,
        };

        var property_id = $('input[name="property_id"]').val();
        var customer_id = $('.customer_profile input[name="userid"]').val();
        if (typeof property_id != "undefined") {
            data.property_id = property_id;
        } else if (typeof customer_id != "undefined") {
            data.customer_id = customer_id;
        }
        $.post(admin_url + "propertybase/propertybase/get_property_invoices_total", data).done(function(
            response
        ) {
            $("#invoices_total").html(response);
        });
    }

})(jQuery);
