<!--
 Copyright 2018 by Embrapa.  All rights reserved.

 This code is part of the machado distribution and governed by its
 license. Please see the LICENSE.txt and README.md files that should
 have been included as part of this package for licensing information.
-->

{% load machado_extras %}


<form action="{% url 'feature_search' %}" method="get" >
<input type="hidden" name="q" value="{{ query }}">

<h3>Filters</h3>


{% if selected_facets or query %}
<div class="card mb-2">
    <div class="card-header">
      <b>Selected filters</b>
    </div>
	<div class="card-body">
	{% if query %}
	  <a title="Remove query" href="?{% remove_query %}">search:{{ query }} <i class="fa fa-times" aria-hidden="true"></i></a><br />
	{% endif %}
	{% for facet_field in selected_facets %}
	  <a title="Remove filter" href="?{% remove_facet_field facet_field %}">
	  {% if facet_field == "orthology:0" %}
	  	no orthology <i class="fa fa-times" aria-hidden="true"></i>
	  {% elif facet_field == "orthology:1" %}
	  	orthology <i class="fa fa-times" aria-hidden="true"></i>
	  {% elif facet_field == "coexpression:0" %}
	  	no coexpression groups <i class="fa fa-times" aria-hidden="true"></i>
	  {% elif facet_field == "coexpression:1" %}
	  	coexpression groups <i class="fa fa-times" aria-hidden="true"></i>
	  {% elif facet_field == "orthologs_coexpression:false" %}
	  	no coexpression in orthologs <i class="fa fa-times" aria-hidden="true"></i>
	  {% elif facet_field == "orthologs_coexpression:true" %}
	  	coexpression in orthologs <i class="fa fa-times" aria-hidden="true"></i>
	  {% else %}
        {{ facet_field }} <i class="fa fa-times" aria-hidden="true"></i>
	  {% endif %}
	  </a><br />
	{%endfor%}
	</div>
</div>
{%endif%}

{% for facet in facet_fields_order %}
{% with facets.fields|get_item:facet as exists %}
{% if exists %}

<div class="card mb-2">
    <div class="card-header">
    {% with facet_fields_desc|get_item:facet as facet_description %}
    <div class="row">
        <div class="col">
            <b style="cursor: help;" title="{{ facet_description }}">
            {% if facet == "so_term" %}
            Feature type
            {% elif facet == "orthologs_coexpression" %}
            Orthologous coexpression
            {% elif facet == "orthologous_group" %}
            Orthologous group
            {% elif facet == "coexpression_group" %}
            Coexpression group
            {% else %}
            {{ facet|capfirst }}
            {% endif %}
            </b>
			{% if facets.fields|get_count:facet > 4 %}
			<small>({% if facets.fields|get_count:facet == 100 %}Truncated to {% endif %}{{ facets.fields|get_count:facet }})</small>
			{% endif %}
        </div>
        <div class="col text-right">
			<div class="btn-group" role="group">
				<button class="btn btn-sm btn-light" type="submit" title="{{ facet_description }}"><small>apply</small></button>
				{% if facet in selected_facets_fields %}
				<a class="btn btn-sm btn-light" type="link" title="Remove filter" href="?{% remove_facet facet %}"><small>remove</small></a>
				{%endif%}
			</div>
        </div>
    </div>
    {% endwith %}
    </div>
	<div class="card-body">
	{% for item in facets.fields|get_item:facet|dictsort:0 %}
		{% with facet_item=facet|add:":"|add:item.0 %}

		<!-- hide items if there are more than 4 -->
		{% if forloop.counter <= 4 or facet_item in selected_facets %}
			<div class="container">
		{% else %}
			<div class="container collapse" id="machado-facet-item-{{ facet }}">
		{% endif %}

		{% if facet == "organism" %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if facet_item in selected_facets %}checked{% endif %}>
			<i>{{ item.0|capfirst }}</i>
		{% elif facet == "orthology" %}
			{% if item.0 == 0 %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if "orthology:0" in selected_facets %}checked{% endif %}>
			no orthology
			{% else %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if "orthology:1" in selected_facets %}checked{% endif %}>
			orthology
			{% endif %}
		{% elif facet == "coexpression" %}
			{% if item.0 == 0 %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if "coexpression:0" in selected_facets %}checked{% endif %}>
			no coexpression groups
			{% else %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if "coexpression:1" in selected_facets %}checked{% endif %}>
			coexpression groups
			{% endif %}
		{% elif facet == "orthologs_coexpression" %}
			{% if item.0 == 'false' %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if "orthologs_coexpression:false" in selected_facets %}checked{% endif %}>
			no coexpression
			{% else %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if "orthologs_coexpression:true" in selected_facets %}checked{% endif %}>
			coexpression
			{% endif %}
		{% elif facet == "orthologs_biomaterial" %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if facet_item in selected_facets %}checked{% endif %}>
			{{ item.0|capfirst }}
		{% else %}
			<input type="checkbox" name="selected_facets" id="selected_facets" value="{{ facet }}:{{ item.0 }}" {% if facet_item in selected_facets %}checked{% endif %}>
			{{ item.0 }}
		{% endif %}
		{% endwith %}
        <small>({{ item.1 }})</small><br />
		</div>
	{% endfor %}
	</div>
	{% if facets.fields|get_count:facet > 4 %}
		<div class="card-footer text-center py-0">
			<a role="button" class="collapsed" data-toggle="collapse" href="#machado-facet-item-{{ facet }}"><i class="fas fa-caret-down"></i></a>
		</div>
	{% endif %}
</div>
{% endif %}
{% endwith %}
{% endfor %}
</form>
