<%
user ||= User.current; block_name ||= ''; uniq_id ||= ''; page_module ||= nil; selected_values ||= []
groups_url ||= params
modul_uniq_id ||= "#{controller_name}_#{action_name}"; uniq_id ||= modul_uniq_id

options ||= Hash.new
options[:block_name] ||= block_name; options[:uniq_id] ||= uniq_id
options[:page_module] ||= page_module
modal_selector = (options[:modal_selector].to_s == 'true')
no_scroll = options[:no_scroll] || params[:easy_query_q]

easy_query ||= local_assigns[:query]
easy_query = present(easy_query, options: options)
options = easy_query.options[:options]

# available *options*
# * :disable_sort  => true - change header from sorting_link to plain text
# * :modal_selector => true - display view as lookup window, with selectable rows. Require parameters for modal selector
# * :go_to_url => {:controller => ...} - set rows clickable, jump to url...
# * :hide_sums_in_group_by => true - This hide sums and count of records in group by row.
# * :hascontextmenu => true/false
# * :modal_selector => true/false
-%>
<div class="clear"></div>
<%= form_tag({}, :id => "easy-query-list-#{easy_query.entity.name.underscore.dasherize}") do -%>
  <%= hidden_field_tag(:tab, params[:tab]) %>
  <div class="autoscroll easy-query-list list">

    <table class="list entities <%= easy_query.entity.name.pluralize.downcase %> <%= modul_uniq_id %> <%= sort_css_classes %> <%= options[:table_class] %>" data-hascontextmenu="<%= easy_query.has_contextmenu? -%>">
    <%# easy_cache ['easy_query_header', block_name, I18n.locale] do  %>
      <thead>
        <% if easy_query.period_columns? %>
          <tr>
            <%= content_tag(:th, nil, :class => 'hide-when-print') -%>
            <th class="hide-when-print">&nbsp;</th>
            <% easy_query.non_period_inline_columns.each do |column| -%>
              <th>&nbsp;</th>
            <% end -%>
            <% 0.upto(easy_query.number_of_periods_by_zoom - 1) do |idx| %>
              <th class="period" colspan="<%= easy_query.period_columns.size %>"><%= query_period_name(easy_query, idx) %></th>
            <% end %>
            <%= content_tag(:th, nil, :class => 'hide-when-print') if modal_selector -%>
            <th class="hide-when-print">&nbsp;</th>
          </tr>
        <% end %>
        <tr>
          <%= content_tag(:th, easy_query.has_contextmenu? ? link_to('', 'javascript:void(0)', :onclick => 'EASY.contextMenu.toggleIssuesSelection(this)', :title => "#{l(:button_check_all)}/#{l(:button_uncheck_all)}", :class => 'icon icon-checked') : '', :class => 'hide-when-print checkbox') %>
          <% if modal_selector %>
            <%= content_tag(:th, nil, :class => 'hide-when-print') %>
          <% end -%>
          <th class="hide-when-print easy-query-additional-beginning-buttons">&nbsp;</th>
          <% easy_query.inline_columns.each do |column| -%>
            <%= easy_query_column_header(easy_query, column, options) %>
          <% end -%>
          <% unless modal_selector %>
            <th class="hide-when-print easy-query-additional-ending-buttons">&nbsp;</th>
          <% end %>
        </tr>
        <% easy_query_first_sumable_index = -1 %>
        <% if easy_query.show_sum_row? && easy_query.inline_columns.detect{|i| easy_query_first_sumable_index += 1; i.sumable?} %>
          <% easy_query_first_sumable_index = 1 if easy_query_first_sumable_index < 1
             sumable_colspan = easy_query_first_sumable_index
          %>
          <tr id="totalsum-summary">
            <td class="totalsum-description">
              <%= options[:label_sumable_header_total] || l(:label_total_total) %>
            </td>

            <% (sumable_colspan - 1).times do %>
              <td>&nbsp;</td>
            <% end %>

            <td class="hide-when-print">&nbsp;</td>
            <%= content_tag(:td, nil, :class => 'hide-when-print') -%>
            <%= content_tag(:td, nil, :class => 'hide-when-print') if modal_selector -%>
            <% easy_query.inline_columns[(easy_query_first_sumable_index)..(easy_query.inline_columns.size-1)].each_with_index do |column, index| %>
              <% if column.sumable_header? %>
                <%= content_tag :td, format_html_entity_attribute(easy_query.entity, column, easy_query.entity_sum(column, entities: entities), inline_editable: false, project: easy_query.project).html_safe, :class => column.css_classes -%>
              <% else %>
                <%= content_tag :td -%>
              <% end %>
            <% end %>
            <% unless modal_selector %>
              <td class="hide-when-print">&nbsp;</td>
            <% end %>
          </tr>
        <% end %>
      </thead>
    <%# end %>
      <tbody>
        <% if easy_query.grouped? && !params[:easy_query_q] %>
          <%= render 'easy_queries/easy_query_groups', easy_query: easy_query, groups: entities, selected_values: selected_values, modul_uniq_id: modul_uniq_id, user: user, options: options %>
        <% else %>
          <%= render 'easy_queries/easy_query_entities', easy_query: easy_query, entities: entities, selected_values: selected_values, modul_uniq_id: modul_uniq_id, user: user, options: options %>
        <% end %>
      </tbody>
    </table>
  </div>
  <%#= render :partial => 'easy_queries/easy_query_tooltip_box', :locals => {:block_name => options[:block_name], :uniq_id => options[:uniq_id]} -%>
