{% load i18n %}
{% load tz %}
    <div class="panel panel-success">

            <div class="panel-heading">
                 <span class="text text-muted">
                    <i class="fas fa-list-alt fa-fw" aria-hidden="true"></i> CRFs</span>
                    <label class="small pull-right" title="visit report datetime">{{ appointment.object.visit.report_datetime|localtime|date:"SHORT_DATETIME_FORMAT" }}</label>
            </div>

            <div class="table-responsive">
            <table class="table table-striped table-hover table-condensed">
                <tbody>

                    {% for crf in crfs %}
                        <tr>
                            <td>
                            <span class="{% if crf.entry_status == NOT_REQUIRED %}text text-muted small{% endif %}">
                              {{ forloop.counter }}.&nbsp;
                            </span>
                                {% if crf.entry_status == NOT_REQUIRED %}
                                    <span class="text text-muted small">{{ crf.object.verbose_name }}</span>
                                {% else %}
                                {{ crf.object.verbose_name }}
                                    {% if crf.object.user_created %}
                                        <p class="text small text-default">
                                            {% if crf.object.crf_status == INCOMPLETE %}<span class="label label-danger" data-toggle="tooltip" title="CRF is flagged as incomplete"><span class="fas fa-tag"></span></span>{% endif %}                                            {{ crf.object.user_created }} on {{ crf.object.object.created|date:"SHORT_DATETIME_FORMAT" }}
                                            {% if crf.object.user_modified %} | {{ crf.object.user_modified }} on {{ crf.object.object.modified|date:"SHORT_DATETIME_FORMAT" }}{% endif %}

                                        </p>
                                    {% endif %}
                                {% endif %}
                            </td>
                            <td>
                              <span class="btn-group pull-right">
                              {% if crf.entry_status != NOT_REQUIRED %}
                                <a id="{{ crf.object.html_id }}" name="{{ crf.object.html_id }}" role="button" class="btn btn-sm {% if crf.object.id %}btn-success{% else %}{% if crf.entry_status == NOT_REQUIRED %} disabled btn-default{% else %}btn-warning{% endif %}{% endif %}"
                                        {% if crf.object.id and crf.object.has_change_permission %}
                                            {%  if request.site.id == crf.site.id %}href="{{ crf.object.href }}"{% else %} disabled {% endif %}><span class="fas fa-pen"></span> Change
                                        {% elif not crf.object.id and crf.object.has_add_permission %}
                                            {%  if request.site.id == crf.site.id %}href="{{ crf.object.href }}"{% else %} disabled {% endif %}><span class="fas fa-plus"></span> Add
                                        {% elif crf.object.id and crf.object.has_view_permission %}
                                            {%  if request.site.id == crf.site.id %}href="{{ crf.object.href }}"{% else %} disabled {% endif %}><span class="fas fa-eye"></span> View
                                        {% else %}
                                            disabled >{% if crf.object.id %}<span class="fas fa-pen"></span> Change{% else %}<span class="fas fa-plus"></span> Add{% endif %}
                                        {% endif %}
                                </a>
                                {% if crf.object.history_url %}
                                    <a href="{{ crf.object.history_url }}" role="button" class="btn btn-sm btn-default">{% trans 'History' %}</a>
                                {% else %}
                                    <a disabled role="button" class="btn btn-sm btn-default">{% trans 'History' %}</a>
                                {% endif %}
                                {% if perms.edc_data_manager.add_dataquery %}
                                  <a href="{% url 'edc_data_manager_admin:edc_data_manager_dataquery_add' %}?registered_subject={{ registered_subject_pk }}&sender={{ request.user.id }}&visit_schedule={{visit_schedule_pk|default:''}}&visit_code_sequence={{ appointment.visit_code_sequence }}&title={{ crf.object.verbose_name }}"
                                    role="button" class="btn btn-sm btn-default">Query</a>
                                {% endif %}

                              {% else%}
                                <span class="text text-muted small">not required</span>
                              {% endif %}
                              </span>
                            </td>
                        </tr>
                    {% endfor %}
                    {% if not crfs %}
                        <tr>
                            <td colspan="3">
                            <span class="text text-muted small">
                                  There are no required CRFs for this visit. See also the PRN CRFs.
                            </span>
                            </td>
                        </tr>
                    {% endif %}
                    {% if appointment.object.visit.visit.crfs_prn %}
                        <tr><td colspan="3">
                        <span class="text text-default small pull-right">
                        PRN CRFs are available.
                        </span>
                        </td></tr>
                    {% endif %}


                </tbody>
            </table>
        </div>
    </div>
