{% set layout = 'ClarolineCoreBundle:Workspace:layout.html.twig' %}

{% if isDesktop() %}
    {% set layout = 'ClarolineCoreBundle:Desktop:layout.html.twig' %}
{% endif %}

{% set _resource = survey %}
{% set surveyId = survey.getId() %}

{% extends layout %}

{% block title %}
    {{ survey.getResourceNode().getName() | striptags | raw }}
{% endblock %}

{% block resourceBreadcrumb %}
    <li class="active">{{ _resource.getResourceNode().getName() }}</li>
{% endblock %}
    
{% block section_content %}
    <div id="survey-data-element" data-survey-id="{{ surveyId }}">
    </div>
    <div class="panel-heading">
        <h3 class="panel-title">
            {{ survey.getResourceNode().getName() }}
            <div class="dropdown pull-right">
                <i class="fa fa-cog pull-right pointer-hand dropdown-toggle"
                   id="dropdownMenu1"
                   data-toggle="dropdown"
                ></i>
                <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
                    <li role="presentation">
                        <a role="menuitem"
                           tabindex="-1"
                           href="{{ path('claro_survey_parameters', {'survey': surveyId}) }}"
                        >
                            <i class="fa fa-cogs"></i>
                            {{ 'parameters'|trans({}, 'survey') }}
                        </a>
                    </li>
                    <li role="presentation">
                        <a role="menuitem"
                           tabindex="-1"
                           href="{{ path('claro_survey_questions_management', {'survey': surveyId, 'orderedBy': 'title', 'order': 'ASC'}) }}"
                        >
                            <i class="fa fa-question"></i>
                            {{ 'questions_management'|trans({}, 'survey') }}
                        </a>
                    </li>
                    
                    {% if status == 'published' or status == 'closed' %}
                        {% if questionResult is not empty %}
                            <li role="presentation" class="divider"></li>
                            <li role="presentation">
                                <a role="menuitem" tabindex="-1"
                                   href="{{ path('claro_survey_results_show', {'survey': surveyId, 'question': questionResult.getId()}) }}"
                                >
                                    <i class="fa fa-bar-chart-o"></i>
                                    {{ 'view_survey_results'|trans({}, 'survey') }}
                                </a>
                            </li>
                        {% endif %}
                    {% endif %}
                </ul>
            </div>
        </h3>
    </div>
    
    {% if status == 'unpublished' %}
        <br>
        <div class="alert alert-info">
            {{ 'how_to_publish_message'|trans({}, 'survey') }}
        </div>
    {% elseif status == 'published' %}
        <br>
        <div class="alert alert-success">
            {{ 'published_survey_message'|trans({}, 'survey') }}
        </div>
    {% else %}
        <br>
        <div class="alert alert-danger">
            {{ 'closed_survey_message'|trans({}, 'survey') }}
        </div>
    {% endif %}
    <div class="panel-body">
        <div>
            <a href="{{ path('claro_survey_question_create_form', {'survey': surveyId, 'source': 'survey'}) }}"
               class="btn btn-primary margin-bottom-md"
            >
                <i class="fa fa-plus-circle"></i>
                {{ 'create_question'|trans({}, 'survey') }}
            </a>
            <span id="add-question-btn" class="btn btn-primary margin-bottom-md">
                <i class="fa fa-search"></i>
                {{ 'add_question'|trans({}, 'survey') }}
            </span>
            <a href="{{ path('claro_survey_question_title_create_form', {'survey': surveyId}) }}"
               class="btn btn-primary margin-bottom-md"
            >
                <i class="fa fa-navicon"></i>
                {{ 'create_title'|trans({}, 'survey') }}
            </a>
            <span class="btn btn-primary view-survey-btn margin-bottom-md"
                  data-survey-id="{{ surveyId }}"
            >
                <i class="fa fa-eye"></i>
                {{ 'display_survey'|trans({}, 'survey') }}
            </span>
            
            {% if status == 'published' or status == 'closed' %}
                <a href="{{ path('claro_survey_answer_form', {'survey': surveyId}) }}"
                   class="btn btn-success margin-bottom-md"
                >
                    <i class="fa fa-check-square-o"></i>
                    {{ 'answer_to_survey'|trans({}, 'survey') }}
                </a>
            {% endif %}
        </div>
        
        {% if survey.getDescription() is not empty %}
            <div class="well well-sm">
                {{ survey.getDescription()|raw }}
            </div>
        {% endif %}
        
        {% if questionRelations|length == 0 %}
            <div class="alert alert-danger" role="alert">
                {{ 'no_question'|trans({}, 'survey') }}
            </div>
        {% else %}
            <div class="table-responsive">
                <table class="table table-bordered" id="question-table">

                {% for relation in questionRelations %}
                    {% set question = relation.getQuestion() %}
                    {% set questionType = question.getType() %}
                    <tr data-question-relation-id="{{ relation.getId() }}">
                        <td>
                            <b>
                                [{{ questionType|trans({}, 'survey') }}]
                                
                                {% if questionType != 'title' %}
                                    {{ question.getTitle() }}
                                {% endif %}
                            </b>
                            {{ question.getQuestion()|raw }}
                        </td>
                        <td>
                            
                            {% if questionType != 'title' %}
                                <span class="btn btn-default view-question-btn"
                                      data-question-id="{{ question.getId() }}"
                                      data-toggle="tooltip"
                                      data-placement="top"
                                      title="{{ 'view_question_btn_explanation'|trans({}, 'survey') }}"
                                >
                                    <i class="fa fa-eye"></i>
                                </span>
                                <span class="btn edit-mandatory-btn {% if relation.getMandatory %}btn-success{% else %}btn-default{% endif %}"
                                      data-relation-id="{{ relation.getId() }}"
                                      data-toggle="tooltip"
                                      data-placement="top"
                                      title="{% if relation.getMandatory() %}{{ 'mandatory_answer_message'|trans({}, 'survey') }}{% else %}{{ 'not_mandatory_answer_message'|trans({}, 'survey') }}{% endif %}"
                                >
                                    <i class="fa fa-exclamation"></i>
                                </span>
                                <a href="{{ path(
                                        'claro_survey_question_edit_form',
                                        {'question': question.getId(), 'survey': surveyId, 'source': 'survey'}
                                    ) }}"
                                    class="btn btn-default"
                                    data-toggle="tooltip"
                                    data-placement="top"
                                    title="{{ 'edit_question_btn_explanation'|trans({}, 'survey') }}"
                                >
                                    <i class="fa fa-edit"></i>
                                </a>
                                <a href="{{ path('claro_survey_remove_question', {'survey': surveyId, 'question': question.getId()}) }}"
                                   class="btn btn-danger"
                                   data-toggle="tooltip"
                                   data-placement="top"
                                   title="{{ 'remove_question_btn_explanation'|trans({}, 'survey') }}"
                                >
                                    <i class="fa fa-trash-o"></i>
                                </a>
                            {% else %}
                                <a href="{{ path(
                                        'claro_survey_question_title_edit_form',
                                        {'question': question.getId(), 'survey': surveyId}
                                    ) }}"
                                    class="btn btn-default"
                                    data-toggle="tooltip"
                                    data-placement="top"
                                    title="{{ 'edit_question_title_btn_explanation'|trans({}, 'survey') }}"
                                >
                                    <i class="fa fa-edit"></i>
                                </a>
                                <a href="{{ path('claro_survey_question_title_delete', {'survey': surveyId, 'question': question.getId()}) }}"
                                   class="btn btn-danger"
                                   data-toggle="tooltip"
                                   data-placement="top"
                                   title="{{ 'delete_question_title_btn_explanation'|trans({}, 'survey') }}"
                                >
                                    <i class="fa fa-trash-o"></i>
                                </a>
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </table>
            </div>
        {% endif %}
    </div>
    
    <div id="add-question-box" class="modal fade" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h3>{{ 'add_question_box_title'|trans({}, 'survey') }}</h3>
                </div>
                <div id="add-question-body" class="modal-body">
                </div>
                <div class="modal-footer">
                    <input type="button"
                           class="btn btn-default"
                           data-dismiss="modal"
                           value="{{ 'close'|trans({}, 'platform') }}"
                    />
                </div>
            </div>
        </div>
    </div>
    
    <div id="view-question-box" class="modal fade" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div id="view-question-body" class="modal-body">
                </div>
                <div class="modal-footer">
                    <input type="button"
                           class="btn btn-default"
                           data-dismiss="modal"
                           value="{{ 'close'|trans({}, 'platform') }}"
                    />
                </div>
            </div>
        </div>
    </div>
    
    <div id="view-survey-box" class="modal fade" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div id="view-survey-body" class="modal-body">
                </div>
                <div class="modal-footer">
                    <input type="button"
                           class="btn btn-default"
                           data-dismiss="modal"
                           value="{{ 'close'|trans({}, 'platform') }}"
                    />
                </div>
            </div>
        </div>
    </div>  
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script type="text/javascript" src="{{ url('bazinga_exposetranslation_js', { 'domain_name': 'survey' }) }}"></script>
    <script
        src='{{ asset('bundles/clarolinesurvey/js/surveyManagement.js') }}'
        type="text/javascript">
    </script>
{% endblock %}