(function($) {
    "use strict";

    $(document).ready(function () {
        propertybase_init_expenses_total();
    });

    // Expenses additional server params
    var Expenses_ServerParams = {};
    var property_id = $('input[name="property_id"]').val();
    $.each($("._hidden_inputs._filters input"), function () {
        Expenses_ServerParams[$(this).attr("name")] =
            '[name="' + $(this).attr("name") + '"]';
    });

    var _table_api = initDataTable(
        ".table-property-expenses",
        admin_url + "propertybase/propertybase/expenses/" + property_id,
        "undefined",
        "undefined",
        Expenses_ServerParams,
        [5, "desc"]
    );

    if (_table_api) {
        _table_api.column(0).visible(false, false).columns.adjust();
    }

    function propertybase_init_expenses_total() {
        if ($("#propertybase_expenses_total").length === 0) {
            return;
        }
        var currency = $("body").find('select[name="expenses_total_currency"]').val();
        var _years = $("body")
            .find('select[name="expenses_total_years"]')
            .selectpicker("val");
        var years = [];
        $.each(_years, function (i, _y) {
            if (_y !== "") {
                years.push(_y);
            }
        });

        var customer_id = "";
        var _customer_id = $('.customer_profile input[name="userid"]').val();
        if (typeof customer_id != "undefined") {
            customer_id = _customer_id;
        }

        var property_id = "";
        var property_id = $('input[name="property_id"]').val();
        if (typeof property_id != "undefined") {
            property_id = property_id;
        }

        $.post(admin_url + "propertybase/propertybase/get_expenses_total", {
            currency: currency,
            init_total: true,
            years: years,
            customer_id: customer_id,
            propertyid: property_id,
        }).done(function (response) {
            $("#propertybase_expenses_total").html(response);
        });
    }

})(jQuery);
