{% extends "layouts/base.html" %}
{% import "macros/form_utils.html" as utils %}
{% import "public_utils.html" as public_utils %}
{% import "macros/comments.html" as comments %}

{% block container %}

<div class="row">
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-body">
        <span class="lead">Case: <span class="text-primary">{{ case.name }}</span></span>

        <a class="btn btn-primary pull-right btn-sm" href="{{ url_for('variants.variants', case_id=case_id) }}">Variants</a>
      </div>
    </div>
  </div>
</div>

<div class="row">
  <div class="col-md-4">
    <div class="panel panel-default">
      <div class="panel-heading">Individuals</div>
      <ul class="list-group">
        {% for individual in case.individuals %}
          <li class="list-group-item">
            <a href="{{ url_for('public.individual', ind_id=individual.ind_id) }}">
              {{ individual.name }}
            </a>

            <small><strong class="text-info">
              {% if individual.sex_human != 'unknown' %}
                {{ individual.sex_human }}
              {% endif %}
            </strong></small>

            {% if individual.is_affected %}
              <span class="badge">AFFECTED</span>
            {% endif %}
          </li>
        {% else %}
          <li class="list-group-item">No individuals found.</li>
        {% endfor %}
      </ul>
    </div>
  </div>

  <div class="col-md-4">
    <div class="panel panel-default">
      <div class="panel-heading">Suspected variants</div>
      <ul class="list-group">
        {% for suspect in case.suspects %}
          <li class="list-group-item">
            <a href="{{ url_for('variants.variant', case_id=suspect.case.case_id, variant_id=suspect.variant_id) }}">{{ suspect.name }}</a>

            <form class="badge" action="{{ url_for('variants.delete_suspect', suspect_id=suspect.id) }}" method="POST" accept-charset="utf-8">
              <button class="no-style" type="submit">
                <span class="glyphicon glyphicon-remove"></span>
              </button>
            </form>
          </li>
        {% else %}
          <li class="list-group-item">No added suspects.</li>
        {% endfor %}
      </ul>
    </div>
  </div>

  <div class="col-md-4">
    <div class="panel panel-default">
      <div class="panel-heading">Synopsis</div>
      <div class="panel-body">
        {% if case.synopsis %}
          {{ case.synopsis|markdown }}
        {% else %}
          <span class="text-muted">Write about the case here...</span>
        {% endif %}
      </div>
      <div class="panel-footer">
        <button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#edit-synopsis">Edit</button>
      </div>
    </div>
  </div>
</div>

<div class="modal fade" id="edit-synopsis" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <form action="{{ url_for('public.synopsis', case_id=case.case_id) }}" method="POST">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Edit synopsis</h4>
        </div>
        <div class="modal-body">
          <textarea class="form-control" name="text" cols="30" rows="10">{{ case.synopsis if case.synopsis }}</textarea>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="submit" class="btn btn-primary">Save changes</button>
        </div>
      </form>
    </div>
  </div>
</div>

{% if config['STORE_ENABLED'] and config['PHENOMIZER_AUTH'] %}
  <div class="panel panel-default">
    <div class="panel-heading"><a href="http://compbio.charite.de/hpoweb/showterm?id=HP:0000118" target="_blank">Phenotypes</a></div>


    <div class="panel-body">
      <div class="row">
        <div class="col-md-8">
          {{ public_utils.phenotype_table(case.phenotypes) }}
        </div>

        <div class="col-md-4">
          {{ public_utils.phenotype_form(individuals=case.individuals) }}
        </div>
      </div>

      {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
          {% for category, message in messages %}
          <div class="alert alert-{{ category }} alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
            {{ message }}
          </div>
          {% endfor %}
        {% endif %}
      {% endwith %}

    </div>
  </div>

  <div class="row">
    <div class="col-md-4">
      <div class="panel panel-default">
        <div class="panel-heading">Gene lists</div>
        <ul class="list-group">
          {% for gene_list in case.gene_lists %}
            <li class="list-group-item">
              <span class="badge">{{ gene_list.gene_ids|length }} genes</span>
              <a href="{{ url_for('public.gene_list', list_id=gene_list.list_id) }}" title="{{ gene_list.list_id }}">{{ gene_list.list_id }}</a>
            </li>
          {% else %}
            <li class="list-group-item">No gene lists added.</li>
          {% endfor %}
        </ul>
      </div>
    </div>

    <div class="col-md-4">
      {{ comments.comments(case.case_id, case.case_comments) }}
    </div>

    <div class="col-md-4">
      <div class="panel panel-default">
        <div class="panel-heading">Resources</div>

        <ul class="list-group">
          {% for resource in case.resources %}
            <li class="list-group-item">
              <a href="{{ url_for('public.resource', resource_id=resource.id) }}" title="{{ resource.name }}">{{ resource.name }}</a>

              <form class="badge" action="{{ url_for('public.delete_resource', resource_id=resource.id) }}" method="POST" accept-charset="utf-8">
                <button class="no-style" type="submit">
                  <span class="glyphicon glyphicon-remove"></span>
                </button>
              </form>
            </li>

          {% endfor %}
        </ul>

        <div class="panel-body">
          <form action="{{ url_for('public.resources') }}" method="POST" enctype="multipart/form-data">
            <div class="form-group">
              <input class="form-control" name="name" placeholder="resource name"></input>
            </div>
            <div class="form-group row">
              <div class="col-xs-4">
                <input class="form-control" type="file" name="file" required></input>
              </div>
              <div class="col-xs-4">
                {{ utils.select_input('ind_id', case.individuals|map(attribute='ind_id')) }}
              </div>
              <div class="col-xs-4">
                <button class="btn btn-default form-control" type="submit">Upload</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
{% endif %}

{% endblock %}
