{% extends is_mobile ? "::base_m.html.twig" : "::base.html.twig" %}

{% block title %}{% if is_advanced %}Расширенный поиск по сайту{% else %}Поиск по сайту{% endif %}{% endblock %}

{% block css %}
    {% if is_admin %}
    <link href="{{ asset('magnific-popup/magnific-popup.css') }}" rel="stylesheet" type="text/css" />
    {% endif %}
{% endblock %}
{% block js %}
    {% if is_admin %}
    <script src="{{ asset('magnific-popup/jquery.magnific-popup.min.js') }}" type="text/javascript"></script>
    {% endif %}
{% endblock %}

{% block body %}
<div id="search-results-form">
    <form method="Post" action="{{ action }}">
        <table border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td align="left" valign="middle" class="search-results-form-query-title">
                    Что искать:
                </td>
            </tr>
            <tr>    
                <td align="left" valign="middle" class="search-results-form-query">
                    <input type="text" name="text" value="{{ query }}" class="search-results-form-query" />
                </td>
            </tr>            
            <tr>
                <td align="left" valign="middle" class="search-results-form-query-type">
                    <input type="radio" name="query_type" value="or" {% if query_type == 'or' %}checked="checked"{% endif %} class="search-results-form-query-type" id="search-results-form-query-type-or" />
                    <label class="search-results-form-query-type-label" for="search-results-form-query-type-or">любое слово</label>
                    <input type="radio" name="query_type" value="and" {% if query_type == 'and' %}checked="checked"{% endif %} class="search-results-form-query-type" id="search-results-form-query-type-and" />
                    <label class="search-results-form-query-type-label" for="search-results-form-query-type-and">все слова</label>
                    <input type="radio" name="query_type" value="equal" {% if query_type == 'equal' %}checked="checked"{% endif %} class="search-results-form-query-type" id="search-results-form-query-type-equal" />
                    <label class="search-results-form-query-type-label" for="search-results-form-query-type-equal">точная фраза</label>
                </td>
            </tr>
            <tr>     
                <td align="left" valign="middle" class="search-results-form-submit">
                    <input type="submit" name="submit" value="Найти >>" class="search-results-form-submit" />
                </td>
            </tr>
        </table>
        {% if is_advanced %}
        <div class="search-results-form-options">
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td align="left" valign="top" class="search-results-form-advanced-label">                    
                        Результатов на странице:
                    </td>
                    <td align="left" valign="top" class="search-results-form-advanced-input">                    
                        <select name="results_per_page">
                            {% for item in results_per_page_available %}
                            <option value="{{ item }}"{% if item == results_per_page %} selected="selected"{% endif %}>{{ item }}</option>
                            {% endfor %}
                        </select>
                    </td>
                </tr>
            </table>
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td align="left" valign="top" class="search-results-form-advanced-check">                    
                        <input type="checkbox" value="yes" {% if show_links %}checked="checked"{% endif %} name="show_links" id="search-results-form-show-links" />
                    </td>
                    <td align="left" valign="top" class="search-results-form-advanced-label">    
                        <label class="search-results-form-show-links-label" for="search-results-form-show-links">Показывать связи</label>
                    </td>
                </tr>
            </table>
        </div>
        <table border="0" cellspacing="0" cellpadding="0" class="search-results-form-advanced">
            <tr>
                <td align="left" valign="top" class="search-results-form-advanced-label">                    
                    Область поиска:
                </td>
                <td align="left" valign="top" class="search-results-form-advanced-input">                    
                    <select name="scope">
                        <option value="any"{% if filter_scope == 'any' %} selected="selected"{% endif %}>везде</option>
                        <option value="articles"{% if filter_scope == 'articles' %} selected="selected"{% endif %}>статьи</option>
                        <option value="comments"{% if filter_scope == 'comments' %} selected="selected"{% endif %}>комментарии</option>
                        <option value="forum"{% if filter_scope == 'forum' %} selected="selected"{% endif %}>форум</option>
                    </select>
                </td>
            </tr>
            <tr>    
                <td align="left" valign="top" class="search-results-form-advanced-label">                    
                    Раздел:
                </td>
                <td align="left" valign="top" class="search-results-form-advanced-input">                    
                    <select name="category">
                        <option value="any">-- любой --</option>
                        {% for item in categories %}
                        <option value="{{ item.id }}"{% if item.selected %} selected="selected"{% endif %}>{{ item.title }}</option>
                        {% endfor %}
                    </select>
                </td>                 
            </tr>
            <tr>
                <td align="left" valign="top" class="search-results-form-advanced-label">                    
                    Тег:
                </td>
                <td align="left" valign="top" class="search-results-form-advanced-input">                    
                    <select name="tag">
                        <option value="any">-- любой --</option>
                        {% for item in tags %}
                        <option value="{{ item.id }}"{% if item.selected %} selected="selected"{% endif %}>{{ item.title }}</option>
                        {% endfor %}
                    </select>
                </td>
            </tr>
            <tr>    
                <td align="left" valign="top" class="search-results-form-advanced-label">                    
                    Персоналия:
                </td>
                <td align="left" valign="top" class="search-results-form-advanced-input">                    
                    <select name="person">
                        <option value="any">-- любая --</option>
                        {% for item in persons %}
                        <option value="{{ item.id }}"{% if item.selected %} selected="selected"{% endif %}>{{ item.title }}</option>
                        {% endfor %}
                    </select>
                </td>
            </tr>
            <tr>
                <td align="left" valign="top" class="search-results-form-advanced-submit">
                    <input type="submit" name="submit" value="Найти >>" class="search-results-form-submit" />
                </td>
            </tr>
        </table>
        {% endif %}       
    </form>
