<% if @period %>
  <h1><%= @period.title %></h1>
  <h4>
    <%= t('survey_b.period') %>:
    <%= display_date_time @period.start_date %>
    -
    <%= display_date_time @period.end_date %>
  </h4>
<% end %>

<table class="table main-list">
  <thead>
    <tr>
      <th></th>
      <th><%= t('survey_b.answered_sum') %></th>
      <th><%= t('survey_b.answered_sum7') %></th>
      <th><%= t('survey_b.answered_count') %></th>
      <% t('survey_b.questions').each do |q_key, question| %>
        <th>
          <%= question.sub /\d\./, ''  %>
        </th>
      <% end %>
    </tr>
  </thead>
  <tbody>
    <% (t('survey_b.institutions').merge({ 'all' => t('survey_b.answered_sum') })).each do |i_key, institution| %>
      <tr>
        <td><%= institution %></td>
        <td>
          <% if @results[i_key]['count'] == 0 %>
            <%= t('survey_b.no_data') %>
          <% else %>
            <%= sprintf("%.2f", ( @results[i_key]['sum'].to_f / t('survey_b.questions').count.to_f ) / @results[i_key]['count'].to_f )  %>
            <%#= ( ( @results[i_key]['sum'].to_f / t('survey_b.questions').count.to_f ) / @results[i_key]['count'].to_f ).round(2)  %>
          <% end %>
        </td>
        <td>
          <% if @results[i_key]['count'] == 0 %>
            <%= t('survey_b.no_data') %>
          <% else %>
            <%= sprintf("%.2f", ( @results[i_key]['sum7'].to_f / 7.0 ) / @results[i_key]['count'].to_f )  %>
            <%#= ( ( @results[i_key]['sum7'].to_f / 7.0 ) / @results[i_key]['count'].to_f ).round(2)  %>
          <% end %>
        </td>
        <td>
          <%= @results[i_key]['count']  %>
        </td>
        <% t('survey_b.questions').each do |q_key, question| %>
          <td>
            <% if @results[i_key]['count'] == 0 %>
              <%= t('survey_b.no_data') %>
            <% else %>
              <%= sprintf("%.2f", ( @results[i_key][q_key].to_f / @results[i_key]['count'].to_f )) %>
              <%#= ( @results[i_key][q_key].to_f / @results[i_key]['count'].to_f ).round(2) %>
            <% end %>
          </td>
        <% end %>
      </tr>
    <% end %>
  </tbody>
</table>

<ul>
<% [:good_institutions, :bad_institutions, :person_question, :rate_question, :quality_question].each do |item| %>
  <li>
    <table>
      <tr>
        <td><div id="<%= item.to_s %>_chart" style="width: 600px; height: 300px;"></div>
        </td>
        <td><div id="<%= item.to_s %>_bar_chart" style="width: 600px; height: 300px;"></div>
        </td>
      </tr>
    </table>
    
  </li>
<% end %>
</ul>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  window.google.load("visualization", "1", {packages: ["columnchart"]});
  window.google.load("visualization", "1", {packages:["corechart"]});
  window.google.setOnLoadCallback(function(){
  <% [:good_institutions, :bad_institutions].each do |item| %>

    <% @tcount = 0 %>
    var data_<%= item %> = google.visualization.arrayToDataTable([
      ['<%= t("survey_b.#{item}_label") %>', ''],
      <% @results[item].each do |institution, count| %>
        ['<%= t("survey_b.institutions.#{institution}") %>', <%= count %>],
        <% @tcount = @tcount + count %>
      <% end %>
    ]);
    var chart_<%= item %> = new google.visualization.PieChart(document.getElementById('<%= item.to_s %>_chart'));
    chart_<%= item %>.draw(data_<%= item %>, {title: '<%= t("survey_b.#{item}_label") %>'});

    //bar

    var data_bar_<%= item %> = google.visualization.arrayToDataTable([
      ['<%= t("survey_b.#{item}_label") %>', '%'],
      <% @results[item].each do |institution, count| %>
        ['<%= t("survey_b.institutions.#{institution}") %>', <%= count == 0 ? "0" : sprintf("%.2f", (count.to_f / @tcount ) * 100 )  %>],
      <% end %>
    ]);
    var bar_chart_<%= item %> = new google.visualization.ColumnChart(document.getElementById("<%= item.to_s %>_bar_chart"));
    bar_chart_<%= item %>.draw(data_bar_<%= item %>, {title: '<%= t("survey_b.#{item}_label") %>'});
  <% end %>

  <% [:person_question, :rate_question, :quality_question].each do |item| %>

    <% @tcount = 0 %>
    var data_<%= item %> = google.visualization.arrayToDataTable([
      ['<%= t("survey_b.#{item}_label") %>', ''],
      <% @results[item].each_with_index do |count, option| %>
        ['<%= t("survey_b.#{item}_options")[option] %>', <%= count %>],
        <% @tcount = @tcount + count %>
      <% end %>
    ]);
    var chart_<%= item %> = new google.visualization.PieChart(document.getElementById('<%= item.to_s %>_chart'));
    chart_<%= item %>.draw(data_<%= item %>, {title: '<%= t("survey_b.#{item}_label") %>'});

    //bar

    var data_bar_<%= item %> = google.visualization.arrayToDataTable([
      ['<%= t("survey_b.#{item}_label") %>', '%'],
      <% @results[item].each_with_index do |count, option| %>
        ['<%= t("survey_b.#{item}_options")[option] %>', <%= count == 0 ? "0" : sprintf("%.2f", (count.to_f / @tcount ) * 100 )  %>],
      <% end %>
    ]);
    
    var bar_chart_<%= item %> = new google.visualization.ColumnChart(document.getElementById("<%= item.to_s %>_bar_chart"));
    bar_chart_<%= item %>.draw(data_bar_<%= item %>, {title: '<%= t("survey_b.#{item}_label") %>'});
  <% end %>
  });
</script>