<%# locals: { template, question, fragment, madmp_schema, research_output, readonly, locking, base_template_org } %>
<!--
  This partial creates a form for each type of question. The local variables are: plan, answer, question, research_output, readonly
-->
<!-- Question text -->
<% q_format = question.question_format %>
<% fragment_id = fragment.id unless fragment.nil? %>
<% url = fragment.present? ? create_or_update_madmp_fragments_path(id: fragment.id) : create_or_update_madmp_fragments_path %>
<%= form_for :madmp_fragment, url: url, html: {method: :post, 'data-autosave': question.id, class: 'form-answer dynamic-form', id: nil  } do |f| %>
  <% if !readonly %>
    <%= f.fields_for(:answer, answer) do |af| %>
      <%= af.hidden_field :plan_id, id: nil %>
      <%= af.hidden_field :question_id, id: nil %>
      <%= af.hidden_field :lock_version, id: nil %>
      <%= af.hidden_field :research_output_id, :value => research_output.id, id: nil%>
      <%= af.hidden_field :is_common, class: 'ans_is_common' if research_output.main? %>
      <%= af.hidden_field :id, id: nil, class: 'answer_id' %>
    <% end %>
  <% end %>
  <%= f.hidden_field :schema_id, :value => madmp_schema.id, id: nil, class: "schema_id" %>
  <%= f.hidden_field :dmp_id, :value => dmp_id, id: nil %>
  <%= f.hidden_field :parent_id, :value => parent_id, id: nil %>
  <%= f.hidden_field "source", :value => "form" %>
  
  <fieldset aria-labelledby="answer-fieldset">
    <% classname = fragment.classname unless fragment.nil? %>
    <%= f.label(:text, sanitize(question.text), class: 'control-label') unless question.nil? %>
    <div class="form-group madmp-fragment fragment-<%= fragment_id %>">   
      <%= render(partial: 'shared/dynamic_form/form', locals: { 
            f: f,
            fragment: fragment, 
            schema: madmp_schema,
            question_id: question.id, 
            readonly: readonly,
            classname: classname,
            fragment_id: fragment_id,
            template_locale: template.locale
          }) %>
      <div class="overlay" style="display:none;">
        <div class="spinner"></div>
        <br/>
        <%= _('Saving...') %>
      </div>
    </div>
    <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %>
  </fieldset>
  <!--Example Answer area -->
  <% if template.present? && template.org.present? %>
    <% question.example_answers([base_template_org.id, template.org.id]).each do |annotation| %>
      <% if annotation.present? && annotation.org.present? && annotation.text.present? %>
        <div class="panel panel-default example-answer">
          <span class="label label-default example-answer-link" 
                data-toggle="collapse" 
                data-target="<%= "#example-#{question.id}-research-output-#{research_output.id}"  %>">
            <%="#{annotation.org.abbreviation} "%> <%=_('example answer')%>
            <i class="more-less fa fa-plus small"></i>
          </span>
          <div class="panel-body collapse" id="<%= "example-#{question.id}-research-output-#{research_output.id}"  %>">
            <%= sanitize annotation.text %>
          </div>
        </div>
      <% end %>
    <% end %>
  <% end %>
<% end %>
