{% load seeker %}

<form action="" method="get">
    <div class="form-group">
        <label class="sr-only" for="q">Keywords</label>
        <input type="text" class="form-control" placeholder="Keywords" name="q" id="q" value="{{ keywords }}" autofocus="autofocus" />
    </div>
    {% for facet, values in results.facet_values %}
        {% if values %}
            <h3>{{ facet.label|capfirst }}</h3>
            <div class="choices">
                {% for value in values %}
                    <div class="checkbox">
                        {% facet_checkbox facet value filters %}
                    </div>
                {% endfor %}
                {% if facet.ranges %}
                    <div class="popover-range">
                        <h4>Custom Range</h4>
                        <input type="number" min="0" step="1" class="form-control input-sm range-min range-pair" placeholder="Min..." />
                        to
                        <input type="number" min="0" step="1" class="form-control input-sm range-max range-pair" placeholder="Max..." />
                        <input type="hidden" name="{{ facet.field }}" value="" class="range-hidden" disabled />
                        <span class="range-separator hidden">{{ facet.separator }}</span>
                    </div>
                {% endif %}
            </div>
        {% endif %}
    {% endfor %}
    <div class="form-actions">
        <button type="submit" class="btn btn-primary">Filter</button>
        <a href="{{ request_path }}?q=" class="btn btn-default">Reset</a>
        <a class="btn btn-default" data-toggle="modal" data-target="#display-fields"><i class="fa fa-list"></i></a>
        {% if can_save %}
            {% if querystring or saved_searches %}
                <div class="btn-group">
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                        <i class="fa fa-floppy-o"></i>
                        <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" role="menu">
                        {% if querystring %}
                            <li><a href="" data-toggle="modal" data-target="#save-dialog">Save This Search</a></li>
                        {% endif %}
                        {% for s in saved_searches %}
                            {% if forloop.first and querystring %}
                                <li class="divider"></li>
                            {% endif %}
                            <li><a href="{{ s.get_absolute_url }}">{{ s }}</a></li>
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}
        {% endif %}
    </div>
    <div class="modal fade" id="display-fields">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h3 class="modal-title">Show Fields</h3>
                </div>
                <div class="modal-body">
                    {% for name, label in selectable_fields.iteritems %}
                        <div class="checkbox">
                            <label><input type="checkbox" name="d" value="{{ name }}"{% if name in display_fields %} checked="checked"{% endif %} /> {{ label }}</label>
                        </div>
                    {% endfor %}
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Show Selected Fields</button>
                </div>
            </div>
        </div>
    </div>

    {# Include a hidden input for the current sort, if one exists. Allows us to maintain the sort across changes to facets, display fields, etc. #}
    {% if sort %}<input type="hidden" name="sort" value="{{ sort }}" />{% endif %}
</form>

<form action="" method="post">
    {% csrf_token %}
    <input type="hidden" name="querystring" value="{{ querystring }}" />
    <div class="modal fade" id="save-dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h3 class="modal-title">Save This Search</h3>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="search-name">Name</label>
                        <input type="text" name="name" id="search-name" class="form-control" />
                    </div>
                    <div class="form-group">
                        <label class="checkbox-inline">
                            <input type="checkbox" name="default" value="1" />
                            Make this your default search for this page
                        </label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button type="submit" name="_save" class="btn btn-primary">Save Search</button>
                </div>
            </div>
        </div>
    </div>
</form>
