<div class="box">
  <%= toggling_container("easy_report_issues_#{field_name}_simple", User.current, {:heading => @report_title, :heading_links =>
        [link_to_function('', "toggleReports('simple_list_container', 'simple_chart_container', 'chart')", :class => 'icon icon-report', :title => l(:title_change_layout)),
        link_to_function('', "toggleReports('simple_list_container', 'simple_chart_container', 'table')", :class => 'icon icon-bullet-list', :title => l(:title_change_layout)),
        link_to_function('', "toggleReports('simple_list_container', 'simple_chart_container', '')", :class => 'icon icon-list', :title => l(:title_change_layout))
        ], :wrapping_heading_element_classes => 'icon icon-reorder'}) do  %>
      <div class="splitcontent">
        <div class="splitcontentleft" id="simple_list_container">
          <div class="autoscroll">
            <%= render :partial => 'reports/simple', :locals => { :data => simple_data, :field_name => field_name, :rows => simple_rows } %>
          </div>
        </div>

        <div class="splitcontentright" id="simple_chart_container">
          <div class="autoscroll">
            <div id="simple_chart"></div>
          </div>
        </div>
      </div>
  <% end %>
</div>
<div class="clear"></div>
<div class="box">
  <%= toggling_container("easy_report_issues_#{field_name}_details", User.current, {:heading => l(:label_details), :heading_links =>
        [link_to_function('', "toggleReports('details_list_container', 'details_chart_container', 'chart')", :class => 'icon icon-report', :title => l(:title_change_layout)),
        link_to_function('', "toggleReports('details_list_container', 'details_chart_container', 'table')", :class => 'icon icon-bullet-list', :title => l(:title_change_layout)),
        link_to_function('', "toggleReports('details_list_container', 'details_chart_container', '')", :class => 'icon icon-list', :title => l(:title_change_layout))
        ], :wrapping_heading_element_classes => 'icon icon-reorder'}) do  %>
      <div class="splitcontent">
        <div class="splitcontentleft" id="details_list_container">
          <div class="autoscroll">
            <%= render :partial => 'reports/details', :locals => { :data => details_data, :field_name => field_name, :rows => details_rows } %>
          </div>
        </div>
        <div class="splitcontentright" id="details_chart_container">
          <div class="autoscroll">
            <div id="details_chart"></div>
          </div>
        </div>
      </div>
  <% end %>
</div>
<div class="clear"></div>
<%
simple_chart_data = [[l(:label_open_issues_plural)], [l(:label_closed_issues_plural)]]
simple_chart_labels = []
simple_rows.each do |r|
  open = aggregate(simple_data, { field_name => r.id, 'closed' => 0})
  closed = aggregate(simple_data, { field_name => r.id, 'closed' => 1})

  if open > 0 || closed > 0
    simple_chart_data[0] << open
    simple_chart_data[1] << closed
    simple_chart_labels << r.to_s
  end
end

@chart_statuses.each do |status|
  details_rows.collect{|r| aggregate(details_data, { field_name => r.id, 'status_id' => status.id})}
end

details_chart_data = @chart_statuses.collect{|status| [status.name]}
details_chart_labels = []
details_rows.each do |r|
  status_values = {}
  @chart_statuses.each do |status|
    status_values[status.id] = aggregate(details_data, { field_name => r.id, 'status_id' => status.id})
  end

  if status_values.detect{|k, v| v > 0}
    @chart_statuses.each_with_index do |status, idx|
      details_chart_data[idx] << status_values[status.id]
    end
    details_chart_labels << r.to_s
  end

end
%>
<script type="text/javascript">
  $(document).ready(function() {
    createChart('simple_chart', 'line', <%= simple_chart_data.to_json.html_safe %>, <%= simple_chart_labels.to_json.html_safe %>);
    createChart('details_chart', 'bar', <%= details_chart_data.to_json.html_safe %>, <%= details_chart_labels.to_json.html_safe %>);
  });

  function toggleReports(table_container_id, chart_container_id, layout) {
    var table_container = $('#' + table_container_id);
    var chart_container = $('#' + chart_container_id);

    table_container.removeClass('splitcontentleft');
    chart_container.removeClass('splitcontentright');

    if (layout === 'chart') {
      chart_container.insertBefore(table_container);
    } else if (layout === 'table') {
      table_container.insertBefore(chart_container);
    } else {
      table_container.insertBefore(chart_container);
      table_container.addClass('splitcontentleft');
      chart_container.addClass('splitcontentright');
    }
  }
</script>
<% content_for :sidebar do %>
  <%= render :partial => 'easy_report_issues/sidebar', :locals => {:statuses => @statuses, :tab => tab} %>
<% end %>
