(function ($) {
    "use strict";

    $(function () {
        init_ajax_properties_search_by_customer_id();
    });

    $("body").on("change", ".f_client_id #clientid", function () {
        var val = $(this).val();
        var projectAjax = $('select[name="property_id"]');
        var clonedProjectsAjaxSearchSelect = projectAjax.html("").clone();
        var projectsWrapper = $(".properties-wrapper");

        projectAjax.selectpicker("destroy").remove();
        projectAjax = clonedProjectsAjaxSearchSelect;
        $("#property_ajax_search_wrapper").append(clonedProjectsAjaxSearchSelect);
        init_ajax_properties_search_by_customer_id();

        if (!val) {
            projectsWrapper.addClass("hide");
            return false;
        }

        var currentInvoiceID = $("body").find('input[name="merge_current_invoice"]').val();
        currentInvoiceID = typeof currentInvoiceID === "undefined" ? "" : currentInvoiceID;

        requestGetJSON(
            "propertybase/propertybase/client_change_data/" + val + "/" + currentInvoiceID
        ).done(function (response) {
            response.customer_has_properties
                ? projectsWrapper.removeClass("hide")
                : projectsWrapper.addClass("hide");
        });
    });

    function init_ajax_properties_search(selector) {
        selector = typeof selector === "undefined" ? "#property_id.ajax-search" : selector;
        init_ajax_search("properties", selector);
    }

    $("body").on("change", ".properties #property_id", function () {
        var val = $(this).val();

        requestGetJSON(
            "propertybase/propertybase/properties_change_data/" + val
        ).done(function (response) {
            _init_property_tasks_billable_select(response.billable_tasks);
        });
    });

    function init_ajax_properties_search_by_customer_id(selector) {
        selector = typeof selector === "undefined" ? "#property_id.ajax-search" : selector;
        init_ajax_search("properties", selector, {
            customer_id: function () {
                return $("#clientid").val();
            },
        });
    }

    function _init_property_tasks_billable_select(tasks, property_id) {
        var billable_tasks_area = $("#task_select");

        if (billable_tasks_area.length > 0) {
            billable_tasks_area.find("option").filter(function () {
                return this.value || $.trim(this.value).length > 0 || $.trim(this.text).length > 0;
            }).remove();

            $.each(tasks, function (i, obj) {
                var option_data = " ";
                if (obj.started_timers === true) {
                    option_data +=
                        'disabled class="text-danger" data-subtext="' +
                        app.lang.invoice_task_billable_timers_found +
                        '"';
                } else if (obj.started_timers === false && obj.rel_type !== "properties") {
                    option_data += 'data-subtext="' + obj.rel_name + '"';
                }
                billable_tasks_area.append(
                    '<option value="' + obj.id + '"' + option_data + ">" + obj.name + "</option>"
                );
            });

            var tasks_help_wrapper = $(".input-group-addon-bill-tasks-help");
            tasks_help_wrapper.find(".popover-invoker").popover("destroy");
            tasks_help_wrapper.empty();

            var help_tooltip = "";

            if (!empty(property_id)) {
                help_tooltip =
                    app.lang["showing_billable_tasks_from_project"] +
                    " " +
                    $("#property_id option:selected").text().trim();
            } else {
                help_tooltip = app.lang["invoice_task_item_project_tasks_not_included"];
            }

            tasks_help_wrapper.html(
                '<span class="pointer popover-invoker" data-container=".form-group-select-task_select" data-trigger="click" data-placement="top" data-toggle="popover" data-content="' +
                help_tooltip +
                '"><i class="fa-regular fa-circle-question"></i></span>'
            );

            delay(function () {
                if (
                    (tasks_help_wrapper.attr("info-shown-count") < 3 ||
                        typeof tasks_help_wrapper.attr("info-shown-count") === "undefined") &&
                    $(".projects-wrapper").is(":visible") &&
                    tasks.length > 0
                ) {
                    tasks_help_wrapper.attr(
                        "info-shown-count",
                        typeof tasks_help_wrapper.attr("info-shown-count") === "undefined"
                            ? 1
                            : parseInt(tasks_help_wrapper.attr("info-shown-count"), 10) + 1
                    );
                    tasks_help_wrapper.find(".popover-invoker").click();
                }
            }, 3500);
        }

        billable_tasks_area.selectpicker("refresh");
    }

})(jQuery);
