{% 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' id='search-jumbotron'>
        <p class="h4">
            Search {{CITY_NAME}} Legislation
        </p>
        <form method="get" action=".">
            <div class="input-group site-intro-search">

                {% for facet, selections in selected_facets.items %}
                    {% for s in selections %}
                        <input name="selected_facets" type="hidden" class="form-control" value="{{facet}}_exact:{{s}}">
                    {% endfor %}
                {% endfor %}

                {% if 'sort_by=date' in request.get_full_path %}
                    <input name="sort_by" type="hidden" class="form-control" value="date">
                {% endif %}

                {% if 'sort_by=title' in request.get_full_path %}
                    <input name="sort_by" type="hidden" class="form-control" value="title">
                {% endif %}

                {% if 'sort_by=relevance' in request.get_full_path %}
                    <input name="sort_by" type="hidden" class="form-control" value="relevance">
                {% endif %}

                <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">

            {% if selected_facets %}
                <p>
                    <a href="/search/" class="btn btn-sm btn-default">
                    <i class='fa fa-times'></i>
                    Clear all filters
                    </a>
                </p>
            {% endif %}

            <!-- Legislation Status -->
            {% with facet_name='inferred_status' facet_label='Status' item_list=facets.fields.inferred_status selected_list=selected_facets.inferred_status %}
                {% include 'partials/search_filter.html' %}
            {% endwith %}


            <!-- Legislation Type -->
            {% with facet_name='bill_type' facet_label='Legislation Type' item_list=facets.fields.bill_type selected_list=selected_facets.bill_type %}
                {% include 'partials/search_filter.html' %}
            {% endwith %}


            <!-- Topic -->
            {% if facets.fields.topics %}
                {% with facet_name='topics' facet_label='Topic' item_list=facets.fields.topics selected_list=selected_facets.topics %}
                    {% include 'partials/search_filter.html' %}
                {% endwith %}
            {% endif %}


            <!-- Controlling Body -->
            {% with facet_name='controlling_body' facet_label='Controlling Body' item_list=facets.fields.controlling_body selected_list=selected_facets.controlling_body %}
                {% include 'partials/search_filter.html' %}
            {% endwith %}


            <!-- Sponsor -->
            {% with facet_name='sponsorships' facet_label='Sponsor' item_list=facets.fields.sponsorships selected_list=selected_facets.sponsorships %}
                {% include 'partials/search_filter.html' %}
            {% endwith %}


            <!-- Legislative Session -->
            {% if facets.fields.legislative_session %}
                <!-- only show leg sesh filter pane if there is more than one leg sesh to select from -->
                {% if facets.fields.legislative_session|length > 1 %}

                    {% with facet_name='legislative_session' facet_label='Legislative Session' item_list=facets.fields.legislative_session selected_list=selected_facets.legislative_session %}
                        {% include 'partials/search_filter.html' %}
                    {% endwith %}

                {% endif %}
            {% endif %}



            <div class="divider"></div>
        </div>

        <div class="col-sm-8">

            <nav class="nav nav-inline">

                Order by:

                {% with order_name='date' %}
                    {% include 'partials/order_by_filter.html' %}
                {% endwith %}

                {% with order_name='title' %}
                    {% include 'partials/order_by_filter.html' %}
                {% endwith %}

                {% with order_name='relevance' %}
                    {% include 'partials/order_by_filter.html' %}
                {% endwith %}

                {% if 'sort_by' in request.get_full_path %}
                    <a href ="#" class="remove-order-value btn btn-sm btn-primary"
                        {% if 'sort_by=date' in request.get_full_path %}
                            data='sort_by=date'
                        {% elif 'sort_by=title' in request.get_full_path%}
                            data='sort_by=title'
                        {% elif 'sort_by=relevance' in request.get_full_path%}
                            data='sort_by=relevance'
                        {% endif %}
                        ><i class="fa fa-times"></i>
                        Remove
                    </a>
                {% endif %}

            </nav>

            <hr>

            {% if query or selected_facets %}
            <h3>{{ paginator.count }} legislation result{{ paginator.count|pluralize }} {% if query %}for '{{ request.GET.q }}'{% endif %}

                {% if query and selected_facets %}
                    <a href="/search/rss/?q={{request.GET.q}}{% for key, values in selected_facets.items %}{% for value in values %}&selected_facets={{key}}:{{value}}{% endfor %}{% endfor %}" title="RSS feed">
                {% elif selected_facets %}
                    <a href="/search/rss/?{% for key, values in selected_facets.items %}{% for value in values %}&selected_facets={{key}}:{{value}}{% endfor %}{% endfor %}" title="RSS feed">
                {% else %}
                    <a href="/search/rss/?q={{request.GET.q}}" title="RSS feed">
                {% endif %}

                <i class="fa fa-rss-square" aria-hidden="true"></i></a>

                {% if USING_NOTIFICATIONS %}
                    {% if user_subscribed %}
                    <a href="#" id="searchSubscribe" class="removeSubscription" data-toggle="tooltip" data-placement="top" title="Unsubscribe from search">
                        <i class="fa fa-bullhorn fa-fw" aria-hidden="true"></i>
                    </a>
                    {% else %}
                    <a href="#" id="searchSubscribe" class="createSubscription" data-toggle="tooltip" data-placement="top" title="Subscribe to search">
                        <i class="fa fa-bullhorn fa-fw" aria-hidden="true"></i>
                    </a>
                    {% endif %}
                {% endif %}
            </h3>
            <div class='row'>
                <div class='col-sm-8' id='search_message'></div>
            </div>
            {% endif %}



            {% for result in page.object_list %}

            <!-- Legislation result -->

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

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

                    {% if query %}
                        <p class="search-result">
                {{ result.highlighted.text.0|safe  }}
                        </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>

            {% if result.last_action_date %}
                <p class="small text-muted condensed">
                    <i class="fa fa-fw fa-calendar-o"></i> {{result.last_action_date|date:'n/d/Y'}} - {{result.object.current_action.description | remove_action_subj }}
                </p>
            {% elif result.object.get_last_action_date %}
                 <p class="small text-muted condensed">
                    <i class="fa fa-fw fa-calendar-o"></i> {{result.object.get_last_action_date|date:'n/d/Y'}} - {{result.object.current_action.description | remove_action_subj }}
                </p>
            {% endif %}

            {% if result.object.primary_sponsor %}
                <p class="small text-muted condensed">
                    <i class="fa fa-fw fa-user"></i>
                    {{result.object.primary_sponsor.person.name}}
                </p>
            {% endif %}

            <p class="condensed">

                {% if result.object.topics %}
                    <i class="fa fa-fw fa-tag"></i>
                    {% for tag in result.object.topics %}
                        <span class="badge badge-muted pseudo-topic-tag">
                            <a href='/search/?q={{request.GET.q}}&selected_facets=topics_exact:{{ tag }}'>{{tag}}</a>
                        </span>&nbsp;
                    {% endfor %}
                    <br/>
                {% elif result.object.pseudo_topics %}
                    <i class="fa fa-fw fa-tag"></i>
                    {% for tag in result.object.pseudo_topics %}
                        <span class="badge badge-muted pseudo-topic-tag">
                            <a href='/search/?q={{request.GET.q}}&selected_facets=topics_exact:{{ tag | committee_topic_only }}'>{{ tag | committee_topic_only }}</a>
                        </span>&nbsp;
                    {% endfor %}
                    <br/>
                {% endif %}

                <br/>
            </p>


        {% empty %}
            <p>
                This site has data from the current legislative session.
                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_filters }}&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_filters }}&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>
    $(document).ready(function() {
        $("#searchSubscribe").click(function() {
            var bullHorn = this;
            if ($(this).hasClass('createSubscription')) {
                if ('{{ request.user.is_authenticated }}'== 'True') {
                    posturl2 = "/search_check_subscription/"
                    facets2 = JSON.stringify({{selected_facets|safe}});
                    stuff2 = $.post(posturl2, { query: "{{request.GET.q}}", selected_facets: facets2 },
                        function(data, status) {
                        console.log("returned from post with data: ", data, "status:", status);
                    }).error(function() {
                    console.log("Could not check subscription for interest {{request.GET.q}} with {{facets2}}" );
                    }),
                    posturl = "/search_subscribe/" //+ "{{request.GET.q}}"
                    facets =  JSON.stringify({{selected_facets|safe}});
                    $.post(posturl, { query: "{{request.GET.q}}", selected_facets: facets }).then(function() {
                        $(bullHorn).removeClass('createSubscription');
                        $(bullHorn).addClass('removeSubscription');
                        $(bullHorn).attr('title', 'remove alert');
                    }, function (error) {
                        console.error("Error subscribing to interest {{request.GET.q}} with {{selected_facets}}" );
                    })
                }
                else {
                    $('#search_message').html(alertMsg);
                }
            } else if ($(this).hasClass('removeSubscription')) {
                posturl = "/search_unsubscribe/";
                facets = JSON.stringify({{selected_facets|safe}});
                $.post(posturl, { query: "{{request.GET.q}}", selected_facets: facets }).then(function() {
                    $(bullHorn).removeClass('removeSubscription');
                    $(bullHorn).addClass('createSubscription');
                    $(bullHorn).attr('title', 'create alert');
                }, function (error) {
                    console.error("Error unsubscribing to  interest {{request.GET.q}} with {{selected_facets|safe}}" );
                });
            }
            else {
                alert("ID #personSubscribe has neither createSubscription or removeSubscription class");
            }
        }
    )});
    </script>



    <script>
        $(function() {
            // filters are collapsed be default b/c they are long
            // filters w/ class panel-show (i.e. filters that have been selected) are expanded
            $('.panel-show').addClass('in');

            $(".panel-heading").click(function () {
                if ($(this).parent().children().eq(1).hasClass("in")){
                    $(this).find('i').removeClass("fa-minus").addClass("fa-plus");
                } else{
                    $(this).find('i').removeClass("fa-plus").addClass("fa-minus");
                }
            });

            var timestamp = Date.now();
            var existing_q = decodeURIComponent("{{ q_filters }}").replace(/&amp;/g, '&').replace(/\+/g, ' ');

            $(".filter-value").click(function() {
                var addtl_filter = $(this).attr('data');

                if(existing_q){
                    window.location.assign('/search/?'+existing_q+'&selected_facets='+encodeURIComponent(addtl_filter)+"&_="+timestamp);
                }
                else{
                    window.location.assign ('/search/?selected_facets=' + encodeURIComponent(addtl_filter)+"&_="+timestamp);
                };

            });

            $(".remove-filter-value").click(function() {

                var to_remove = encodeURIComponent('selected_facets='+$(this).attr('data'))

                var existing_components = existing_q.split("&")
                var new_components = $.grep(existing_components, function(value) {
                    return encodeURIComponent(value) != to_remove;
                });

                window.location.assign('/search/?' + new_components.join('&')+"&_="+timestamp);
            });

            $(".remove-order-value").click(function() {
                var to_remove = $(this).attr('data');
                var existing_components = existing_q.split("&")

                for(var i = existing_components.length -1; i >= 0 ; i--){
                    var el = existing_components[i]
                    if(el.includes('ascending') || el.includes(to_remove) || el.includes('descending')) {
                        existing_components.splice(i, 1);
                    }
                }

                window.location.assign('/search/?' + existing_components.join('&')+"&_="+timestamp);
            });

        });
    </script>
{% endblock %}
