{% load i18n %}
{% get_available_languages as languages %}

    <div class="modal" id="questions-form-modal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h2 class="modal-title">
                        <span ng-hide="service.values.id">{% trans 'Create question' %}</span>
                        <span ng-show="service.values.copy">{% trans 'Copy question' %}</span>
                        <span ng-show="service.values.id && !service.values.copy">{% trans 'Update question' %}</span>
                    </h2>
                </div>

                <div class="modal-body">
                    <div class="copy-block" ng-show="service.values.copy">
                        <p>
                            {% trans 'Please enter the values for the copy of the question below and change at least the <strong>key</strong>.' %}
                        </p>
                        <p>
                            {% trans 'Other properties can be changed once the new question is created.' %}
                        </p>
                    </div>
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group" ng-class="{'has-error': service.errors.uri_prefix }">
                                <label class="control-label" for="uri_prefix">
                                    {{ meta.Question.uri_prefix.verbose_name }}
                                </label>

                                <div class="input-group">
                                    <input class="form-control"
                                           id="question_uri_prefix"
                                           type="text"
                                           ng-model="service.values.uri_prefix" />

                                    <span class="input-group-btn">
                                        <button type="button" class="btn btn-default"
                                            title="{% trans 'Insert default URI Prefix' %}"
                                            ng-click="service.values.uri_prefix = service.settings.default_uri_prefix">

                                            <span class="fa fa-magic"></span>
                                        </button>
                                    </span>
                                </div>

                                <p class="help-block">
                                    {{ meta.Question.uri_prefix.help_text }}
                                </p>

                                <ul class="help-block list-unstyled" ng-show="service.errors.uri_prefix">
                                    <li ng-repeat="error in service.errors.uri_prefix">
                                        {$ error $}
                                    </li>
                                </ul>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <formgroup
                                data-id="question_key"
                                data-type="text"
                                data-label="{{ meta.Question.key.verbose_name }}"
                                data-help="{% trans 'The internal identifier of this question.' %}"
                                data-model="service.values.key"
                                data-errors="service.errors.key">
                            </formgroup>
                        </div>
                    </div>
                    <div ng-show="service.values.copy">
                        <formgroup
                            data-id="question_questionset"
                            data-type="selectnumber"
                            data-label="{{ meta.Question.questionset.verbose_name }}"
                            data-help="{{ meta.Question.questionset.help_text }}"
                            data-model="service.values.questionset"
                            data-errors="service.errors.questionset"
                            data-options="service.questionsets"
                            data-options-label="uri"
                            data-options-null="1"
                            data-quicksearch="true">
                        </formgroup>
                    </div>
                    <div ng-hide="service.values.copy">
                        <formgroup
                            data-id="question_comment"
                            data-type="textarea"
                            data-label="{{ meta.Question.comment.verbose_name }}"
                            data-help="{% trans 'Additional internal information about this question.' %}"
                            data-model="service.values.comment"
                            data-errors="service.errors.comment">
                        </formgroup>
                        <formgroup
                            data-id="question_locked"
                            data-type="checkbox"
                            data-label="{{ meta.Question.locked.verbose_name }}"
                            data-help="{{ meta.Question.locked.help_text }}"
                            data-model="service.values.locked"
                            data-errors="service.errors.locked">
                        </formgroup>
                        <div>
                            <ul class="nav nav-tabs" role="tablist">
                                <li role="presentation" class="active" ng-class="{
                                    'has-error':
                                        service.errors.questionset ||
                                        service.errors.order ||
                                        service.errors.attribute ||
                                        service.errors.is_collection ||
                                        service.errors.widget_type ||
                                        service.errors.value_type ||
                                        service.errors.unit ||
                                        service.errors.width
                                }">
                                    <a class="control-label" href="#questions-general"
                                        role="tab" data-toggle="tab" aria-controls="questions-general">

                                        {% trans 'General' %}
                                        <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
                                    </a>
                                </li>
                                {% for lang_code, lang in languages %}
                                <li role="presentation" ng-class="{
                                    'has-error':
                                        service.errors.text_{{ lang_code }} ||
                                        service.errors.help_{{ lang_code }} ||
                                        service.errors.verbose_name_{{ lang_code }} ||
                                        service.errors.verbose_name_plural_{{ lang_code }},
                                    'has-warning':
                                        service.values.id && !service.values.text_{{ lang_code }}
                                }">
                                    <a class="control-label" href="#questions-{{ lang_code }}"
                                        role="tab" data-toggle="tab" aria-controls="questions-{{ lang_code }}">

                                        {{ lang }}
                                        <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
                                    </a>
                                </li>
                                {% endfor %}
                                <li role="presentation" ng-class="{
                                    'has-error':
                                        service.errors.optionsets
                                }">
                                    <a class="control-label" href="#questions-optionsets"
                                        role="tab" data-toggle="tab" aria-controls="questions-optionsets">

                                        {% trans 'Option sets' %}
                                        <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
                                    </a>
                                </li>
                                <li role="presentation" ng-class="{
                                    'has-error':
                                        service.errors.conditions
                                }">
                                    <a class="control-label" href="#questions-conditions"
                                        role="tab" data-toggle="tab" aria-controls="questions-conditions">

                                        {% trans 'Conditions' %}
                                        <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
                                    </a>
                                </li>
                                <li role="presentation" ng-class="{
                                    'has-error':
                                        service.errors.minimum ||
                                        service.errors.maximum ||
                                        service.errors.step
                                }">
                                    <a class="control-label" href="#questions-range"
                                        role="tab" data-toggle="tab" aria-controls="questions-range">

                                        {% trans 'Range' %}
                                        <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
                                    </a>
                                </li>
                                <li role="presentation" ng-class="{
                                    'has-error':
                                        {% for lang_code, lang in languages %}
                                        service.errors.text_{{ lang_code }} ||
                                        {% endfor %}
                                        service.errors.default_option ||
                                        service.errors.default_external_id
                                }">
                                    <a class="control-label" href="#questions-default"
                                        role="tab" data-toggle="tab" aria-controls="questions-default">

                                        {% trans 'Default' %}
                                        <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
                                    </a>
                                </li>
                            </ul>

                            <div class="tab-content">
                                <div role="tabpanel" class="tab-pane active" id="questions-general">
                                    <div class="row">
                                        <div class="col-sm-8">
                                            <formgroup
                                                data-id="question_questionset"
                                                data-type="selectnumber"
                                                data-label="{{ meta.Question.questionset.verbose_name }}"
                                                data-help="{{ meta.Question.questionset.help_text }}"
                                                data-model="service.values.questionset"
                                                data-errors="service.errors.questionset"
                                                data-options="service.questionsets"
                                                data-options-label="uri"
                                                data-options-null="1"
                                                data-quicksearch="true">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-4">
                                            <formgroup
                                                data-id="question_order"
                                                data-type="number"
                                                data-label="{{ meta.Question.order.verbose_name }}"
                                                data-help="{{ meta.Question.order.help_text }}"
                                                data-model="service.values.order"
                                                data-errors="service.errors.order">
                                            </formgroup>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <formgroup
                                                data-id="question_attribute"
                                                data-type="selectnumber"
                                                data-label="{{ meta.Question.attribute.verbose_name }}"
                                                data-help="{{ meta.Question.attribute.help_text }}"
                                                data-model="service.values.attribute"
                                                data-errors="service.errors.attribute"
                                                data-options="service.attributes"
                                                data-options-label="uri"
                                                data-options-null="1"
                                                data-quicksearch="true">
                                            </formgroup>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-6">
                                            <formgroup
                                                data-id="question_is_collection"
                                                data-type="checkbox"
                                                data-label="{{ meta.Question.is_collection.verbose_name }}"
                                                data-help="{{ meta.Question.is_collection.help_text }}"
                                                data-model="service.values.is_collection"
                                                data-errors="service.errors.is_collection">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-6">
                                            <formgroup
                                                data-id="question_is_optional"
                                                data-type="checkbox"
                                                data-label="{{ meta.Question.is_optional.verbose_name }}"
                                                data-help="{{ meta.Question.is_optional.help_text }}"
                                                data-model="service.values.is_optional"
                                                data-errors="service.errors.is_optional">
                                            </formgroup>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-sm-3">
                                            <formgroup
                                                data-id="question_widget_type"
                                                data-type="select"
                                                data-label="{{ meta.Question.widget_type.verbose_name }}"
                                                data-help="{{ meta.Question.widget_type.help_text }}"
                                                data-model="service.values.widget_type"
                                                data-errors="service.errors.widget_type"
                                                data-options="service.widgettypes"
                                                data-options-label="text">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-3">
                                            <formgroup
                                                data-id="question_value_type"
                                                data-type="select"
                                                data-label="{{ meta.Question.value_type.verbose_name }}"
                                                data-help="{{ meta.Question.value_type.help_text }}"
                                                data-model="service.values.value_type"
                                                data-errors="service.errors.value_type"
                                                data-options="service.valuetypes"
                                                data-options-label="text">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-3">
                                            <formgroup
                                                data-id="question_unit"
                                                data-type="text"
                                                data-label="{{ meta.Question.unit.verbose_name }}"
                                                data-help="{{ meta.Question.unit.help_text }}"
                                                data-model="service.values.unit"
                                                data-errors="service.errors.unit">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-3">
                                            <formgroup
                                                data-id="question_width"
                                                data-type="text"
                                                data-label="{{ meta.Question.width.verbose_name }}"
                                                data-help="{{ meta.Question.width.help_text }}"
                                                data-model="service.values.width"
                                                data-errors="service.errors.width">
                                            </formgroup>
                                        </div>
                                    </div>
                                </div>
                                {% for lang_code, lang in languages %}
                                <div role="tabpanel" class="tab-pane" id="questions-{{ lang_code }}">
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <formgroup
                                                data-id="question_text_{{ lang_code }}"
                                                data-type="text"
                                                data-label="{% trans 'Text' %} ({{ lang }})"
                                                data-help="{% trans 'The text for this question.' %}"
                                                data-model="service.values.text_{{ lang_code }}"
                                                data-errors="service.errors.text_{{ lang_code }}"
                                                data-warning="service.values.id && !service.values.text_{{ lang_code }}">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-12">
                                            <formgroup
                                                data-id="question_help_{{ lang_code }}"
                                                data-type="textarea"
                                                data-label="{% trans 'Help' %} ({{ lang }})"
                                                data-help="{% trans 'The help text for this question.' %}"
                                                data-model="service.values.help_{{ lang_code }}"
                                                data-errors="service.errors.help_{{ lang_code }}">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-6">
                                            <formgroup
                                                data-id="question_verbose_name_{{ lang_code }}"
                                                data-type="text"
                                                data-label="{% trans 'Verbose name' %} ({{ lang }})"
                                                data-help="{% trans 'The verbose name for this question.' %}"
                                                data-model="service.values.verbose_name_{{ lang_code }}"
                                                data-errors="service.errors.verbose_name_{{ lang_code }}">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-6">
                                            <formgroup
                                                data-id="question_verbose_name_plural_{{ lang_code }}"
                                                data-type="text"
                                                data-label="{% trans 'Verbose name plural' %} ({{ lang }})"
                                                data-help="{% trans 'The plural verbose name for this question.' %}"
                                                data-model="service.values.verbose_name_plural_{{ lang_code }}"
                                                data-errors="service.errors.verbose_name_plural_{{ lang_code }}">
                                            </formgroup>
                                        </div>
                                    </div>
                                </div>
                                {% endfor %}
                                <div role="tabpanel" class="tab-pane" id="questions-optionsets">
                                    <formgroup
                                        data-id="question_optionsets"
                                        data-type="selectmultiple"
                                        data-label="{% trans 'Option sets' %}"
                                        data-model="service.values.optionsets"
                                        data-errors="service.errors.optionsets"
                                        data-options="service.optionsets"
                                        data-options-label="uri"
                                        data-help="{% trans 'Hold down Control, or Command on a Mac, to select more than one.' %}">
                                    </formgroup>
                                </div>
                                <div role="tabpanel" class="tab-pane" id="questions-conditions">
                                    <formgroup
                                        data-id="question_conditions"
                                        data-type="selectmultiple"
                                        data-label="{% trans 'Conditions' %}"
                                        data-model="service.values.conditions"
                                        data-errors="service.errors.conditions"
                                        data-options="service.conditions"
                                        data-options-label="uri"
                                        data-help="{% trans 'Hold down Control, or Command on a Mac, to select more than one.' %}">
                                    </formgroup>
                                </div>
                                <div role="tabpanel" class="tab-pane" id="questions-range">
                                    <div class="row">
                                        <div class="col-sm-4">
                                            <formgroup
                                                data-id="question_minimum"
                                                data-type="text"
                                                data-label="{{ meta.Question.minimum.verbose_name }}"
                                                data-help="{{ meta.Question.minimum.help_text }}"
                                                data-model="service.values.minimum"
                                                data-errors="service.errors.minimum">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-4">
                                            <formgroup
                                                data-id="question_maximum"
                                                data-type="text"
                                                data-label="{{ meta.Question.maximum.verbose_name }}"
                                                data-help="{{ meta.Question.maximum.help_text }}"
                                                data-model="service.values.maximum"
                                                data-errors="service.errors.maximum">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-4">
                                            <formgroup
                                                data-id="question_step"
                                                data-type="text"
                                                data-label="{{ meta.Question.step.verbose_name }}"
                                                data-help="{{ meta.Question.step.help_text }}"
                                                data-model="service.values.step"
                                                data-errors="service.errors.step">
                                            </formgroup>
                                        </div>
                                    </div>
                                </div>
                                <div role="tabpanel" class="tab-pane" id="questions-default">
                                    <div class="row">
                                        {% for lang_code, lang in languages %}
                                        <div class="col-sm-12">
                                            <formgroup
                                                data-id="question_default_text_{{ lang_code }}"
                                                data-type="textarea"
                                                data-label="{% trans 'Default answer text' %} ({{ lang }})"
                                                data-help="{% trans 'The default answer text for this question.' %}"
                                                data-model="service.values.default_text_{{ lang_code }}"
                                                data-errors="service.errors.default_text_{{ lang_code }}"
                                                rows="2">
                                            </formgroup>
                                        </div>
                                        {% endfor %}
                                        <div class="col-sm-8">
                                            <formgroup
                                                data-id="question_default_option"
                                                data-type="selectnumber"
                                                data-label="{{ meta.Question.default_option.verbose_name }}"
                                                data-help="{{ meta.Question.default_option.help_text }}"
                                                data-model="service.values.default_option"
                                                data-errors="service.errors.default_option"
                                                data-options="service.options"
                                                data-options-label="uri"
                                                data-options-null="1"
                                                data-quicksearch="true">
                                            </formgroup>
                                        </div>
                                        <div class="col-sm-4">
                                            <formgroup
                                                data-id="question_default_external_id"
                                                data-type="text"
                                                data-label="{{ meta.Question.default_external_id.verbose_name }}"
                                                data-help="{{ meta.Question.default_external_id.help_text }}"
                                                data-model="service.values.default_external_id"
                                                data-errors="service.errors.default_external_id">
                                            </formgroup>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">
                        {% trans 'Close' %}
                    </button>
                    <button type="button" class="btn btn-primary"
                            ng-click="service.submitFormModal('questions')">
                        <span ng-show="service.copy">{% trans 'Copy' %}</span>
                        <span ng-hide="service.copy">{% trans 'Save' %}</span>
                    </button>
                </div>
            </div>
        </div>
    </div>
