<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title><%= @plan.title %></title>
    <%= render partial: '/shared/export/plan_styling' %>
  </head>
  <body>
    <% if @show_coversheet %>
      <%= render partial: '/shared/export/plan_coversheet' %>
    <% end %>
    
    <% @hash[: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>
        <%= (@hash[:phases].length > 1 ? "#{@plan.title} - #{phase[:title]}" : @plan.title) %>
      </h1>

      <hr />
      <% phase[:sections].each do |section| %>
        <% if @show_sections_questions %>
          <h3><%= section[:title] %></h3>
        <% end %>

        <% section[:questions].each do |question| %>
          <div class="question">
            <% if @show_sections_questions && !@public_plan %>
              <%# text in this case is an array to accomodate for option_based %>
              <% if question[:text].length > 1 %>
                <ul>
                  <% question[:text].each do |txt| %>
                    <li><%= txt %></li>
                  <% end %>
                </ul>
              <% else %>
                <p><%= raw question[:text][0].gsub(/<tr>(\s|<td>|<\/td>|&nbsp;)*(<\/tr>|<tr>)/,"") if question[:text].present? && question[:text][0].present? %></p>
              <% end %>
              <br>
            <% end %>

            <% answer = @plan.answer(question[:id], false) %>
            <% blank = (answer.present? && answer.is_valid?) ? answer.text.gsub(/<\/?p>/, '').gsub(/<br\s?\/?>/, '').chomp.blank? : true %>
            <% if blank && @show_unanswered %>
              <p><%= _('Question not answered.') -%></p>
            <% elsif !blank %>
              <% if answer.question_options.length > 0 %>
                <ul>
                  <% answer.question_options.each do |opt| %>
                    <li><%= opt.text %></li>
                  <% end %>
                </ul>
              <% else %>
                <p><%= raw answer.text %></p>
              <% end %>
            <% end %>
          </div>
        <% end %>
      <% end %> <!-- sections.each -->
    <% end %>
  </body>
</html>