<% if @statuses.empty? or rows.empty? %>
    <p><i><%=l(:label_no_data)%></i></p>
<% else %>
<table class="list issue-report-detailed">
<thead><tr>
<th></th>
<% for status in @statuses %>
<th><%= status.name %></th>
<% end %>
<th><strong><%=l(:label_open_issues_plural)%></strong></th>
<th><strong><%=l(:label_closed_issues_plural)%></strong></th>
<th><strong><%=l(:label_total)%></strong></th>
</tr></thead>
<tbody>
<% for row in rows %>
<tr>
  <td class="name"><%= link_to row.name, aggregate_path(@project, field_name, row, :subproject_id => nil) %></td>
  <% for status in @statuses %>
    <td><%= aggregate_link data, { field_name => row.id, "status_id" => status.id }, aggregate_path(@project, field_name, row, :status_id => status.id, :subproject_id => nil) %></td>
  <% end %>
  <td><%= aggregate_link data, { field_name => row.id, "closed" => 0 }, aggregate_path(@project, field_name, row, :status_id => "o", :subproject_id => nil) %></td>
  <td><%= aggregate_link data, { field_name => row.id, "closed" => 1 }, aggregate_path(@project, field_name, row, :status_id => "c", :subproject_id => nil) %></td>
  <td><%= aggregate_link data, { field_name => row.id }, aggregate_path(@project, field_name, row, :status_id => "*", :subproject_id => nil) %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr class="total">
  <td><%= l(:label_total) %></td>
  <% for status in @statuses %>
  <td><%= aggregate data, { "status_id" => status.id } %></td>
  <% end %>
  <td><%= aggregate data, { "closed" => 0 } %></td>
  <td><%= aggregate data, { "closed" => 1 } %></td>
  <td><%= aggregate data, { } %></td>
</tr>
</tfoot>
</table>
<% other_formats_links do |f| %>
  <%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
<% end %>
<div id="csv-export-options" style="display: none;">
  <h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
  <%= form_tag(project_issues_report_details_path(@project, :detail => params[:detail], :format => 'csv'), :method => :get, :id => 'csv-export-form') do %>
  <%= export_csv_encoding_select_tag %>
  <%= export_csv_separator_select_tag %>
  <p class="buttons">
    <%= submit_tag l(:button_export), :name => nil, :onclick => 'hideModal(this);', :data => {:disable_with => false} %>
    <%= link_to_function l(:button_cancel), 'hideModal(this);' %>
  </p>
  <% end %>
</div>

<div class="issue-report-graph hide-when-print">
  <canvas id="issues_by_<%= params[:detail] %>"></canvas>
</div>
<div class="issue-report-graph hide-when-print">
  <canvas id="issues_by_status"></canvas>
</div>
<%= javascript_tag do %>
  function renderChart(canvas_id, title, chartData){
    var backgroundColors = ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)'];
    var borderColors = ['rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)'];
    for (var i = 0; i < chartData.datasets.length; i++) {
      chartData.datasets[i].backgroundColor = backgroundColors[i % backgroundColors.length];
      chartData.datasets[i].borderColor = borderColors[i % borderColors.length];
      chartData.datasets[i].borderWidth = 1;
    }
    new Chart($(canvas_id), {
      type: 'bar',
      data: chartData,
      options: {
        indexAxis: 'y',
        elements: {
          bar: {borderWidth: 2}
        },
        responsive: true,
        plugins: {
          legend: {position: 'right'},
          title: {
            display: true,
            text: title
          }
        },
        scales: {
          yAxis: {stacked: true},
          xAxis: {stacked: true, ticks: {precision: 0}}
        }
      }
    });
  }
  $(document).ready(function(){
    var chartData1 = {
      labels: <%= raw rows.collect{|row| row.name}.to_json %>,
      datasets: <%= raw @statuses.collect{|status| {"label" => status.name, "hidden" => status.is_closed?, "data" => rows.collect{|row| aggregate(data, {field_name => row.id, "status_id" => status.id})}}}.to_json %>
    };
    var chartData2 = {
      labels: <%= raw @statuses.collect{|status| status.name}.to_json %>,
      datasets: <%= raw rows.collect{|row| {"label" => row.name, "data" => @statuses.collect{|status| aggregate(data, {field_name => row.id, "status_id" => status.id})}}}.to_json %>
    };
    renderChart("#issues_by_<%= params[:detail] %>", "<%= l(:label_issues_by, @report_title) %>", chartData1);
    renderChart("#issues_by_status", "<%= l(:label_issues_by, l(:field_status)) %>", chartData2);
  });
<% end %>
<% content_for :header_tags do %>
  <%= javascript_include_tag "chart.min" %>
<% end %>
<% end %>
