<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>
        <%= @plan.title %>
    </title>
    <style>
      html {
        font-family: <%= @formatting[:font_face].tr('"', '') -%>; 
        font-size: <%= @formatting[:font_size] -%>pt; 
        margin: <%= @formatting[:margin][:top] %>px <%= @formatting[:margin][:right] %>px <%= @formatting[:margin][:bottom] %>px <%= @formatting[:margin][:left] %>px; */
      }
      h1 {
        font-size: <%= @formatting[:font_size] + 8 -%>pt; 
        font-face:bold; 
        padding: 0;
      }
      h2 {
        font-size: <%= @formatting[:font_size] + 3 -%>pt; 
        font-face:bold; 
        padding: 0; 
        margin: 1em 0 0 0;
      }
      h3 {
        font-size: <%= @formatting[:font_size] + 2 -%>pt; 
        font-face:bold; 
        padding: 0; 
        margin: 1em 0 0 0;
      }
      h2 + div.question > h3 {
        margin: 0; 
      }
      table, tr, td, th, tbody, thead, tfoot {
        page-break-inside: avoid !important; 
      }
      table { 
        border-collapse: collapse; 
      }
      th, td { 
        border: 1px solid black !important; 
        padding: 2px;
      }
      p { 
        margin: 0.25em 0;
      }
      .title {
        width: 70%;
        margin-left: auto;
        margin-right: auto;
      }
      .cover-page {
        font-size: <%= @formatting[:font_size] + 1 -%>pt; 
      }
      .question {
        margin-top: 15px;
        margin-bottom: 10px;
      }
      .question:first-child {
        margin-top: 0;
      }
      .annotations {
        margin-left: 15px;
        margin-bottom: 10px;
      }
      hr.bottom {
        position: relative;
        top: 590px;
      }
    </style>
  </head>
  <body>
    <hr />
    <div class="title" style="text-align:center;">
      <h1><%= @plan.title %></h1>
      <p><em><%= _("A Data Management Plan created using ") + Rails.configuration.branding[:application][:name] %></em></p>
    </div>
    </br>
    <div class="cover-page">
      <p><%= _("Creator(s): ") + @creator_text %></p>    <br>
      <p><%= _("Affiliation: ") + @affiliation %></p>    <br>
      <% if @funder.present? %>
        <p><%= _("Template: ") + @funder %></p>   <br>
      <% else %>
        <p><%= _("Template: ") + @template + @customizer %></p> <br>
      <% end %>
      <% if @plan.grant_number.present? %>
        <p><%= _("Grant number: ") + @plan.grant_number %></p> <br>
      <% end %>
      <% if @plan.description.present? %>
        <p><%= _("Project abstract: ") %></p>
        <div style="margin-left: 15px;"><%= raw(@plan.description) %></div><br>
      <% end %>
      <p><%= _("Last modified: ") + @plan.updated_at.to_date.to_s %></p> <br>

      <p><%= _("Copyright information:") %></p>
      <div style="margin-left: 15px;"><p><%= _(" The above plan creator(s) have agreed that others may use as much of the text of this plan as they would like in their own plans, and customise it as necessary. You do not need to credit the creator(s) as the source of the language used, but using any of the plan's text does not imply that the creator(s) endorse, or have any relationship to, your project or proposal") %></p></div>
     <hr class="bottom" />
    </div>

    <% @phases.each do |phase| %>
      <div style="page-break-before:always;"></div> <!-- Page break before each phase -->
      <!-- If there is more than one phase show the plan title and phase title -->
      <h1><%= (@phases.length > 1 ? "#{@plan.title} - #{phase.title}" : @plan.title) %></h1>
      <hr />
      <% Section.where(phase_id: phase.id, id: @a_s_ids).order(:number).each do |section| %>
        <h3><%= section.title %></h3>
        <% Question.where(section_id: section, id: @a_q_ids).order(:number).each do |question| %>
          <div class="question">
            <% answer = @plan.answer(question.id, false) %>
            <% if answer.nil? %>
              <p><%= _('Question not answered.') -%></p>
            <% else %>
              <% q_format = question.question_format %>
              <% if q_format.option_based? %>
                <ul>
                <% answer.question_options.each do |option| %>
                  <li><%= option.text %></li>
                <% end %>
                </ul>
                <!-- Validates if this question has comments-->
                <% if question.option_comment_display == true %>
                  <% if !answer.text.nil? %>
                    <%= raw answer.text.gsub(/<tr>(\s|<td>|<\/td>|&nbsp;)*(<\/tr>|<tr>)/,"") %>
                  <% end %>
                <% end %>
              <% else %>
                <!-- display an answer for questions type 'text area' and 'text field'-->
                <% if !answer.text.nil?  %>
                  <%= raw answer.text.gsub(/<tr>(\s|<td>|<\/td>|&nbsp;)*(<\/tr>|<tr>)/,"") %>
                <% end %>
              <% end %>
            <% end %>
          </div>
        <% end %>
      <% end %>
    <% end %>
  </body>
</html>
