{% load bloomerp %}

{% if list_level_links %}
    {% for item in list_level_links %}
    <li class="dropdown-item fw-bold">{{ item.model_name | title }}</li>
        {% for link in item.links %}
            <a 
                class="dropdown-item pointer"
                hx-get="{% url link.url %}"
                hx-push-url="true"
                hx-target="#main-content"
                hx-swap="innerHTML"
                href="#"
                >
                {{ link }}</a>
        {% endfor %}
    <hr class="dropdown-divider">
    {% endfor %}
{% elif app_level_links %}
    {% for link in app_level_links %}
        <a 
            class="dropdown-item pointer"
            hx-get="{% url link.url %}"
            hx-push-url="true"
            hx-target="#main-content"
            hx-swap="innerHTML"
            href="#"
            >
            {{ link }}</a>
        <hr class="dropdown-divider">

    {% endfor %}
{% else %}
    {% for result in results %}
        <li class="dropdown-item fw-bold">{{ result|get_dict_value:'model_name'|title}}</li>

        {% for object in result|get_dict_value:'objects' %}
            <a 
            class="dropdown-item pointer" 
            hx-get="{{ object | detail_view_url }}" 
            hx-push-url="true" 
            hx-target="#main-content" 
            hx-swap="innerHTML"
            href="#"
            >{{object}}</a>    
        {% endfor %}

        <hr class="dropdown-divider">
    {% endfor %}
{% endif %}

<script>
    document.addEventListener('htmx:afterSwap', function(event) {
        if (event.detail.target.id != 'searchResult') {
            // Empty the search input
            document.getElementById('searchInput').value = '';
        }
    });
</script>
