{% extends "isearch/base.html" %}

{% block title %}Проверка поиска{% endblock %}

{% block viewer_active %}active{% endblock %}

{% block container %}
<script type="text/javascript" src="https://yandex.st/js/jquery/1.3.2/_jquery.js"></script>
<div class="row-fluid">
    <form>
        <table>
            {{ form.as_table() }}
        </table>
        <input class="btn primary" type="submit" value="Отправить">
    </form>

<h2>Результаты</h2>
    {% if abovemeta_url %}<div><a href="{{ abovemeta_url }}">Запрос в abovemeta</a></div>{% endif %}
    {% if result %}
        {% if result.errors %}
        <h4>Ошибки</h4>
        <div>
            {% for error in result.errors %}
                <strong>{{ error.code }}</strong>: {{ error.text }}
            {% endfor %}
        </div>
        {% endif %}
        {% if result.errata %}
            <div>
                <h4>errata</h4>
                <pre>{{ result.errata|to_json }}</pre>
            </div>
        {% endif %}
        <div class="row-fluid">
            <h4 class='heading' id='search_results' style='cursor:pointer'><i class="icon-plus"></i>search_results</h4>
            <p><strong>Нашлось документов: </strong>{{ result.meta.count }}</p>
            {% if result.meta.count > 0 %}
                <p><strong>Переколдованный запрос: </strong>{{ result.results.backend.text }}</p>
                <p><a href="{{ result.results.backend.url }}">Запрос в бекенд</a></p>
                <p>Страница {{ result.meta.page }} из {{ result.meta.pages }} </p>
                <div class="content" id="data_search_results">
                    <div class="span9">
                        <ol start={{ result.meta.per_page * result.meta.page + 1 }}>
                        {% if result.results.docs %}
                            {% for doc in result.results.docs %}
                                <li>
                                {% with doc=doc, name='search_results', form=form %}
                                    {% include "isearch/search/include/doc.html" %}
                                {% endwith %}
                                </li>
                            {% endfor %}
                        {% else %}
                            {% for group in result.results.groups %}
                                <li>
                                    <p><b>{{ group.category.attr }} ({{ group.category.name }})</b>: {{ group.category.value }}</p>
                                    {% for doc in group.docs %}
                                        {% with doc=doc, name='search_results', form=form %}
                                            {% include "isearch/search/include/doc.html" %}
                                        {% endwith %}
                                    {% endfor %}
                                </li>
                            {% endfor %}
                        {% endif %}
                        </ol>
                        {% if paginator and paginator.num_pages > 1 %}
                            <div class="pagination pagination-left">
                                <ul>
                                    {% if paginator.previous_num %}
                                    <li><a href={{ url("isearch-viewer-search") }}?{{ paginator.previous_url }}#{{ name }}>...</a></li>
                                    {% endif %}
                                    {% for p in paginator.pages %}
                                    <li {% if p == paginator.page %}class="active"{% endif %}><a href={{ url("isearch-viewer-search")}}?{{ paginator.build_query(p)}}#search_results>{{ p }}</a></li>
                                    {% endfor %}
                                    {% if paginator.next_num %}
                                    <li><a href={{ url("isearch-viewer-search") }}?{{ paginator.next_url }}#{{ name }}>...</a></li>
                                    {% endif %}
                                </ul>
                            </div>
                        {% endif %}
                    </div>
                    {% if result.facets %}
                    <div class="span3 well sidebar-nav">
                        <ul class="nav nav-list">
                        {% for facet in result.facets %}
                        <li class="nav-header">{{ facet.name }}</li>
                            {% for item in facet.groups %}
                            <li><a href='{{ url("isearch-viewer-search")}}?{{ build_facet_query(facet.value, item.value) }}#search_results'>{{ item.name | e }} [{{ item.count }}]</a></li>
                            {% endfor %}
                        {% endfor %}
                        <li class="nav-header">Finally</li>
                        <li>
                            <a href='{{ url("isearch-viewer-search")}}?{{ build_facet_query() }}#search_results'>
                                Убрать фильтры
                            </a>
                        </li>
                     </ul>
                    </div>
                    {% endif %}
                </div>
            {% endif %}
        </div>
        <br>
        {% for wizard in result.wizards %}
            {% set name = 'wizard_%s' % wizard.position %}
            <div class="row-fluid">
                <h4 class='heading' id='{{ name }}' style='cursor:pointer'><i class="icon-plus"></i> {{name}} [{{ wizard.type }}]</h4>
                <p><strong>Нашлось документов: </strong>{{ wizard.count }}</p>
                <p><strong>Позиция: </strong>{{ wizard.position }}</p>
                <p><strong>Переколдованный запрос: </strong>{{ wizard.backend.text }}</p>
                <p><a href="{{ wizard.backend.url }}">Запрос в бекенд</a></p>
                <div class="content" id="data_{{ name }}">
                    <div class="span9">
                        <ol>
                        {% for doc in wizard.docs %}
                            <li>
                            {% with doc=doc, name='search_results', form=form %}
                                {% include "isearch/search/include/doc.html" %}
                            {% endwith %}
                            </li>
                        {% endfor %}
                        </ol>
                    </div>
                </div>
            </div>
            <br>
        {% endfor %}
    {% else %}
        {% if response_error %}
            <div><strong>STATUS_CODE: {{ response_error.status_code }}</strong></div>
            <div>{{ response_error.content }}</div>
        {% endif %}
    {% endif %}
</div>

<script type="text/javascript">
$(document).ready(function() {
  $(".content:not(#data_search_results)").hide();
  $(".doc_content").hide();
  $('#search_results i').attr('class', 'icon-minus');

  function expand(source, icon_open, icon_close) {
    var $elem = $("#data_" + source.attr('id'));
        $elem.toggle();
        if ( $elem.is( ':visible' ) == true ) {
            source.find('i').attr('class', icon_open);
        } else {
            source.find('i').attr('class', icon_close);
        }
    }

    $(".heading").click(function() {
        expand($(this), 'icon-minus', 'icon-plus');
    });

    $(".doc_heading").click(function() {
        expand($(this), 'icon-chevron-up', 'icon-chevron-down');
    });

    $("a.unbubble").click(function(event) {
        event.stopPropagation();
    });
});
</script>

{% endblock %}
