{% load edc_dashboard_extras %}
{% load i18n l10n tz %}
{% load static %}
{% load edc_adverse_event_extras %}

{% has_perms_for_tmg_role as may_access_page %}
{% if may_access_page %}
  <div class="col-md-12">
    <div class="row">
      {% if not results %}
        <div class="alert alert-success"><i class="fas fa-thumbs-up fa-lg"></i> {{ empty_message }}</div>
      {% endif %}
      {% has_perms_for_tmg_role as may_access_page %}
      {% if may_access_page %}
        {% for result in results %}
          <div class="panel panel-default">
            <div class="panel-heading clearfix">
              <span class="badge">{{ forloop.counter }}</span> <span class="text text-uppercase">{{ result.subject_identifier }}</span> {% copy_string_to_clipboard_button result.subject_identifier %} <span class="text text-muted small">{{ utc_date|timeuntil:result.report_datetime.date }} ago</span>
              <span class="pull-right">
                <div class="btn-group">
                  <a class="btn btn-sm btn-default" role="button" title="Go to action item" href="{{ result.get_absolute_url }}?next={{ listboard_filter_url }},subject_identifier&subject_identifier={{ result.subject_identifier }}">{{ result.identifier }}</a>
                  <a class="btn btn-sm btn-primary" role="button" title="go to list of reported visits for this subject" href="{% url subject_review_listboard_url|default:"subject_review_listboard_url_cannot_be_none" subject_identifier=result.subject_identifier %}?q={{ result.subject_identifier }}">Subject Review  <i class="fas fa-share fa-fw"></i></a>
                </div>
              </span>
            </div>
            <div class="panel-body">
            <div class="row">
              <div class="col-md-4">

                <div class="list-group">
                  {# AE TMG #}
                  <div class="list-group-item list-group-item-default"><i class="fa-solid fa-folder-tree fa-fw"></i> TMG documents in workflow</div>
                  {% render_tmg_panel action_item=result by_user_created_only=True counter=forloop.counter report_status=result.reference_obj.report_status next_url_name=listboard_filter_url %}

                {% death_report_tmg_queryset action_item=result as qs %}
                {% for death_report_tmg in qs %}
                  {% render_tmg_panel reference_obj=death_report_tmg by_user_created_only=True counter=forloop.counter next_url_name=listboard_filter_url %}
                {% endfor %}

                {% death_report_tmg2_queryset action_item=result as qs %}
                {% for death_report_tmg2 in qs %}
                  {% render_tmg_panel reference_obj=death_report_tmg2 by_user_created_only=True counter=forloop.counter next_url_name=listboard_filter_url %}
                {% endfor %}
              </div>
              </div>

              <div class="col-md-4">
                <div class="list-group">
                  {# AE INTIAL #}
                  <div class="list-group-item"><i class="fa-regular fa-file fa-fw"></i> First document in workflow</div>
                  {% if result.parent_action_item %}
                    {% render_tmg_panel action_item=result.parent_action_item counter=forloop.counter next_url_name=listboard_filter_url %}
                    {% if result.related_action_item and result.parent_action_item.reference_obj.action_identifier != result.related_action_item.reference_obj.action_identifier %}
                      {% render_tmg_panel action_item=result.related_action_item counter=forloop.counter next_url_name=listboard_filter_url %}
                    {% endif %}
                  {% endif %}
                </div>
              </div>

              <div class="col-md-4">

                <div class="list-group">
                <div class="list-group-item"><i class="fa-solid fa-folder-tree fa-fw"></i> Clinical documents in workflow</div>
                {% ae_followup_queryset ae_initial=result.parent_action_item.reference_obj as qs %}
                {% if qs.count == 0 %}<div class="list-group-item">There are no follow-ups to this AE</div>{% endif  %}
                {% for ae_followup in qs %}
                  {% render_tmg_panel reference_obj=ae_followup counter=forloop.counter next_url_name=listboard_filter_url %}
                {% endfor %}

                {% death_report_queryset subject_identifier=result.subject_identifier as qs %}
                {% for death_report in qs %}
                  {% render_tmg_panel reference_obj=death_report counter=forloop.counter next_url_name=listboard_filter_url %}
                {% endfor %}
                </div>

              </div>
            </div>
            </div>
          </div>
        {% endfor %}
      {% else %}
        <div class="alert alert-danger">
          <i class="fas fa-hand-paper fa-lg"></i> You do not have permissions to view this page. Only users with the TMG Role are allowed access. Check with your administrator.
        </div>
      {% endif %}
      <div class="well">{% paginator_row %}</div>
    </div>
  </div>
{% endif %}
