{% extends "layout_bs4.html" %}
{% from "utils.html" import comments_table %}
{% from "variants/utils.html" import sv_filters, cell_rank %}
{% from "variants/components.html" import frequency_cell_general %}

{% block title %}
  {{ super() }} - {{ institute.display_name }} - {{ case.display_name }} - SV variants
{% endblock %}

{% block css %}
{{ super() }}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/css/bootstrap-select.min.css">
{% endblock %}

{% block top_nav %}
  {{ super() }}
  <li class="nav-item">
    <a class="nav-link text-nowrap" href="{{ url_for('cases.index') }}">Institutes</a>
  </li>
  <li class="nav-item">
    <a class="nav-link text-nowrap" href="{{ url_for('cases.cases', institute_id=institute._id) }}">
      {{ institute.display_name }}
    </a>
  </li>
  <li class="nav-item">
    <a class="nav-link text-nowrap" href="{{ url_for('cases.case', institute_id=institute._id, case_name=case.display_name) }}">
      {{ case.display_name }}
    </a>
  </li>
  <li class="nav-item active">
    <span class="navbar-text">{{ form.variant_type.data|capitalize }}  Structural variants</span>
  </li>
{% endblock %}

{% block top_nav_right %}
  <li class="nav-item text-nowrap"><p class="navbar-text">Panels: {{ (form.gene_panels.data or ['All'])|join(',') }}</p></li>
  {{ super() }}
{% endblock %}

{% block content_main %}
<form method="POST" id="filters_form" action="{{url_for('variants.sv_variants', institute_id=institute._id, case_name=case.display_name)}}"
  onsubmit="return validateForm()">
  <div class="container-float">
    <div class="card panel-default" id="accordion">
      <div class="card-header">
        <strong><a data-toggle="collapse" data-parent="#accordion" href="#collapseFilters">SvFilters</a></strong>
      </div>
      <div id="collapseFilters" class="card-body panel-collapse collapse">
        <div class="card-body">{{ sv_filters(form, institute, case) }}</div>
      </div>
    </div>
  </div>
  <div class="card mt-3">
    <table id="variantsTable" class="table table-hover table-bordered">
      <thead>
        <tr>
          <th>Rank</th>
          <th>Score</th>
          <th>Type</th>
          <th>Chr</th>
          <th>Start loc</th>
          <th>Stop loc</th>
          <th>Length</th>
          <th>Region</th>
          <th>Function</th>
          <th>Frequency</th>
          <th>Gene(s)</th>
        </tr>
      </thead>
      <tbody>
        {% for variant in variants %}
          {{ variant_row(variant) }}
        {% else %}
          <tr>
              <td colspan="11">No matching variants</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  <div class="container-fluid">
    {{ pagination() }}
  </div>
</form>
{% endblock %}

{% macro variant_row(variant) %}
  {% if variant.dismiss_variant %}
  <tr class="dismiss">
  {% else %}
  <tr>
  {% endif %}
    <td class="text-left">
      {{ cell_rank(variant, institute, case, form, manual_rank_options) }}
    </td>
    <td class="text-right">{{ variant.rank_score|int }}</td>
    <td>{{ variant.sub_category|upper }}</td>
    <td>{{ variant.chromosome if variant.chromosome == variant.end_chrom else variant.chromosome+'-'+variant.end_chrom }}</td>
    <td>{{ variant.position }}</td>
    <td>{{ 'inf' if variant.end == 100000000000 else variant.end }}</td>
    <td class="text-right">{{ variant.length }}</td>
    <td>
      {% if 'region_annotations' in variant %}
        {% for annotation in variant.region_annotations[:3] %}
          <div>{{ annotation }}</div>
        {% endfor %}
      {% endif %}
    </td>
    <td>
      {% if 'functional_annotations' in variant %}
        {% for annotation in variant.functional_annotations[:3] %}
          <div>{{ annotation }}</div>
        {% endfor %}
      {% endif %}
    </td>
    <td>
      {{ frequency_cell_general(variant) }}
    </td>
    <td>
      <div class="d-flex justify-content-between align-items-center">
        <div>
          {% if variant.hgnc_symbols|length >= 5 %}
            {{ variant.hgnc_symbols[:2]|join(', ') }} [...] {{ variant.hgnc_symbols[-2:]|join(', ') }}
          {% else %}
            {% for symbol in variant.hgnc_symbols %}
              <div>{{ symbol }}</div>&nbsp;&nbsp;
            {% endfor %}
          {% endif %}
        </div>
        {% if variant.hgnc_symbols %}
          <span class="badge badge-secondary">{{ variant.hgnc_symbols|length }}</span>
        {% endif %}
      </div>
    </td>
  </tr>
{% endmacro %}

{% macro pagination() %}
  <div class="container-fluid">
    {% if more_variants %}
    <div class="d-flex justify-content-around">
        <div>
            <button name="page" type="submit" class="btn btn-outline-secondary mx-auto d-block" value=1>First page</button>
        </div>
       <div>
          <button name="page" type="submit" class="btn btn-outline-secondary mx-auto d-block" value={{ page + 1 }}>Next page</button>
       </div>
    </div>
    {% else %}
      <i class="text-muted">No more variants to display</i>
      <button name="page" type="submit" class="btn btn-outline-secondary mx-auto d-block" value=1>First page</button>
    {% endif %}
  </div>
{% endmacro %}


{% block scripts %}
  {{ super() }}
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/1.0.11/purify.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sticky-table-headers/0.1.19/js/jquery.stickytableheaders.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/js/bootstrap-select.min.js"></script>
  <script>
    function validateForm(){
      var valid_chroms = [];
      i = 1;
      while(valid_chroms.push(i++) <22){};
      valid_chroms.push("X","Y","MT");
      var chrom = document.forms["filters_form"].elements["chrom"].value;
      if(chrom && !valid_chroms.map(String).includes(chrom)){
        alert("Chromosome field is not valid. Accepted values are: "+valid_chroms)
        return false
      }
      var start = document.forms["filters_form"].elements["start"].value
      var end = document.forms["filters_form"].elements["end"].value
      if(start || end){
        if(!chrom){
          alert("Chromosome field is required");
          return false;
        }
        else if( !start || !end){
          alert("Both start and end coordinates are required");
          return false;
        }
        else if( (isNaN(start) || isNaN(end)) || end<start ){
          alert("Coordinate field not valid");
          return false;
        }
      }
      return true;
    }

    $('select[multiple]').selectpicker({
      width: '100%'
    });

    $(function () {
      $('[data-toggle="tooltip"]').tooltip();
      $('[data-toggle="popover"]').popover({
        sanitizeFn: function (content) {
          return DOMPurify.sanitize(content)
        },
        container: 'body',
      });
    })
  </script>
{% endblock %}
