{% extends "base.html" %}
{% load compress %}
{% load static %}
{% load i18n %}
{% load is_assistant_or_coordinator %}
{% load is_course_coordinator %}

{% block wrapper_classes %}
wrapper-course-lesson
{% endblock %}

{% block js %}
    {{block.super}}
    <script>
        window.course_id = {{ course.pk }};
        window.course_slug = '{{ course.slug }}';
    </script>

    <link rel="stylesheet" type="text/css" href="https://unpkg.com/ui-select@0.19/dist/select.min.css" />

    <script src="https://unpkg.com/tinymce@4.5.10/tinymce.min.js"></script>
    <script src="https://unpkg.com/angular-ui-tinymce@0.0.19/dist/tinymce.min.js"></script>
    <script src="{% static 'js/ui.tinymce.courses-legacy.config.js' %}"></script>

    <script src="https://unpkg.com/angular-filter@0.5.17/dist/angular-filter.min.js"></script>
    <script src="https://unpkg.com/ng-file-upload@12.0.4/dist/ng-file-upload.min.js"></script>
    <script src="https://unpkg.com/checklist-model@1.0.0/checklist-model.js"></script>

    {% compress js %}
    <script type="text/javascript" src="{% static 'js/messages/app.js' %}"></script>
    <script type="text/javascript" src="{% static 'js/messages/controllers.js' %}"></script>
    <script type="text/javascript" src="{% static 'js/messages/services.js' %}"></script>
    <script type="text/javascript" src="{% static 'js/factories/timtec-models.js' %}"></script>
    <script type="text/javascript" src="{% static 'js/discussion-services.js' %}"></script>
    {% endcompress %}

{% endblock %}

{% block body_attrs %}
  ng-app="messages"
{% endblock %}

{% block content %}

<div class="course messages messages-list">

    {% if in_admin %}
        {% include "_admin_header_inline.html" with course=course %}
    {% else %}
        {% include "_course-header-inline.html"  with course=course %}
    {% endif %}

    <!-- COURSE-CONTENT -->
    <section class="page container-fluid">
        <header class="page-header" ng-controller="NewMessageController">
            <h2>Avisos</h2>
            {% if user|is_assistant_or_coordinator:course %}
            <a class="btn btn-primary edit" ng-click="new_message()">Novo aviso</a>
            {% endif %}
            <script type="text/ng-template" id="newMessageModal.html">
                <div class="modal-header">
                    <button type="button" class="close" ng-click="cancel()" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Novo aviso</h4>
                </div>
                <div class="modal-body">
                    <div class="alert alert-danger" role="alert" ng-show="empty_msg_subject_error || empty_msg_body_error ">
                        <ul>
                            <li ng-show="empty_msg_subject_error"><a href="#id-title">Assunto do aviso</a></li>
                            <li ng-show="empty_msg_body_error"><a href="#wmd-input">Texto do aviso</a></li>
                        </ul>
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" ng-model="new_message.subject" placeholder="{% trans 'Subject' %}"/>
                    </div>
                    <div class="form-group">
                        <textarea ui-tinymce
                                    ng-model="new_message.message" class="form-control" rows="10"
                                    placeholder="Mensagem" name="content" required></textarea>
                    </div>
                    <div class="form-group row">
                        <div class="students col-lg-12">
                            <p><label><input type="checkbox" ng-model="modal.all_checked"> Enviar para todas as turmas deste curso</label></p>
                            {% verbatim %}
                            <div ng-repeat="klass in classes">
                                <label ng-show="!modal.all_checked"><input type="checkbox" checklist-model="classes.checked" checklist-value="klass.students"> {{ klass.name }}</label>
                            </div>
                            {% endverbatim %}
                            <div class="autocomplete" ng-show="!modal.all_checked">
                                <input type="text" id="search-user" class="form-control" ng-model="asyncSelected"
                                                placeholder="{% trans 'Type the name, email or user name of the professor to search...' %}"
                                                uib-typeahead="user as user.formated_name for user in getUsers($viewValue)"
                                                typeahead-loading="loadingLocations"
                                                typeahead-on-select="on_select_student($model); asyncSelected = '';"
                                                typeahead-min-length="2"
                                                typeahead-wait-ms="500">
                                <i ng-show="loadingLocations" class="loading fa fa-refresh fa-spin"></i>
                            </div>
                            <table class="table table-list" ng-show="!modal.all_checked && recipient_list.length">
                                <tr>
                                    <th class="fullname">Nome completo</th>
                                    <th class="email">Email</th>
                                    <th class="username">Usuário</th>
                                    <th class="actions">Ações</th>
                                </tr>
                                <tr ng-repeat="student in recipient_list">
                                    {% verbatim %}
                                    <td>{{student.name}}</td>
                                    <td>{{student.email}}</td>
                                    <td>{{student.username}}</td>
                                    {% endverbatim %}
                                    <td class="textcenter"><button class="btn btn-warning" ng-click="remove_student($index)"><i class="fa fa-trash-o"></i></button></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" ng-click="cancel()">Fechar</button>
                        <button type="button" class="btn btn-success" ng-click="send()" ng-disabled="!classes_ready">Enviar</button>
                    </div>
                </div>
            </script>
        </header>
        {% block messages_content %}
        <div ng-controller="MessagesListController">
            <div class="message" ng-class="{'unread': !message.is_read}" ng-repeat="message in messages" ng-if="messages.length > 0">
                <div class="message-body">
                    {% verbatim %}
                    <div class="message-header">
                        <h4 class="message-subject">{{ message.subject }}</h4>
                        <span class="message-date">{{ message.date|date:"dd/MM/yy" }}</span>
                    </div>
                    {% endverbatim %}
                    <div class="message-text">
                        <span ng-bind-html="strip_html(message.message | limitTo : 300)"></span>
                        {% if in_admin %}
                            {% verbatim %}
                            <a class="btn btn-default see-more" ng-href="/admin/course/{{message.course_slug}}/message/{{message.id}}">
                                ver aviso completo
                            </a>
                             {% endverbatim %}
                        {% else %}
                            {% verbatim %}
                            <a class="btn btn-default see-more" ng-href="/course/{{message.course_slug}}/message/{{message.id}}">
                                ver aviso completo
                            </a>
                             {% endverbatim %}
                        {% endif %}
                    </div>
                </div>
            </div>

            <div class="message" style="margin-bottom: 20px;" ng-if="messages.length == 0 || !messages">
                <div class="row">
                    <div class="col-lg-11">
                        <div class="body">
                          <span>
                              Não há avisos importantes neste curso para você no momento.
                          </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% endblock %}
    </section>
</div>
{% endblock %}