{% extends "base.html" %}
{% block title %}Search{% endblock %}
{% block content %}
<nav class="breadcrumb" aria-label="breadcrumbs">
    <ul>
        {% if is_all_clusters: %}
        <li><a href="/clusters">all</a></li>
        {% else: %}
        {% if cluster: %}
        <li><a href="/clusters/{{ cluster }}">{{ cluster }}</a></li>
        {% if is_all_namespaces: %}
        <li><a href="/clusters/{{ cluster }}/namespaces">all</a></li>
        {% elif namespace: %}
        <li><a href="/clusters/{{ cluster }}/namespaces/{{ namespace }}">{{ namespace }}</a></li>
        {% endif %}
        {% endif %}
        {% endif %}
        <li class="is-active"><a href="#">search</a></li>
    </ul>
</nav>

<h1 class="title">Search</h1>

<form action="/search" method="get" class="tools-form is-active">
    <input type="hidden" name="cluster" value="{{ cluster or '' }}">
    <input type="hidden" name="namespace" value="{{ namespace or '' }}">
    <div class="field is-horizontal">
        <div class="field-label is-normal">
            <label class="label">Search Text</label>
        </div>
        <div class="field-body">
            <div class="field has-addons">
                <p class="control is-expanded has-icons-left">
                <input class="input" type="text" placeholder="Search query text" name="q" value="{{ search_query }}">
                <span class="icon is-small is-left">
                    <i class="fas fa-search"></i>
                </span>
                </p>
                <div class="control">
                    <button class="button is-primary" type="submit">Search!</button>
                </div>
            </div>
        </div>
    </div>
    <div class="field is-horizontal">
        <div class="field-label is-normal">
            <label class="label">Resource Types</label>
        </div>
        <div class="field-body">
            <div class="field">
                <p class="control checkboxes" id="type-checkboxes">
                {% for _type, kind in searchable_resource_types.items()|sort: %}
                <label class="checkbox"><input type="checkbox" name="type" value="{{ _type }}" {{ 'checked' if _type in resource_types }}> {{ kind }}</label>
                {% endfor %}
                <a class="button is-light unselect" title="unselect all types" data-target="type-checkboxes">
                    <span class="icon">
                        <i class="fas fa-times"></i>
                    </span>
                    <span>unselect all</span>
                </a>
                </p>
            </div>
        </div>
    </div>
</form>


{% for result in search_results: %}

<div class="search-result">
    <h3 class="title is-6"><a href="{{ result.link }}">{{ result.title }} ({{ result.kind }})</a></h3>
    <p><a href="{{ result.link }}">{{ result.link }}</a></p>
    {% if result.created or result.matches: %}
    <p>
    {% if result.created: %}
    Created: {{ result.created.replace('T', ' ').replace('Z', '') }}
    {% endif %}
    {% for pre, highlight, post in result.matches: %}
    <span class="match">{{ pre }}<em>{{ highlight }}</em>{{ post }}</span>
    {% endfor %}
    </p>
    {% endif %}
    <p>
    {% for key, val in result.labels.items()|sort: %}
    <a href="{{ rel_url.update_query(q=key+'='+val) }}"><span class="tag is-link">{{ key }}: {{ val }}</span></a>
    {% endfor %}
    </p>
</div>

{% else: %}

{% if search_query: %}

<div class="content">
    <p>No results found for "{{ search_query }}".</p>
</div>

{% endif %}

{% endfor %}

{% if search_query: %}
<div class="content">

    {% if not is_all_namespaces: %}
    <p><a href="{{ rel_url.update_query(namespace='') }}">Repeat search across all namespaces</a></p>
    {% endif %}

    <p class="has-text-grey">{{ search_results|length }} result{{ 's' if search_results|length != 1 }} found. Searched {{ resource_types|length }} resource types in {{ search_clusters|length }} cluster{{ 's' if search_clusters|length > 1 }} in {{ '%.3f'|format(search_duration) }} seconds.</p>

</div>
{% endif %}


{% for cluster_name, errors in search_errors.items(): %}
<article class="message is-danger">
    <div class="message-header">
        <p>Error{{ 's' if errors|length > 1 }} for cluster {{ cluster_name }}</p>
    </div>
    <div class="message-body">
        {% for error in errors: %}
        <p>Failed to search {{ error.resource_type }}: {{ error.exception }}</p>
        {% endfor %}
    </div>
</article>
{% endfor %}

{% endblock %}
