module OrbitBackendHelper

  def sortable(column)
    direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
    {:sort => column, :direction => direction}
  end

  def is_sort_active?(name)
    res = ''
    res << ' select' if params[:sort].eql?(name)
    res << ' active' if params[:sort].eql?(name) && params[:direction].eql?('asc')
    res
  end

  def is_sort?(name)
    ' web-symbol' if params[:sort].eql?(name)
  end

  def is_filter_active?(type, id)
    ' active' if (@filter[type].include?(id.to_s) rescue nil)
  end

  def render_sort_bar(delete_all, *titles)
    content_tag :table, :class => "table main-list" do
      content_tag :thead do
        content_tag :tr, :class => "sort-header" do
          concat (content_tag :th, :class => "span1 strong" do
            concat check_box_tag :check_all
            concat link_to content_tag(:i, nil, :class => "icon-trash"), '#', :class => "list-remove"
           end) if (delete_all && (is_admin? || (is_manager? rescue nil)))
          titles.each do |title|
            concat render_title(title[0], title[1], title[2], title[3])
          end
        end
      end
    end
  end

  def render_title(title, fields, span, translation)
    content_tag :th, :class => "sort #{span} #{is_sort_active?(title)}" do
      link_to (t(translation) + content_tag(:b, nil, :class => is_sort?(title))).html_safe, url_for({:filter => @filter}.merge(sortable(title).merge(:sort_options => fields))), :class => 'js_history'
    end
  end

end