</div>

{% if not waiting %}
    {% if not is_error %}
        {% if data %}
        <div class="search-results">
        
        {% if is_admin %}
        {#        
        <form action="{{ admin_action }}" method="Post">
        #}
        <div class="search-results-admin-submit">
            <div class="search-results-admin-submit-check">
                <a href="javascript:check_all()">Отметить все</a> / <a href="javascript:uncheck_all()">Снять отметку со всех</a>
            </div>
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>            
                    <td class="search-results-admin-submit-label" align="left" valign="middle">
                        Действия над отмеченными:
                    </td>
                    <td class="search-results-admin-submit-buttons" align="left" valign="middle">
                        <input class="search-results-admin-submit-bind-tag" type="submit" name="bind_tag" value="Привязать к тегу" />
                        <input class="search-results-admin-submit-bind-person" type="submit" name="bind_person" value="Привязать к персоналии" />
                        <input class="search-results-admin-submit-bind-user" type="submit" name="bind_user" value="Назначить владельца" />
                    </td>
                </tr>
            </table>    
        </div>
        {% endif %}

        {% for row in data %}
        <div class="search-result">
        <div class="search-result-title"><a href="{{ row.link }}">{{ row.title|raw }}</a></div>
        <div class="search-result-body">
        {{ row.body|raw }}
        </div>
        {% if row.links|length > 0 %}
        <div class="search-result-links">
        {% for link in row.links %}
        <a href="{{ link.link }}">{{ link.title }}</a>{% if not loop.last %}, {% endif %}
        {% endfor %}
        </div>
        {% endif %}
        {% if is_admin %}
        {% if row.article_id %}
        <div class="search-result-admin">
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td align="left" velign="middle" class="search-results-admin-check">
                        <input id="check{{ row.id }}" class="search-result-admin-check" type="checkbox" name="check_article[]" value="{{ row.id }}" />
                    </td>
                    <td align="left" velign="middle" class="search-results-admin-check-label">
                        <label for="check{{ row.id }}">- отметить</label>
                    </td>
                </tr>
            </table>
        </div>
        {% endif %}
        {% endif %} 
        </div>
        {% endfor %}
        </div>

        {{ pager|raw }}
        
        {% if is_admin %}
        <div class="search-results-admin-submit">
            <div class="search-results-admin-submit-check">
                <a href="javascript:check_all()">Отметить все</a> / <a href="javascript:uncheck_all()">Снять отметку со всех</a>
            </div>
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>            
                    <td class="search-results-admin-submit-label" align="left" valign="middle">
                        Действия над отмеченными:
                    </td>
                    <td class="search-results-admin-submit-buttons" align="left" valign="middle">
                        <input class="search-results-admin-submit-bind-tag" type="submit" name="bind_tag" value="Привязать к тегу" />
                        <input class="search-results-admin-submit-bind-person" type="submit" name="bind_person" value="Привязать к персоналии" />
                        <input class="search-results-admin-submit-bind-user" type="submit" name="bind_user" value="Назначить владельца" />
                    </td>
                </tr>
            </table>    
        </div>
        {#
        </form>
        #}
        {% endif %} 

        {% else %}
        <p>Нет результатов, удовлетворяющим вашему запросу.</p>

        {% endif %}
    {% endif %}
{% endif %}

{% if is_admin %}
<div id="bind-person-dialog" style="display: none" class="popup">
    <p style="margin-bottom: 5px;">
        <select name="person">
            <option value="any">-- выберите персоналию --</option>
            {% for item in persons %}
            <option value="{{ item.id }}">{{ item.title }}</option>
            {% endfor %}
        </select>
    </p>
    <p>
        <div id="bind-person-dialog-ajax" style="display: none; float: right;"><img src="{{ asset('images/ajax.gif') }}" width="16" height="16" /></div>
        <input type="button" name="submit" id="bind-person-dialog-submit" style="float: left; margin-right: 10px;" value="Привязать к персоналии" />
        {# <a href="#" id="bind-person-dialog-submit" style="margin-right: 10px;">Привязать к персоналии</a> #}
        <a href="#" id="bind-person-dialog-cancel" style="margin-right: 10px; margin-top: 2px;">отмена</a>
    </p>
</div>
<div id="bind-tag-dialog" style="display: none" class="popup">
    <p style="margin-bottom: 5px;">
        <select name="tag">
            <option value="any">-- выберите тег --</option>
            {% for item in tags %}
            <option value="{{ item.id }}">{{ item.title }}</option>
            {% endfor %}
        </select>
    </p>
    <p>
        <div id="bind-tag-dialog-ajax" style="display: none; float: right;"><img src="{{ asset('images/ajax.gif') }}" width="16" height="16" /></div>
        <input type="button" name="submit" id="bind-tag-dialog-submit" style="float: left; margin-right: 10px;" value="Привязать к тегу" />
        {# <a href="#" id="bind-tag-dialog-submit" style="margin-right: 10px;">Привязать к тегу</a> #}
        <a href="#" id="bind-tag-dialog-cancel" style="margin-right: 10px; margin-top: 2px;">отмена</a>
    </p>
</div>
<div id="bind-user-dialog" style="display: none;" class="popup">
    <p>
        <div id="bind-user-dialog-ajax" style="display: none; float: right; margin-top: 20px"><img src="{{ asset('images/ajax.gif') }}" width="16" height="16" /></div>
            <div class="form-row" style="margin-bottom: 10px;"><label for="form_name" class="required">Имя пользователя:</label><input style="margin-left: 10px;" type="text" id="bind-user-dialog-name" name="user_name" value="" /></div>
        <input type="button" name="submit" id="bind-user-dialog-submit" style="float: left; margin-right: 10px;" value="Найти пользователя" action="{{ search_users_action }}" />
        <a href="#" id="bind-user-dialog-cancel" style="margin-right: 10px; margin-top: 2px;">отмена</a>
    </p>
    <p style="display: none; clear: both; max-height: 200px; overflow-y: hiddent" id="bind-user-dialog-results">
    </p>
</div>
<script type="text/javascript">
function check_all () {
    $('.search-result-admin-check').each(function(){
        $(this).prop('checked', 'checked')
    });    
}
function uncheck_all () {
    $('.search-result-admin-check').each(function(){
        $(this).prop('checked', '')
    });    
}
$('.search-results-admin-submit-bind-person').magnificPopup({
    items: [
      {
        src: '#bind-person-dialog',
        type: 'inline'
      }
    ],
    callbacks: {
        open: function() {
          $('#bind-person-dialog').css('display', '');
        },
      },
    gallery: {
      enabled: true
    },
    type: 'image' // this is default type
});
$('.search-results-admin-submit-bind-tag').magnificPopup({
    items: [
      {
        src: '#bind-tag-dialog',
        type: 'inline'
      }
    ],
    callbacks: {
        open: function() {
          $('#bind-tag-dialog').css('display', '');
        },
      },
    gallery: {
      enabled: true
    },
    type: 'image' // this is default type
});
$('.search-results-admin-submit-bind-user').magnificPopup({
    items: [
      {
        src: '#bind-user-dialog',
        type: 'inline'
      }
    ],
    callbacks: {
        open: function() {
          $('#bind-user-dialog').css('display', '');
        },
      },
    gallery: {
      enabled: true
    },
    type: 'image' // this is default type
});
$('#bind-person-dialog-submit').click(function(){
    ids_str = '';
    $('#bind-person-dialog-submit').css('disabled', 'disabled');
    $('.search-results-admin-check input').each(function(){
        checked = $(this).prop('checked');
        val = $(this).val();
        if (checked) {
            if (ids_str != '') {
                ids_str += ',';
            }
            ids_str += val;
            //ids.push(val);
        }
    });
    person_val = $('#bind-person-dialog select').val();    
    if (person_val != 'any') {
        if (ids_str != '') {
            $('#bind-person-dialog-ajax').css('display', '');
            post = {'ids':ids_str,'person_id':person_val};
            $.ajax({
                url: '{{ person_bind_action }}',
                type: "POST",
                data: post,
                dataType: 'json'
            }).done(function( response ) {
                $('#bind-person-dialog-ajax').css('display', 'none');
                $.magnificPopup.close();
                {% if show_links %}
                location.reload();
                {% endif %}
                //alert( "Result: " + response['debug'] );
            });
        } else {
            alert('Укажите хотя бы один результат поиска!');
        }
    } else {
        alert('Выберите персоналию!');
    }
    //alert(person_val);
    //alert(ids_str);
    //$.magnificPopup.close();
});
$('#bind-user-dialog-submit').click(function(){
    //alert('#bind-user-dialog-submit');
    query = $('#bind-user-dialog-name').val();    
    $('#bind-user-dialog-results').css('display', 'none');
    $('#bind-user-dialog-ajax').css('display', '');
    post = {'query':query};
    $.ajax({
        url: '{{ search_users_action }}',
        type: "POST",
        data: post,
        dataType: 'json'
    }).done(function( response ) {
        //alert(response['status']);
        $('#bind-user-dialog-ajax').css('display', 'none');
        $('#bind-user-dialog-results').css('display', '');
        $('#bind-user-dialog-results').html();
        ids_str = '';
        $('#bind-person-dialog-submit').css('disabled', 'disabled');
        $('.search-results-admin-check input').each(function(){
            checked = $(this).prop('checked');
            val = $(this).val();
            if (checked) {
                if (ids_str != '') {
                    ids_str += ',';
                }
                ids_str += val;
                //ids.push(val);
            }
        });
        html = '';
        for (i in response['data']) {
            if (html != '') {
                html += '<br />';
            }
            html += response['data'][i]['display'];
            html += ' - ';
            //link = '#';
            //link = '{{ user_bind_action }}+'?ids='+ids_str+'&user_id='+response['data'][i]['id'];
            //html += '<a href="{{ user_bind_action }}?ids='+ids_str+'&user_id='+response['data'][i]['id']+'">назначить владельцем</a>';
            html += '<a href="javaScript:bindUser(\''+ids_str+'\', \''+response['data'][i]['id']+'\')">назначить владельцем</a>';
        }
        html = '<h3>Найденные результаты:</h3>'+html;
        $('#bind-user-dialog-results').html(html);
        //alert('html');
        //alert(html);
        //$.magnificPopup.close();
        //alert('found');
        //alert( "Result: " + response['debug'] );
    }); 
});
function bindUser(ids, user_id) {
    $('#bind-user-dialog-ajax').css('display', '');
    $.ajax({
        url: '{{ user_bind_action }}?ids='+ids+'&user_id='+user_id,
        type: "POST",
        data: post,
        dataType: 'json'
    }).done(function( response ) {
        $('#bind-user-dialog-ajax').css('display', 'none');
        $.magnificPopup.close();
    }); 
}
$('#bind-person-dialog-cancel').click(function(){
    $.magnificPopup.close();
});
$('#bind-user-dialog-cancel').click(function(){
    $.magnificPopup.close();
});
$('#bind-tag-dialog-submit').click(function(){
    ids_str = '';
    $('#bind-tag-dialog-submit').css('disabled', 'disabled');
    $('.search-results-admin-check input').each(function(){
        checked = $(this).prop('checked');
        val = $(this).val();
        if (checked) {
            if (ids_str != '') {
                ids_str += ',';
            }
            ids_str += val;
            //ids.push(val);
        }
    });
    tag_val = $('#bind-tag-dialog select').val();    
    if (tag_val != 'any') {
        if (ids_str != '') {
            $('#bind-tag-dialog-ajax').css('display', '');
            post = {'ids':ids_str,'tag_id':tag_val};
            //alert(ids_str);
            //alert(tag_val);
            $.ajax({
                url: '{{ tag_bind_action }}',
                type: "POST",
                data: post,
                dataType: 'json'
            }).done(function( response ) {
                $('#bind-tag-dialog-ajax').css('display', 'none');
                $.magnificPopup.close();
                {% if show_links %}
                location.reload();
                {% endif %}
                //alert( "Result: " + response['debug'] );
            });
        } else {
            alert('Укажите хотя бы один результат поиска!');
        }
    } else {
        alert('Выберите тег!');
    }
});
$('#bind-tag-dialog-cancel').click(function(){
    $.magnificPopup.close();
});
</script>
{% endif %}

{% endblock %}

{% block javascripts %}

{% endblock %}