{% extends dashboard_base_template %}
{% load static i18n %}
{% load edc_visit_schedule_extras edc_subject_dashboard_extras edc_dashboard_extras edc_adverse_event_extras %}

{% block main %}
  <div id="subject_dashboard" class="container">
    <div class="row ">
      <div class="panel-group col-md-12">
        <div class="panel panel-info">
          <div class="panel-heading">
            <h6 class="panel-title clearfix">
              <i class="{% if gender == MALE %}fas fa-male{% else %}fas fa-female{% endif %} fa-lg fa-fw"></i> <a id="demographics-panel-title">{{ subject_identifier }} </a> {% copy_string_to_clipboard_button subject_identifier %}
              {% block extra_demographic_title %}{% endblock extra_demographic_title %}
              {% if related_visit and related_visit.site.id != request.site.id %}<span class="bg-warning text small text-default">{% translate "You are viewing a subject who is not from site" %} {{ request.site.id }}</span>{% endif %}
              <span class="pull-right">
                {% render_tmg_button_group subject_identifier=subject_identifier %}
                <div class="btn-group">
                  {% 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 }}" role="button" class="btn btn-sm btn-default" data-toggle="tooltip" title="Add new data query">Query</a>{% endif %}
                  {% block top_bar_buttons_extra %}{% endblock top_bar_buttons_extra %}
                  {% block schedule_button %}
                    {% if related_visit %}{% render_subject_schedule_button subject_dashboard_url subject_identifier %}<a href="{% url subject_review_listboard_url|default:'subject_review_listboard_url_cannot_be_none' subject_identifier=subject_identifier appointment=appointment.id|stringformat:"s" %}#{{ subject_identifier }}-{{ appointment.visit_code }}-{{ appointment.visit_code_sequence }}" role="button" class="btn btn-sm btn-primary" data-toggle="tooltip" title="{% trans "Go to subject review" %}"><i class="fa-solid fa-reply fa-1x fa-fw" aria-hidden="true"></i>{% trans "Review" %}</a>{% else %}<a href="{% url subject_review_listboard_url|default:'subject_review_listboard_url_cannot_be_none' subject_identifier=subject_identifier %}" role="button" class="btn btn-sm btn-primary" data-toggle="tooltip" title="{% trans "Go to subject review" %}"><i class="fa-solid fa-reply fa-1x fa-fw" aria-hidden="true"></i>{% trans "Review" %}</a>{% endif %}
                  {% endblock schedule_button %}
                  <a {% block close_url %}href="{% url subject_listboard_url|default:'subject_listboard_url_cannot_be_none' %}?q={{ subject_identifier }}"{% endblock close_url %} role="button" class="btn btn-sm btn-primary" data-toggle="tooltip" title="{% trans "Back to Subjects" %}"><i class="fa-solid fa-reply fa-1x fa-fw" aria-hidden="true"></i> {% trans "Subjects" %}</a>
                </div>
              </span>
            </h6>
          </div>
        </div>
      </div>
    </div>

    <div class="row ">
      <div class="panel-group col-md-12">
        <div class="panel-group col-md-9" id="accordion_schedule">
          {% if not visit_schedules %}
            <div class="alert alert-warning">
              <i class="far fa-hand-paper fa-lg fa-fw"></i> {% trans "Subject is not enrolled to any schedules" %}.
            </div>
            <div class="panel panel-default">
              <div class="panel-heading">
                <h6 class="panel-title"><i class="far fa-calendar fa-lg fa-fw"></i><a href="#">{% trans "Schedules" %}</a></h6>
              </div>
            </div>
          {% endif %}
          {% if appointment %}<!--begin appointment -->
            <div class="row">
              <div class="panel panel-info" id="subject_dashboard_for_{{ appointment.visit_schedule.name|slugify }}-{{ appointment.schedule.name|slugify }}">
                <div class="panel-heading clearfix">
                  <i class="far fa-calendar fa-lg fa-fw"></i><a data-toggle="collapse" data-parent="#accordion_{{ appointment.visit_schedule.name|slugify }}-{{ appointment.schedule.name|slugify }}" aria-hidden="true" href="#subject_dashboard_forms_for_{{ appointment.visit_schedule.name|slugify }}-{{ appointment.schedule.name|slugify }}">{{ appointment.visit_schedule.verbose_name }}<i class="fas fa-caret-right"></i> {{ appointment.schedule.verbose_name }}<i class="fas fa-caret-right"></i> {{ appointment.visit_code }}.{{ appointment.visit_code_sequence }} {{ appointment.title|lower|title }} {{ appointment.wrapped_visit.object.get_reason_display }}<span class="caret"></span></a>
                  <span class="pull-right">{% render_refresh_data_collection_schedule_button related_visit_id=related_visit.id visit_schedule_name=appointment.visit_schedule_name schedule_name=appointment.schedule_name %}</span>
                </div>
                <div class="panel-collapse collapse in" id="subject_dashboard_forms_for_{{ appointment.visit_schedule.name|slugify }}-{{ appointment.schedule.name|slugify }}" >
                  {% block crfs %}
                    {% include "edc_subject_dashboard/dashboard/crfs.html" %}
                  {% endblock crfs %}
                  {% block requisitions %}
                    {% include "edc_subject_dashboard/dashboard/requisitions.html" %}
                  {% endblock requisitions %}
                </div>
                {% subject_schedule_footer_row subject_identifier appointment.visit_schedule appointment.schedule subject_dashboard_url %}
                <div id="subject_dashboard_data_queries_for_{{ subject_identifier }}" class="panel-collapse collapse in">
                  {% block data_queries %}{% if open_data_queries %}{% include "edc_subject_dashboard/dashboard/data_queries.html" %}{% endif %}{% endblock data_queries %}
                </div>
              </div>
            </div>
          {% else %}
            {% for schedule_obj in onschedule_models %}
              <div class="row">
                <div id="subject_dashboard_for_{{ schedule_obj.visit_schedule.name|slugify }}-{{ schedule_obj.schedule.name|slugify }}" class="panel panel-info">
                  <div class="panel-heading clearfix">
                    <i class="far fa-calendar fa-lg fa-fw"></i>
                    <a data-toggle="collapse" data-parent="#accordion_{{ schedule_obj.visit_schedule.name|slugify }}-{{ schedule_obj.schedule.name|slugify }}" aria-hidden="true" title="click to expand / collapse" href="#subject_dashboard_appointments_for_{{ schedule_obj.visit_schedule.name|slugify }}-{{ schedule_obj.schedule.name|slugify }}">{{ schedule_obj.visit_schedule.verbose_name }}<i class="fas fa-caret-right"></i> {{ schedule_obj.schedule.verbose_name }}<span class="caret"></span></a>
                    <span class="pull-right">
                      {% render_refresh_appointments_button visit_schedule_name=schedule_obj.visit_schedule.name schedule_name=schedule_obj.schedule.name subject_identifier=subject_identifier %}
                      {% appointment_in_progress subject_identifier schedule_obj.visit_schedule schedule_obj.schedule %}
                    </span>
                  </div>
                  <div id="subject_dashboard_appointments_for_{{ schedule_obj.visit_schedule.name|slugify }}-{{ schedule_obj.schedule.name|slugify }}" class="panel-collapse collapse {% if current_visit_schedule.name == schedule_obj.visit_schedule.name and current_schedule.name == schedule_obj.schedule.name %}in{% endif %}">
                    {% block appointments %}
                      {% include "edc_subject_dashboard/dashboard/appointments.html" %}
                    {% endblock appointments %}
                  </div>
                  {% subject_schedule_footer_row subject_identifier schedule_obj.visit_schedule schedule_obj.schedule subject_dashboard_url %}
                  <div id="subject_dashboard_data_queries_for_{{ subject_identifier }}" class="panel-collapse collapse in">
                    {% if open_data_queries %}
                      {% include "edc_data_manager/dashboard_data_queries.html" %}
                    {% endif %}
                  </div>
                </div>
              </div>
            {% endfor %}
          {% endif %}
        </div> <!-- panel-group col-md-9 -->
        <div class="row">
          <div class="panel col-md-3">
            {% block top_bar %}{% include "edc_subject_dashboard/dashboard/topbar.html" %}{% endblock top_bar %}
            {% block side_bar %}{% include 'edc_subject_dashboard/dashboard/sidebar.html' %}{% endblock side_bar %}
          </div>
        </div>
      </div><!-- panel group col-md-12-->
    </div>
  </div> <!-- container -->
{% endblock main %}
