{% extends "modal_base.html" %}
{% load i18n %}

{% block title %}
    {% if object.pk %}
	    {% trans 'Edit open question' %}
    {% else %}
        {% trans 'New open question' %}
    {% endif %}
{% endblock %}

{% block actions %}
    <a href="{{ questionnaire.get_detail_url }}" class="btn btn-default">
        {% trans 'Back to the list of questions' %}
    </a>
{% endblock %}

{% block body %}
    <form action="{% if object.pk %}{{ object.get_edit_url }}{% else %}{{ create_url }}{% endif %}" method="post" id="questionnaire-question-form">{% csrf_token %}
        <div class="modal-body">
            {% include 'cms/extras/form.html' with display='block' %}
        </div>    
        <div class="modal-footer clearfix">
            <a class="btn btn-default pull-left" data-dismiss="modal">{% trans 'Cancel' %}</a>
            <div class="pull-right">
                <input class="btn btn-primary" type="submit" value="{% trans 'Confirm' %}" />
                {% include 'questionnaires/_question_add_another.html' %}
            </div>
        </div>
    </form>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#questionnaire-question-form').ajaxForm({
                beforeSubmit:function(fields, $form) {
                    $('input[type="submit"], button').attr('disabled', 'disabled');
                },
                success: function(htmlResponse, statusText, xhr, $form){
                    var $htmlResponse = $(htmlResponse);
                    
                    if ($htmlResponse.closest('#questionnaire-question-form').length) {
                        $('#dialog-box-modal .modal-content').html($htmlResponse);
                        $('input[type="submit"], button').attr('disabled', false);
                    } else {
                        var addAnother = $form.data('add_another');
                        
                        $('body').html($htmlResponse);
                        $('.dropdown-toggle').dropdown();
                        
                        if (addAnother) {
                            $('#question-create').modal('show');
                        }
                    }
                }
            });
        });
    </script>
{% endblock %}
