{% extends "layouts/base.html" %}

{% block container %}
  <div class="row">
    <div class="col-md-6">
      {% for (source, vtype, mode), cases in case_groups.items() %}
        {% if mode == 'gemini' %}
          {{ case_form(source, vtype, cases) }}
        {% else %}
          {{ case_list(cases) }}
        {% endif %}
      {% endfor %}
    </div>

    {% if config['STORE_ENABLED'] %}
      <div class="col-md-6">
        <div class="panel panel-default">
          <div class="panel-heading">Gene lists</div>
          <ul class="list-group">
            {% for gene_list in gene_lists %}
              <li class="list-group-item">
                <a href="{{ url_for('public.gene_list', list_id=gene_list.list_id) }}" title="{{ gene_list.list_id }}">{{ gene_list.list_id }}</a>

                <form class="badge" action="{{ url_for('public.delete_genelist', list_id=gene_list.list_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 gene lists added.</li>
            {% endfor %}
          </ul>

          <div class="panel-heading">Upload a new gene list</div>
          <div class="panel-body">
            <form action="{{ url_for('public.gene_list') }}" method="POST" enctype="multipart/form-data">
              <div class="form-group">
                <input class="form-control" name="list_id" placeholder="gene list id"></input>
              </div>
              <div class="form-group row">
                <div class="col-md-8">
                  <input class="form-control" type="file" name="file" required></input>
                </div>
                <div class="col-md-4">
                  <button class="btn btn-default form-control" type="submit">Upload</button>
                </div>
              </div>
            </form>
          </div>
        </div>

        {% if queries %}
          <div class="panel panel-default">
            <div class="panel-heading">GEMINI queries</div>
            <ul class="list-group">
              {% for query in queries %}
                <li class="list-group-item">
                  {{ query.name_query }}

                  <form class="badge" action="{{ url_for('variants.delete_query', query_id=query.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 queries added.</li>
              {% endfor %}
            </ul>
          </div>
        {% endif %}
      </div>
    {% endif %}
  </div>
{% endblock %}


{% macro case_form(source, vtype, cases) %}
  <form action="{{ url_for('public.add_case') }}" method="POST">
    <input type="text" name="source" value="{{ source }}" hidden>
    <input type="text" name="type" value="{{ vtype }}" hidden>
    <div class="panel panel-default">
      <div class="panel-heading">Source: <strong>{{ source }}</strong></div>

      {% for case in cases %}
        <div class="panel-heading">
          <a href="{{ url_for('public.case', case_id=case.case_id) }}">
            {{ case.name }}
          </a>
          <span class="label label-info">
            {{ case.variant_mode }}
          </span>
          <span class="label label-warning">
            {{ case.variant_type }}
          </span>

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

        <ul class="list-group">
          {% for individual in case.individuals %}
            <li class="list-group-item">
              <div class="row">
                <div class="col-xs-1">
                  <input name="ind_id" type="checkbox" value="{{ individual.ind_id }}">
                </div>
                <div class="col-xs-10">
                  <a href="{{ url_for('public.individual', ind_id=individual.ind_id) }}">
                    {{ individual.name }}
                  </a>
                </div>
                <div class="col-xs-1">
                  {% if individual.is_affected %}
                    <span class="badge">
                      <span class="glyphicon glyphicon-exclamation-sign"></span>
                    </span>
                  {% endif %}
                </div>
              </div>
            </li>
          {% else %}
            <li class="list-group-item">No individuals.</li>
          {% endfor %}
        </ul>
      {% endfor %}
      <div class="panel-footer">
        <div class="row">
          <div class="col-xs-9">
            <input class="form-control" name="case_id" type="text" placeholder="New case name" required>
          </div>
          <div class="col-xs-3">
            <button class="btn btn-default form-control">Add case</button>
          </div>
        </div>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro case_list(cases) %}
  <div class="panel panel-default">
    {% for case in cases %}
      <div class="panel-heading">
        <a href="{{ url_for('public.case', case_id=case.case_id) }}">
          {{ case.name }}
        </a>
        <span class="label label-info">
          {{ case.variant_mode }}
        </span>
        <span class="label label-warning">
          {{ case.variant_type }}
        </span>

        <a class="pull-right" href="{{ url_for('variants.variants', case_id=case.case_id) }}">
          Variants
        </a>
      </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>
            {% if individual.is_affected %}
              <span class="badge">
                <span class="glyphicon glyphicon-exclamation-sign"></span>
              </span>
            {% endif %}
          </li>
        {% else %}
          <li class="list-group-item">No individuals.</li>
        {% endfor %}
      </ul>
    {% endfor %}
  </div>
{% endmacro %}
