{% extends 'base_with_margins.html' %}
{% load extras %}
{% load highlight %}
{% block title %}
    {% if request.GET.q %}
        Search results for '{{ request.GET.q }}'
    {% else %}
        Search Legislation
    {% endif %}
{% endblock %}
{% block content %}
    <br/>
    <div class='jumbotron'>
        <p class="h4">
            Search {{CITY_NAME}} legislation
        </p>
        <form method="get" action=".">
            <div class="input-group site-intro-search">
                <input name="q" type="text" class="input-lg form-control" placeholder="{{SEARCH_PLACEHOLDER_TEXT}}" {% if request.GET.q %}value="{{request.GET.q}}"{% endif %}>
                <div class='input-group-btn'>
                    <button type="submit" class="btn btn-lg btn-primary">
                        <i class='fa fa-fw fa-search'></i>
                    </button>
                </div>
            </div>
        </form>
    </div>

    <div class="row">
        <div class="col-sm-4">
            <div class="panel panel-default">
                <div class="panel-heading" data-toggle="collapse" data-target="#search-filters-mobile">
                    <h3 class="panel-title">
                        <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                        Filter results <i class="fa fa-fw fa-plus pull-right"></i></a>
                    </h3>
                </div>
                <div class="panel-body collapse" id="search-filters-mobile">

                    <p><strong>Legislation status</strong></p>
                    <ul class="search-facet-list">
                        {% for name, count in facets.fields.inferred_status %}
                            {% if count %}
                                <li class="small">
                                    <a href="/search/?q={{query}}&selected_facets=inferred_status_exact:{{name}}" title="{{ name|title }}">
                                        {{ name|committee_topic_only|title|short_title }}
                                    </a>
                                    <span class="badge badge-muted pull-right">{{ count }}</span>
                                </li>
                            {% endif %}
                        {% endfor %}
                    </ul>

                    <p><strong>Legislation type</strong></p>
                    <ul class="search-facet-list">
                        {% for name, count in facets.fields.bill_type %}
                            {% if count %}
                                <li class="small">
                                    <a href="/search/?q={{query}}&selected_facets=bill_type_exact:{{name}}" title="{{ name|title }}">
                                        {{ name|committee_topic_only|title|short_title }}
                                    </a>
                                    <span class="badge badge-muted pull-right">{{ count }}</span>
                                </li>
                            {% endif %}
                        {% endfor %}
                    </ul>

                    <p><strong>Controlling Body</strong></p>
                    <ul class="search-facet-list">
                        {% for name, count in facets.fields.controlling_body %}
                            {% if count %}
                                <li class="small">
                                    <a href="/search/?q={{query}}&selected_facets=controlling_body_exact:{{name}}" title="{{ name|title }}">
                                        {{ name|committee_topic_only|title|short_title }}
                                    </a>

                                    <span class="badge badge-muted pull-right">{{ count }}</span>
                                </li>
                            {% endif %}
                        {% endfor %}
                    </ul>

                    <p><strong>Sponsor</strong></p>
                    <ul class="search-facet-list">
                        {% for name, count in facets.fields.sponsorships %}
                            {% if count %}
                                <li class="small">
                                    <a href="/search/?q={{query}}&selected_facets=sponsorships_exact:{{name}}" title="{{ name|title }}">
                                        {{ name|committee_topic_only|title|short_title }}
                                    </a>
                                </li>
                            {% endif %}
                        {% endfor %}
                    </ul>

                </div>
            </div>
        </div>

        <div class="col-sm-8">
            {% if query %}
                <h3>{{ paginator.count }} legislation result{{ paginator.count|pluralize }} for '{{ request.GET.q }}'</h3>
            {% endif %}

        {% for result in page.object_list %}

            <!-- Legislation result -->

            <p>
                <a class="small" href="/legislation/{{ result.object.slug }}">{{ result.object.friendly_name }}</a>
                {{ legislation.inferred_status | inferred_status_label | safe }}
            </p>


            <div class="row">
                <div class="col-xs-11">

                    {% if query %}
                        <p>
                            {% highlight result.text with query css_class "bg-info" %}
                        </p>
                    {% else %}
                        <p>
                            {{result.object.listing_description | short_blurb}}
                        </p>
                    {% endif %}

                </div>
                <div class='col-xs-1 no-pad-mobile'>
                    <div>
                        <a class='btn-bill-detail' href='/legislation/{{ result.object.slug }}'>
                            <i class="fa fa-fw fa-chevron-right"></i>
                        </a>
                    </div>
                </div>
            </div>
            <p>
                <span class="small text-muted">
                    <i class="fa fa-fw fa-calendar-o"></i> {{result.object.last_action_date|date:'n/d/Y'}} - {{result.object.current_action.description | remove_action_subj }}
                </span><br/>

                {% if result.object.committees_involved %}
                    <i class="fa fa-fw fa-tag"></i>
                    {% for c in result.object.committees_involved %}
                        <span class="badge badge-muted pseudo-topic-tag">
                            {{ c | committee_topic_only }}
                        </span>&nbsp;
                    {% endfor %}
                    <br/>
                {% endif %}
                <br/>
            </p>


        {% empty %}
            <p>This site has data from the current legislative session, which
            goes back to 2014. If you're searching for something older, you can
            browse the
                <a href="{{ LEGISTAR_URL }}" target="_blank">
                    {{ CITY_COUNCIL_NAME }} Legistar site
                </a>.
            </p>
            <p>We'd be happy to help you find what you're looking for -
                <a href="mailto:info@councilmatic.org">contact us</a>!
            </p>

        {% endfor %}
        </div>
    </div>
    {% if page.has_previous or page.has_next %}
        <div class="row">
            <div class="col-md-8 col-md-offset-4">
                <nav>
                    <ul class="pagination">
                        {% if page.has_previous %}
                            <li>
                                <a href="?q={{ query }}&amp;page={{ page.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">&laquo; Previous</span></a>
                            </li>
                        {% else %}
                            <li class="disabled">
                                <a href="#" aria-label="Previous"><span aria-hidden="true">&laquo; Previous</span></a>
                            </li>
                        {% endif %}

                        {% if page.has_next %}
                            <li>
                                <a href="?q={{ query }}&amp;page={{ page.next_page_number }}" aria-label="Next"><span aria-hidden="true">Next &raquo;</span></a>
                            </li>
                        {% else %}
                            <li class="disabled">
                                <a href="#" aria-label="Next"><span aria-hidden="true">Next &raquo;</span></a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            </div>
        </div>
    {% endif %}
{% endblock %}

{% block extra_js %}
    <script>
        $(function() {
            if ($(window).width() >= 768) {
                $('#search-filters-mobile').collapse('show');
            }
        });
    </script>
{% endblock %}