<% end -%>

<%= render :partial => 'easy_queries/easy_query_tooltip_box' if options[:render_query_tooltip_box] %>

<p style="display: none;" class="infinite-scroll-load-next-page-trigger-container hide-when-print">
  <a href="javascript:void(0)" class="button button-main button-rounded button-outlined infinite-scroll-load-next-page-trigger"><%= l(:button_load_additional_records) %><i class="icon-arrow"></i></a>
</p>

<% if local_assigns[:entity_pages] && local_assigns[:entity_count] %>
  <span class="pagination" style="display: none">
    <% params[:view_options] = options %>
    <%= pagination_links_full(entity_pages, entity_count) %>
  </span>
<% end %>

<script type="text/javascript">

  $(function() {
    var $table = $('table.entities.<%= modul_uniq_id %>');
    $table.easygrouploader({
      loadUrl: '<%= url_for(groups_url.merge(:view_options => options, only_path: true)).html_safe %>',
      easy_query: '<%= easy_query.type %>',
      load_opened: <%= !!easy_query.load_groups_opened? %>,
      next_button_cols: '<%= easy_query.inline_columns.count + 3 %>',
      callback: function(){
        $table.closest('#easy_modal').dialog('option', 'position', {my: "center", at: "center", of: window , collision: 'fit'});
      }
    });

    <% unless modal_selector || no_scroll %>
      initEasyInlineEdit();

      if (typeof EASY.utils.setInfiniteScrollDefaults === 'function')
      {
        EASY.utils.setInfiniteScrollDefaults();
        $('table.list.entities.<%= easy_query.entity.name.pluralize.downcase -%>:first > tbody').infinitescroll({
          navSelector: '.pagination',
          nextSelector: '.pagination .next > a',
          itemSelector: 'table.list.entities.<%= easy_query.entity.name.pluralize.downcase -%>:first > tbody > tr, .pagination .next > a',
          previousSelect: '.pagination .previous > a'
          //binder: $('#main')
        }, function(data, opts) {
          var $sumary_was = $(data[0]).prevAll('tr.summary').first();
          var $sumary_is;
          for (var i = 0; i < data.length; ++i) {
            $sumary_is = $(data[i]);
            if ($sumary_is.hasClass('summary'))
              break;
          }
          if ($sumary_is && $sumary_was && $sumary_is.data('uniq_id') === $sumary_was.data('uniq_id'))
            $sumary_was.remove();

          var a = $(data.pop());
          if (a.is('a')) {
            opts.path = [a.attr('href')];
            a.remove();
          } else {
            data.push(a[0]);
            opts.state.isPaused = true;
            $(".infinite-scroll-load-next-page-trigger").parent().hide();
          }
          initEasyInlineEdit();
          EASY.utils.initProjectEdit();
          initEasyAutocomplete();
          LazyLoader.refresh();
          $('table.entities.<%= modul_uniq_id %>').easygrouploader('load_groups');
        });
      }
    if (<%= User.current.pref.disable_automatic_loading %>){
      $(window).unbind('.infscr');
    }
    <% end %>
  });
  $(document).off('click', '.infinite-scroll-load-next-page-trigger');
  $(document).on('click', '.infinite-scroll-load-next-page-trigger', function(e) {
    var selector = $('<%= '#modal-selector-entities ' if modal_selector %>table.list.entities.<%= easy_query.entity.name.pluralize.downcase -%>:first > tbody');
    selector.infinitescroll('retrieve', {});
  });

</script>
