{% macro action_bar(institute, case, collaborators) %}
  <ul class="nav nav-sidebar">
    <li class="nav-sidebar-heading">
      Case
      <h4>{{ case.display_name }}</h4>
    </li>
    <h5>
      <span class="glyphicon glyphicon-file"></span>
      Reports
    </h5>
    <li>{{ reports(institute, case) }}</li>
    <li>{{ analysis_date(case) }}</li>
    <li>{{ genome_build(case) }}</li>
    <li>{{ status(institute, case) }}</li>
    <li>{{ assign(institute, case) }}</li>
    {% if not (case.is_archived or case.is_research) %}
      <li>{{ research(case) }}</li>
    {% endif %}
    <li>{{ rerun(case) }}</li>
    <li>{{ rank_model(case) }}</li>
    <li>{{ share_case(institute, case, collaborators) }}</li>
  </ul>
{% endmacro %}

{% macro reports(institute, case) %}
  <div class="nav-sidebar-item">
      <a class="btn btn-default form-control" href="{{ url_for('cases.case_report', institute_id=institute._id, 
                case_name=case.display_name) }}">General</a>
  </div>
  {% if case.multiqc %}
    <p>
      <div class="nav-sidebar-item">
        <a class="btn btn-default form-control" href="{{ url_for('cases.multiqc', institute_id=institute._id, 
                  case_name=case.display_name) }}">
        MultiQC
      </a>
      </div>
    </p>
  {% endif %}
  {% if case.delivery_report %}
    <div class="nav-sidebar-item">
        <a href="{{ url_for('cases.delivery_report', institute_id=institute._id, 
                    case_name=case.display_name) }}" class="btn btn-default form-control" target="_blank">
        Delivery
      </a>
      <hr>
      {% for analysis in case.analyses %}
        <a href="{{ url_for('cases.delivery_report', institute_id=institute._id,
                            case_name=case.display_name, date=analysis.date) }}" target="_blank">
          {{ analysis.date.date() }}
        </a>
        <br />
      {% endfor %}
    </div>
  {% endif %}
  
  {% if config.SQLALCHEMY_DATABASE_URI %}
  <div class="nav-sidebar-item">
    <form method="POST" action="{{ url_for('report.report', sample_id=case.individual_ids, level=institute.coverage_cutoff, panel_name=case.panel_names|join(', ')) }}" target="_blank">
      <input type="hidden" name="gene_ids" value="{{ case.default_genes|join(',') }}"></input>

      <div class="form-group">
        <button type="submit" class="btn btn-default form-control">Coverage report</button>
      </div>
    </form>
    <p>Based on: {{ case.panels|selectattr('is_default')|map(attribute='panel_name')|join(', ') }}</p>
    <p>
      <form method="POST" action="{{ url_for('report.pdf', level=institute.coverage_cutoff, dl='yes') }}">
        <input type="hidden" name="gene_ids" value="{{ case.default_genes|join(',') }}" />
        <input type="hidden" name="panel_name" value="{{ case.panel_names|join(', ') }}" />
        {% for sample_id in case.individual_ids %}
				    <input type="text" name="sample_id" value="{{ sample_id }}" hidden>
			  {% endfor %}
        <div class="form-group">
          <button type="submit" class="btn btn-default form-control">
            Download PDF (coverage)
          </button>
        </div>
      </form>
    </p>
  </div>
  {% endif %}

{% endmacro %}

{% macro analysis_date(case) %}
  <div class="nav-sidebar-item">
    <h5>
      <span class="glyphicon glyphicon-time"></span>
      Analysis date
    </h5>
    {{ case.analysis_date.date() }}
    {% if case.is_migrated %}
      <span class="label label-info">migrated</span>
    {% endif %}
  </div>
{% endmacro %}

{% macro genome_build(case) %}
  <div class="nav-sidebar-item">
    <h5>
      <span class="glyphicon glyphicon-wrench"></span>
      Genome build
    </h5>
    {{ case.genome_build }}
  </div>
{% endmacro %}

