{% extends 'atlas/base.html' %}

{% block content %}
    <h1>Categories</h1>
    <p>The platform has numberous different ways to tag and categorise content. These help with organsing cases and series, given the ability to find cases by a particuarly property (such as a finding) or find similar cases.</p><p>These can be browsed below.</p>
    <ul>
        <li><a href="{% url 'atlas:condition_view' %}">Conditions</a></li>
        <li><a href="{% url 'atlas:finding_view' %}">Findings</a></li>
        <li><a href="{% url 'atlas:structure_view' %}">Structures</li></a>
        <li><a href="{% url 'atlas:subspecialty_view' %}">Subspecialties</li></a>
        <li><a href="{% url 'atlas:presentation_view' %}">Presentations</li></a>
        <li><a href="{% url 'atlas:pathological_process_view' %}">Pathological Process</li></a>
        <li><a href="{% url 'atlas:procedure_view' %}">Procedures</a></li>
    </ul>

    <div class="mt-4 mb-3">
            {# HTMX loading indicator styles: hide by default, show when HTMX adds the .htmx-request class #}
        <style>
            #category-search-loading { display: none; }
            #category-search-loading.htmx-request { display: inline-block; }
        </style>

        <p>Alternatively you can use the search function below</p>

        <form method="get" class="d-flex flex-column" action="" id="category-search-form">
            <div class="d-flex align-items-center">
                <input id="category-search-input" type="search" name="q" placeholder="Search all categories (conditions, findings, structures...)" value="{{ query|default:'' }}" class="form-control me-2"
                       hx-get="{% url 'atlas:categories_search_partial' %}"
                       hx-trigger="keyup delay:500ms"
                       hx-target="#category-search-results"
                       hx-swap="innerHTML"
                       hx-include="#category-search-form"
                       hx-indicator="#category-search-loading" />

                    {# Spinner shown while HTMX request is in-flight (selector matched by hx-indicator) #}
                <div id="category-search-loading" role="status" aria-hidden="true" class="ms-2">
                    <span class="spinner-border spinner-border-sm text-primary" aria-hidden="true"></span>
                    <span class="visually-hidden">Loading...</span>
                </div>

                <button class="btn btn-primary ms-2" type="submit">Search</button>
            </div>

                {# Collapsed advanced filters block - defaults to collapsed #}
            <div class="mt-2">
                <button class="btn btn-link p-0" type="button" data-bs-toggle="collapse" data-bs-target="#category-filters" aria-expanded="false" aria-controls="category-filters">
                    Advanced filters ▾
                </button>

                <div class="collapse mt-2" id="category-filters">
                    <div class="card card-body">
                        <div class="d-flex flex-wrap gap-2">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="type-conditions" name="types" value="conditions" checked
                                       hx-get="{% url 'atlas:categories_search_partial' %}"
                                       hx-target="#category-search-results"
                                       hx-swap="innerHTML"
                                       hx-include="#category-search-form"
                                       hx-indicator="#category-search-loading" />
                                <label class="form-check-label" for="type-conditions">Conditions</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="type-findings" name="types" value="findings" checked
                                       hx-get="{% url 'atlas:categories_search_partial' %}"
                                       hx-target="#category-search-results"
                                       hx-swap="innerHTML"
                                       hx-include="#category-search-form"
                                       hx-indicator="#category-search-loading" />
                                <label class="form-check-label" for="type-findings">Findings</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="type-structures" name="types" value="structures" checked
                                       hx-get="{% url 'atlas:categories_search_partial' %}"
                                       hx-target="#category-search-results"
                                       hx-swap="innerHTML"
                                       hx-include="#category-search-form"
                                       hx-indicator="#category-search-loading" />
                                <label class="form-check-label" for="type-structures">Structures</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="type-subspecialties" name="types" value="subspecialties" checked
                                       hx-get="{% url 'atlas:categories_search_partial' %}"
                                       hx-target="#category-search-results"
                                       hx-swap="innerHTML"
                                       hx-include="#category-search-form"
                                       hx-indicator="#category-search-loading" />
                                <label class="form-check-label" for="type-subspecialties">Subspecialties</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="type-presentations" name="types" value="presentations" checked
                                       hx-get="{% url 'atlas:categories_search_partial' %}"
                                       hx-target="#category-search-results"
                                       hx-swap="innerHTML"
                                       hx-include="#category-search-form"
                                       hx-indicator="#category-search-loading" />
                                <label class="form-check-label" for="type-presentations">Presentations</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="type-procedures" name="types" value="procedures" checked
                                       hx-get="{% url 'atlas:categories_search_partial' %}"
                                       hx-target="#category-search-results"
                                       hx-swap="innerHTML"
                                       hx-include="#category-search-form"
                                       hx-indicator="#category-search-loading" />
                                <label class="form-check-label" for="type-procedures">Procedures</label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

                {# Spinner shown while HTMX request is in-flight (selector matched by hx-indicator) #}
            <div id="category-search-loading" role="status" aria-hidden="true" class="ms-2">
                <span class="spinner-border spinner-border-sm text-primary" aria-hidden="true"></span>
                <span class="visually-hidden">Loading...</span>
            </div>

                {% comment %} <button class="btn btn-primary ms-2" type="submit">Search</button> {% endcomment %}
        </form>
    </div>

    <div id="category-search-results">
        {% if results is not None %}
            {% include 'atlas/_categories_search_results.html' %}
        {% endif %}
    </div>

    {% if request.user.is_staff %}
        <p>Manage <a href="{% url 'generic:examination_view' %}">Examinations</a>
        </p>
    {% endif %}
{% endblock %}
