<!--
**Project: DMPRoadmap
**Description: This block shows a question, its guidance, and options when multiple choice is selected.
**Arguments transferred: 'question'
**Copyright: Digital Curation Centre and University of California Curation Center
-->

<div class="question-div">
  <table class="dmp_details_table phase">
    <!--Question number -->
    <tr>
      <td class="first_template"><%= t('org_admin.questions.question_number_label')%></td>
      <td><%= question.number %></td>
    </tr>

    <!--Question text -->
    <tr>
      <td class="first_template"><%= t('org_admin.questions.question_text_label')%></td>
      <td><%= raw question.text %>
        </br>
        <div class="answer-readonly">
          <% q_format = question.question_format %>
          <% if q_format.title == t("helpers.checkbox") || q_format.title == t("helpers.multi_select_box") || q_format.title == t("helpers.radio_buttons") || q_format.title == t("helpers.dropdown") %>
            <ul class='options'>
            <% if question.question_options.is_a? QuestionOption  %>
              <li>- <%= question.question_options.text %></li>
            <% else %>
              <% if !question.question_options.to_a.nil? %>
                <% question.question_options.to_a.sort_by{|op| op['number']}.each do |o| %>
                  <li>- <%= o.text %></li>
                <% end %>
              <% end %>
            <% end %>
            </ul>
          <% end %>
        </div>
      </td>
    </tr>
    <!--Default value -->
    <% if q_format.title  == t("helpers.text_field") || q_format.title == t("helpers.text_area") %>
      <% if !question.default_value.nil? %>
        <tr>
          <td class="first_template"><%= t('org_admin.questions.default_value_label')%></td>
          <td><%= raw question.default_value %></td>
        </tr>
      <% end %>
    <% end %>
    <!-- Question format -->
    <tr>
      <td class="first_template"><%= t('org_admin.questions.answer_format_label')%></td>
      <td><%= q_format.title %>
        </br>
        <% if q_format.title == t("helpers.checkbox") || q_format.title == t("helpers.multi_select_box") || q_format.title == t("helpers.radio_buttons") || q_format.title == t("helpers.dropdown") %>
          <% if question.option_comment_display == true %>
            <%= t("org_admin.questions.option_comment_display")%>
          <% else %>
            <%= t("org_admin.questions.option_comment_hide")%>
          <% end %>
        <% end %>
        <div class="add_space"></div>
      </td>
    </tr>

    <!-- Suggested answer or Example-->
    <% if question.section.phase.template.org_type != constant("organisation_types.funder")  %>
      <% suggested_answer = question.get_suggested_answer(current_user.org_id) %>
      <% if !suggested_answer.nil?  && suggested_answer.text != ""  %>
        <tr>
          <td class="first_template">
            <% if suggested_answer.is_example? %>
              <%= t('org_admin.questions.example_answer_label')%>
            <% else %>
              <%= t('org_admin.questions.suggested_answer_label')%>
            <% end %>
          </td>
          <td><%= raw suggested_answer.text %></td>
        </tr>
      <% end %>
    <% end %>
    <!-- Guidance linked to this question -->
    <% if !question.guidance.nil?  %>
      <tr>
        <td class="first_template"><%= t('org_admin.questions.guidance_label')%></td>
        <td><%= raw question.guidance %></td>
      </tr>
    <% end %>
    <!-- Themes -->
    <% themes_q = question.themes %>
    <% if !themes_q.nil? %>
      <tr>
        <td class="first_template"><%= t('org_admin.questions.themes_label')%></td>
        <td><% i = 1%>
          <% themes_q.each do |t|%>
            <%= t.title %>
            <% if themes_q.count > i %>
              ,
              <% i +=1 %>
            <% end %>
          <% end %>
        </td>
      </tr>
    <% end %>
  </table>
  <br/>
  <!-- Add suggested or example answers to a funders template-->
  <% if !question.modifiable %>
    <% suggested_answer = question.get_suggested_answer(current_user.org_id) %>
    <% if !suggested_answer.nil?  && suggested_answer.text != "" %>
      <div id="show_suggested_answer_div_<%= question.id%>">
        <%= render partial: 'show_suggested_answer', locals: {suggested_answer: suggested_answer, question: question} %>
      </div>
      <div id="edit_suggested_answer_div_<%= question.id%>" style="display: none">
        <%= render partial: 'edit_suggested_answer', locals: {suggested_answer: suggested_answer, question: question} %>
      </div>
    <% end %>
    <div id="add_suggested_answer_block_<%= question.id%>" style="display: none">
      <%= render partial: 'add_suggested_answer', locals: {question: question} %>
    </div>
  <% end %>

  <br/>
  <!-- edit question button-->
  <% if (@edit && question.modifiable) %>
    <div class="move_2_right">
      <%= hidden_field_tag :question_id, question.id, class: "question_id" %>
      <%= link_to t("org_admin.questions.question_edit_button"), '# ', class: "btn btn-primary edit_question_button"%>
      <% if !question.section.published? %>
        <%= link_to t("org_admin.questions.question_delete_button"), admin_destroyquestion_template_path(question_id: question.id),
        confirm: t("org_admin.questions.delete_message", question_text: question.text ), method: :delete, class: "btn btn-primary"%>
      <% end %>
    </div>
  <% elsif !@edit && question.modifiable %>
    <% suggested_answer = question.get_suggested_answer(current_user.org_id) %>
    <% if suggested_answer.nil? %>
      <div id="add_suggested_answer_button_<%= question.id%>">
        <div class="move_2_right">
          <%= hidden_field_tag :question_id, question.id, class: "question_id" %>
          <%= link_to t("org_admin.questions.suggested_or_example_answer_button"), '# ', class: "btn btn-primary add_suggested_answer_button"%>
        </div>
      </div>
    <% end %>
  <% end %>
  <div class="div_clear"></div>
</div>


<!--guidance block -->
<%= render partial: 'guidance_display', locals: {question: question}%>