{% macro status(institute, case) %}
  <form method="POST"
        action="{{ url_for('cases.status', institute_id=institute._id, case_name=case.display_name) }}">

    <div class="nav-sidebar-item">
      <h5>
        <span class="glyphicon glyphicon-{{ 'star' if case.status == 'prioritized' else 'star-empty' }}"></span>
        Status: {{ case.status|capitalize }}
      </h5>

      <div class="btn-group btn-group-justified">
        <div class="btn-group">
          {% if case.status != 'archived' %}
            <button type="submit" class="btn btn-warning btn-sm" name="status" value="archived" onclick="return confirm('Are you sure? This will disable the alignment view and delete analysis files. You will have to request a FULL rerun to continue evaluating e.g. research variants.')">
              Archive
            </button>
          {% else %}
            <button type="submit" class="btn btn-default btn-sm" name="status" value="active">
              Unarchive
            </button>
          {% endif %}
        </div>
        <div class="btn-group">
          <button name="status" value="{{ 'active' if case.status == 'prioritized' else 'prioritized' }}" type="submit" class="btn btn-default btn-sm">
            {{ 'De-prioritize' if case.status == 'prioritized' else 'Prioritize' }}
          </button>
        </div>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro assign(institute, case) %}
  <div class="nav-sidebar-item">
    <h5>
      <span class="glyphicon glyphicon-user"></span>
      Assignees
    </h5>
    {% for user in case.assignees %}
      <div class="form-group">
        <form method="POST"
              action="{{ url_for('cases.assign', institute_id=institute._id,
                                 case_name=case.display_name, user_id=user._id) }}">
          <button name="action" value="DELETE" class="btn btn-default form-control">
            <span class="glyphicon glyphicon-remove"></span>
            {{ user.name }}
          </button>
        </form>
      </div>
    {% endfor %}
    <form method="POST"
          action="{{ url_for('cases.assign', institute_id=institute._id,
                             case_name=case.display_name) }}"
          title="You adopt a case to show that you assume responsibility for it. Your name will show up in relation to the case for all team members to see.">
      <button class="btn btn-primary form-control">Assign yourself</button>
    </form>
  </div>
{% endmacro %}

{% macro research(case) %}
  <div class="nav-sidebar-item">
    <h5>
      <span class="glyphicon glyphicon-education"></span>
      Research list
    </h5>
    {% if case.research_requested %}
      <i>Research pending</i>
    {% else %}
      <button type="button" class="btn btn-danger form-control" data-toggle="modal" data-target="#research-modal">Request research</button>
    {% endif %}
  </div>
{% endmacro %}

{% macro research_modal(institute, case) %}
  <form action="{{ url_for('cases.research', institute_id=institute._id,
                           case_name=case.display_name) }}" method="POST">
    <div class="modal fade" id="research-modal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Request research list</h4>
          </div>
          <div class="modal-body">
            <p>Please confirm that you want to <strong>upload research variants for {{ case.display_name }}</strong>.</p>
            <p>You also confirm that you have the <strong>relevant consent</strong>.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Confirm</button>
          </div>
        </div>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro rerun(case) %}
  <div class="nav-sidebar-item">
    <h5><span class="glyphicon glyphicon-retweet"></span> Reruns</h5>
    {% if case.rerun_requested %}
      <i>Rerun pending</i>
    {% else %}
      <button type="button" class="btn btn-warning form-control" data-toggle="modal" data-target="#rerun-modal">Request rerun</button>
    {% endif %}
  </div>
{% endmacro %}

{% macro rerun_modal(institute, case) %}
  <form action="{{ url_for('cases.rerun', institute_id=institute._id,
                           case_name=case.display_name) }}" method="POST">
    <div id="rerun-modal" class="modal fade">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Request rerun</h4>
          </div>
          <div class="modal-body">
            <p>Please confirm that you want to <strong>rerun {{ case.display_name }}</strong>. This will <strong>replace current variants</strong> with updated information.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Confirm</button>
          </div>
        </div>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro rank_model(case) %}
  <div class="nav-sidebar-item">
    <h5>
      <span class="glyphicon glyphicon-barcode"></span>
      Rank model
    </h5>
    Version {{ case.rank_model_version }}
  </div>
{% endmacro %}

{% macro share_case(institute, case, collaborators) %}
  {% if case.owner == institute._id %}
  <div class="nav-sidebar-item">
    <h5>
      <span class="glyphicon glyphicon-cog"></span>
      Share case
    </h5>
    {% if collaborators %}
      <div class="form-group">
        <form action="{{ url_for('cases.share', institute_id=institute._id, case_name=case.display_name) }}" method="POST">
          <div class="input-group">
            <select class="form-control" name="collaborator">
              <option>Select institute</option>
              {% for collab_id, collab_name in collaborators %}
                <option value="{{ collab_id }}">{{ collab_name }}</option>
              {% endfor %}
            </select>
            <div class="input-group-btn">
              <button type="submit" class="btn btn-default">Share</button>
            </div>
          </div>
        </form>
      </div>
    {% endif %}
    {% if case.o_collaborators %}
      <form method="POST" action="{{ url_for('cases.share', institute_id=institute._id, case_name=case.display_name) }}">
        <input type="hidden" name="revoke" />
        <div class="input-group">
          <select class="form-control" name="collaborator">
            <option>Select institute</option>
            {% for collab_id, collab_name in case.o_collaborators %}
              <option value="{{ collab_id }}">{{ collab_name }}</option>
            {% endfor %}
          </select>
          <div class="input-group-btn">
            <button type="submit" class="btn btn-default">Revoke</button>
          </div>
        </div>
      </form>
    {% endif %}
  </div>
  {% endif %}
{% endmacro %}
