{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}
{% from "deliver_grant_funding/partials/_advanced_formatting_options.html" import advancedFormattingOptions %}
{% from "deliver_grant_funding/partials/_question_data_options.html" import questionDataOptions %}

{% set page_title = "Add a question - " ~ db_form.title %}
{% set type_constants = db_form.collection.type.constants %}
{% set active_item_identifier = type_constants.active_nav %}

{% block beforeContent %}
  {{
    govukBackLink({
        "text": "Back",
        "href": url_for("deliver_grant_funding.choose_question_type", grant_id=grant.id, form_id=db_form.id, question_data_type=chosen_question_data_type.name, parent_id=parent.id if parent else None)
    })
  }}
{% endblock beforeContent %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      <h1 class="govuk-heading-l">Add question</h1>

      <div class="govuk-body govuk-form-group">
        <p id="question-type-label" class="govuk-!-font-weight-bold govuk-!-margin-bottom-1">Question type</p>
        <p class="govuk-body" aria-labelledby="question-type-label">{{ chosen_question_data_type.value }}</p>
      </div>

      <form method="post" novalidate>
        {{ form.csrf_token }}

        {# Duplicate the submit button, fully hidden, so that the default action for the form is to submit it (rather than add data to a field) #}
        {{ form.submit(params={"classes": "govuk-!-display-none"}) }}

        <div data-module="context-aware-editor" data-toolbar-enabled="false" data-i18n="{}">
          <div class="govuk-!-display-none" data-context nonce="{{ csp_nonce() }}">{{ context_keys_and_labels | tojson }}</div>
          {% set addDataButton %}
            {{ form.add_context(params={"html": "Reference data <span class='govuk-visually-hidden'>in question name</span>", "classes": "govuk-button--secondary app-context-aware--button", "value": "text"}) }}
          {% endset %}
          {{ form.text(params={"label": {"classes": "govuk-!-font-weight-bold"}, "attributes": {"data-module": "textarea-no-newlines", "data-context-aware-editor-target": ""}, "formGroup": {"classes": "app-context-aware--container", "afterInput": {"html": addDataButton} } }) }}

          {{ form.name(params={"label": {"classes": "govuk-!-font-weight-bold"} }) }}
        </div>

        <div data-module="context-aware-editor" data-toolbar-enabled="false" data-i18n="{}">
          <div class="govuk-!-display-none" data-context nonce="{{ csp_nonce() }}">{{ context_keys_and_labels | tojson }}</div>
          {% set addDataButton %}
            {{ form.add_context(params={"html": "Reference data <span class='govuk-visually-hidden'>in question hint</span>", "classes": "govuk-button--secondary app-context-aware--button", "value": "hint"}) }}
          {% endset %}
          {{ form.hint(params={"label": {"classes": "govuk-!-font-weight-bold"}, "attributes": {"data-module": "textarea-no-newlines", "data-context-aware-editor-target": ""}, "formGroup": {"classes": "app-context-aware--container", "afterInput": {"html": addDataButton} } }) }}
        </div>

        {% if chosen_question_data_type in [enum.question_type.RADIOS, enum.question_type.CHECKBOXES] %}
          {{ form.data_source_items(params={"label": {"classes": "govuk-!-font-weight-bold"} }) }}


          {% set conditionalHtml %}
            {{ form.none_of_the_above_item_text }}
          {% endset %}
          {{ form.separate_option_if_no_items_match(params={"items": [{"conditional": {"html": conditionalHtml} }]}) }}
        {% endif %}

        {{ questionDataOptions(form, chosen_question_data_type, enum) }}

        {{ advancedFormattingOptions(form, chosen_question_data_type, enum) }}

        <span class="govuk-button-group">
          {{ form.submit(params={"text": "Add question"}) }}

          {% set link = url_for("deliver_grant_funding.list_section_questions", grant_id=grant.id, form_id=db_form.id) if not parent else url_for('deliver_grant_funding.list_group_questions', grant_id=grant.id, group_id=parent.id) %}
          <a class="govuk-link govuk-link--no-visited-state" href="{{ link }}">Cancel</a>
        </span>
      </form>
    </div>
  </div>
{% endblock content %}
