{% extends 'base/layout.html' %}
{% load helpers %}
{% load form_helpers %}
{% load render_table from django_tables2 %}

{% block title %}Search{% endblock %}

{% block content %}
    {% if request.GET.q %}
        {% if results %}
            <div class="row">
                <div class="col col-md-9">
                    {% for obj_type in results %}
                        <div class="card">
                            <h5 class="card-header" id="{{ obj_type.name|lower }}">{{ obj_type.name|bettertitle }}</h5>
                            <div class="card-body table-responsive">
                                {% render_table obj_type.table 'inc/table.html' %}
                            </div>
                            <div class="card-footer text-end">
                                <a href="{{ obj_type.url }}" class="btn btn-sm btn-primary my-1">
                                    <i class="mdi mdi-arrow-right-bold" aria-hidden="true"></i>
                                    {% if obj_type.table.page.has_next %}
                                        See All {{ obj_type.table.page.paginator.count }} Results
                                    {% else %}
                                        Refine Search
                                    {% endif %}
                                </a>    
                            </div>
                        </div>
                    {% endfor %}
                </div>
                <div class="col col-md-3">
                    <div class="card">
                        <h5 class="card-header">
                            Search Results
                        </h5>
                        <div class="card-body">
                            <div class="list-group list-group-flush">
                                {% for obj_type in results %}
                                    <a href="#{{ obj_type.name|lower }}" class="list-group-item">
                                        <div class="float-end">
                                          {% badge obj_type.table.page.paginator.count %}
                                        </div>
                                        {{ obj_type.name|bettertitle }}
                                    </a>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        {% else %}
            <h3 class="text-muted text-center">No results found</h3>
        {% endif %}
    {% else %}
        <div class="row">
            <div class="col col-12 col-lg-6 offset-lg-3">
                <form action="{% url 'search' %}" method="get" class="form form-horizontal">
                    <div class="card">
                        <h5 class="card-header">
                            Search
                        </h5>
                        <div class="card-body">
                            {% render_form form %}
                        </div>
                        <div class="card-footer text-end">
                            <button type="submit" class="btn btn-primary">
                                <span class="mdi mdi-magnify" aria-hidden="true"></span> Search
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    {% endif %}
{% endblock content %}
