{% extends "layout_bs4.html" %}
{% from "utils.html" import comments_table %}
{% from "variants/utils.html" import sv_filters, cell_rank, pagination_footer, pagination_hidden_div, dismiss_variants_block, filter_form_footer %}
{% 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('overview.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 %}
  <div class="container-float">
    <form method="POST" id="filters_form" action="{{url_for('variants.sv_variants', institute_id=institute._id, case_name=case.display_name)}}"
  onsubmit="return validateForm()">
    {{ pagination_hidden_div(page) }}
      <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>
        <!--Expand filters form if filters were used in a previous POST request-->
        <div id="collapseFilters" class="card-body panel-collapse collapse {{ 'show' if expand_search is sameas true }}">
          <div class="card-body">{{ sv_filters(form, institute, case) }}</div>
        </div>
        {{ filter_form_footer(form, result_size, total_variants, more_variants, page, variants|length) }}
      </div>
      <div class="card mt-3">
        <table id="variantsTable" class="table table-hover table-bordered" style="table-layout: fixed">
          <thead>
            <tr>
              <th style="width:3%"></th>
              <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>
        {{ dismiss_variants_block(dismiss_variant_options, institute, case) }}
      </div>
    </form>
  </div>
  <div class="container-fluid">
  {{ pagination_footer(more_variants, page) }}
  </div>
{% endblock %}

{% macro variant_row(variant) %}
  {% if variant.dismiss_variant %}
   <tr class="dismiss">
  {% elif variant._id in case.causatives %}
   <tr class="causative">
  {% else %}
  <tr>
  {% endif %}
    <td><input type="checkbox" value="{{variant._id}}" name="dismiss" onclick="enableDismiss()"></td>
    <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">{{ '-' if variant.length == 100000000000 else 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 %}


{% 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 src="{{ url_for('variants.static', filename='form_scripts.js') }}"></script>
  <script type="text/javascript">

    var startElem = document.getElementById("start");
    var endElem = document.getElementById("end");
    var coords=[startElem,endElem];

    coords.forEach(function (item, index) {
      item.addEventListener("input", function() {
      eraseChromPosString()});
    });

    var chromSel = document.getElementById("chrom");
    chromSel.addEventListener("change", function() {
      populateCytobands({{cytobands|safe}});
    });

    var chromPosInpt = document.getElementById("chrom_pos");
    chromPosInpt.addEventListener("change", function() {
      populateCytobands({{cytobands|safe}});
    })

   chromPosInpt.addEventListener("input", function() {
      updateCoordinateFields(event.target)
   })

    var cytoStart = document.getElementById("cytoband_start");
    initSearchConstraints(cytoStart, startElem)

    var cytoEnd = document.getElementById("cytoband_end");
    initSearchConstraints(cytoEnd, endElem)

    document.getElementById('hide_dismissed').onchange = function() {
      document.getElementById('expand_search').value = "False";
      var the_form = document.forms['filters_form'];
      the_form.submit();
    };

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

    window.onload=function() {
        populateCytobands({{cytobands|safe}});
    }

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